/** * DragAMac3.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 DragAMac3 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() { addMouseListener(new LocalMouseListener()); addMouseMotionListener(new LocalMouseMotionListener()); 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 * events and to handle them */ private class LocalMouseListener extends MouseAdapter { /** * 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. } // Providing empty definitions for unused event methods is no longer // necessary, because they are in the MouseAdapter class. } /** * Define an inner class within DragAMac to listen for mouse dragged * events and to handle them. */ private class LocalMouseMotionListener extends MouseMotionAdapter { /** * Sets instance variable dragPoint to wherever the mouse is. * Then redraw the window with that as the upper left corner of the Mac. * Note that because this is in an inner class it can see dragPoint and * repaint. */ public void mouseDragged(MouseEvent event) { dragPoint = event.getPoint(); repaint(); // Will re-draw it at the new dragPoint. } // Providing empty definitions for unused event methods is no longer // needed, because they are in the MouseMotionAdapter class. } /** * 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); } }