/** * DragAMac.java * * Allows one to drag a drawing of a Mac around the screen. * Demonstrates events and listeners. * This version uses an inner class as the listener. * * @author Scot Drysdale on 4/12/00. Converted to JApplet 1/15/2012. * @author THC modified to use just one listener class that implements two interfaces. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DragAMac extends JApplet { private static final long serialVersionUID = 1L; private final int APPLET_WIDTH = 500; private final int APPLET_HEIGHT = 500; // Instance variable for the point where the mouse is now. private Point dragPoint = null; public void init() { LocalMouseListener listener = new LocalMouseListener(); addMouseListener(listener); addMouseMotionListener(listener); setSize(APPLET_WIDTH, APPLET_HEIGHT); Container cp = getContentPane(); // Content pane holds components cp.add(new Canvas()); // The canvas is the only component setVisible(true); // Makes the applet (and its components) visible } /** * Defines the canvas that the drawing is done on. */ private class Canvas extends JPanel { private static final long serialVersionUID = 1L; /** * Paints what goes on the canvas - an old-style Mac * @param page the graphic object of the panel for drawing on */ public void paintComponent(Graphics page) { super.paintComponent(page); if (dragPoint != null) drawAMac(dragPoint, page); } } /** * Define an "inner class" within DragAMac to listen for mouse pressed * and mouse dragged events and to handle them. */ private class LocalMouseListener implements MouseListener, MouseMotionListener { /** * Sets instance variable dragPoint to wherever the mouse located. * Then redraws the component with that as the upper left corner of the Mac. * Note that because this is in an inner class, it can see dragPoint * @param event the event that caused this callback. */ public void mousePressed(MouseEvent event) { dragPoint = event.getPoint(); repaint(); // will redraw it at the new dragPoint. } /** * When we drag the mouse, we should just do the same things as when * it's pressed. * @param event the event that caused this callback. */ public void mouseDragged(MouseEvent event) { mousePressed(event); } // Provide empty definitions for unused event methods. public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseMoved(MouseEvent event) {} } /** * Draws a Mac on the page with p as the upper left corner. * This method is declared private because it is called only by paintComponent. * * @param p point that is the upper left corner of Mac drawn * @param page the graphics object to draw on */ private void drawAMac(Point p, Graphics page) { page.setColor(Color.blue); page.drawRoundRect(p.x, p.y, 160, 210, 10, 10); // draw the Mac outline page.drawLine(p.x, p.y + 175, p.x + 160, p.y + 175); // Draw floppy drive page.setColor(Color.red); page.drawLine(p.x + 70, p.y + 147, p.x + 70, p.y + 143); page.drawLine(p.x + 70, p.y + 143, p.x + 120, p.y + 143); page.drawLine(p.x + 120, p.y + 143, p.x + 120, p.y + 138); page.drawLine(p.x + 120, p.y + 138, p.x + 145, p.y + 138); page.drawLine(p.x + 145, p.y + 138, p.x + 145, p.y + 152); page.drawLine(p.x + 145, p.y + 152, p.x + 120, p.y + 152); page.drawLine(p.x + 120, p.y + 152, p.x + 120, p.y + 148); page.drawLine(p.x + 120, p.y + 148, p.x + 70, p.y + 148); page.drawRect(p.x + 15, p.y + 145, 20, 20); // Apple Logo box page.setColor(Color.green); page.drawRoundRect(p.x + 20, p.y + 15, 120, 100, 20, 20); // screen outline page.setColor(Color.gray); page.fillRoundRect(p.x + 30, p.y + 25, 100, 80, 20, 20); } }