Download: Drag.jar
Drag the ball around on the applet to the left. The source for this
program is given below. It's written as both an applet (subclass
JApplet
) and application (it has a main method). The two source files
are given below.
The image is drawn with the Graphics method (fillOval
), but an
image could be easily displayed.
<applet code="DragDemo.class" archive="Drag.jar" width=200 height=200></applet>
DragDemo.java
01: /** DragDemo.java - Mouse drag example dual application/applet 02: @author Fred Swartz 03: @version 2004-04-15 04: */ 05: // "appletviewer DragDemo.java" works because of the following line. 06: // <applet code="DragDemo.class" height="200" width="200"></applet> 07: import java.awt.*; 08: import java.awt.event.*; 09: import javax.swing.*; 10: import javax.swing.event.*; 11: 12: /** This is an application because it has a main method. 13: It's also an applet because it extends JApplet. 14: */ 15: 16: public class DragDemo extends JApplet { 17: public static void main(String[] args) { 18: JFrame window = new JFrame(); 19: window.setTitle("Drag Demo"); 20: window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21: window.setContentPane(new DragBallPanel()); 22: window.pack(); 23: window.setVisible(true); 24: } 25: 26: public DragDemo() { 27: this.setContentPane(new DragBallPanel()); 28: } 29: } 30:
DragBallPanel.java
01: /** DragBallPanel.java - Panel that allows dragging a ball around. 02: @author Fred Swartz 03: @version 2004-04-15 04: */ 05: 06: import java.awt.*; 07: import java.awt.event.*; 08: import javax.swing.*; 09: 10: /** When the mousePressed listener is called to position is tested 11: to see if it's in the area of the ball. If it is, 12: (1) _canDrag is set true meaning pay attention to the MouseDragged events. 13: (2) Record where in the ball (relative to the upper left coordinates) 14: the mouse was clicked, because it looks best if we drag from there. 15: */ 16: 17: public class DragBallPanel extends JPanel implements MouseListener, MouseMotionListener { 18: 19: private static final int BALL_DIAMETER = 40; // Diameter of ball 20: //--- instance variables 21: /** Ball coords. Changed by mouse listeners. Used by paintComponent. */ 22: private int _ballX = 50; // x coord - set from drag 23: private int _ballY = 50; // y coord - set from drag 24: 25: /** Position in ball of mouse press to make dragging look better. */ 26: private int _dragFromX = 0; // pressed this far inside ball's 27: private int _dragFromY = 0; // bounding box. 28: 29: /** true means mouse was pressed in ball and still in panel.*/ 30: private boolean _canDrag = false; 31: 32: /** Constructor sets size, colors, and adds mouse listeners.*/ 33: public DragBallPanel() { 34: setPreferredSize(new Dimension(200, 200)); 35: setBackground(Color.blue); 36: setForeground(Color.yellow); 37: //--- Add the mouse listeners. 38: this.addMouseListener(this); 39: this.addMouseMotionListener(this); 40: } 41: 42: /** Ball is drawn at the last recorded mouse listener coordinates. */ 43: public void paintComponent(Graphics g) { 44: super.paintComponent(g); // Required for background. 45: g.fillOval(_ballX, _ballY, BALL_DIAMETER, BALL_DIAMETER); 46: }//end paintComponent 47: 48: /** Set _canDrag if the click is in the ball (or in the bounding 49: box, which is lazy, but close enuf for this program). 50: Remember displacement (dragFromX and Y) in the ball 51: to use as relative point to display while dragging. 52: */ 53: public void mousePressed(MouseEvent e) { 54: int x = e.getX(); // Save the x coord of the click 55: int y = e.getY(); // Save the y coord of the click 56: 57: if (x >= _ballX && x <= (_ballX + BALL_DIAMETER) 58: && y >= _ballY && y <= (_ballY + BALL_DIAMETER)) { 59: _canDrag = true; 60: _dragFromX = x - _ballX; // how far from left 61: _dragFromY = y - _ballY; // how far from top 62: } else { 63: _canDrag = false; 64: } 65: } 66: 67: 68: /** Set x,y to mouse position and repaint. */ 69: public void mouseDragged(MouseEvent e) { 70: if (_canDrag) { // True only if button was pressed inside ball. 71: //--- Ball pos from mouse and original click displacement 72: _ballX = e.getX() - _dragFromX; 73: _ballY = e.getY() - _dragFromY; 74: 75: //--- Don't move the ball off the screen sides 76: _ballX = Math.max(_ballX, 0); 77: _ballX = Math.min(_ballX, getWidth() - BALL_DIAMETER); 78: 79: //--- Don't move the ball off top or bottom 80: _ballY = Math.max(_ballY, 0); 81: _ballY = Math.min(_ballY, getHeight() - BALL_DIAMETER); 82: 83: this.repaint(); // Repaint because position changed. 84: } 85: } 86: 87: /** Turn off dragging if mouse exits panel. */ 88: public void mouseExited(MouseEvent e) { 89: _canDrag = false; 90: } 91: 92: public void mouseMoved (MouseEvent e) {} // ignore these events 93: public void mouseEntered (MouseEvent e) {} // ignore these events 94: public void mouseClicked (MouseEvent e) {} // ignore these events 95: public void mouseReleased(MouseEvent e) {} // ignore these events 96: } 97:
Maintained by John Loomis, updated Tue Mar 25 13:15:46 2008