In step 2, we add function that allows the user to draw on the drawing panel by holding down the mouse button and dragging. Note: no function has been added for the clear button.
The new function is found in the
MyPanel
class. Specifically, it implements twojava.awt.event
interfaces:MouseListener
andMouseMotionListener
. In the first example, below, only skeleton methods are included. In the second, code is added to perform the drawing function.Example Applet: Skeleton Version
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Step2 extends Applet { // Add drawing function Button clear; Panel buttonPanel; MyPanel drawPanel; public void init() { super.init(); clear = new Button ( "clear" ); buttonPanel = new Panel (); drawPanel = new MyPanel (); clear.setBackground ( new Color (187, 00, 00) ); clear.setForeground ( Color.white ); buttonPanel.setBackground ( new Color (00, 102, 155) ); buttonPanel.add ( clear ); drawPanel.setBackground ( Color.white ); drawPanel.setForeground ( Color.black ); setLayout ( new BorderLayout () ); add ( "North", buttonPanel ); add ( "Center", drawPanel ); drawPanel.addMouseListener ( drawPanel ); drawPanel.addMouseMotionListener ( drawPanel ); } // end init } // end Step2
class MyPanel extends Panel implements MouseListener, MouseMotionListener { //********************** Interface Methods// MouseListener Methods public void mousePressed ( MouseEvent e ) { } // end mousePressed public void mouseReleased ( MouseEvent e ) { } // end mouseReleased public void mouseEntered ( MouseEvent e ) { } // end mouseEntered public void mouseExited ( MouseEvent e ) { } // end mouseExited public void mouseClicked ( MouseEvent e ) { } // end mouseClicked // MouseMotionListener Methods public void mouseDragged ( MouseEvent e ) { } // end mouseDragged public void mouseMoved ( MouseEvent e ) { } // end mouseMoved } // end MyPanel
In the example, below, code is added in the
MyPanel
class to perform the drawing function, using the mouse.Example Applet
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Step2 extends Applet { // Add drawing function Button clear; Panel buttonPanel; MyPanel drawPanel; public void init() { super.init(); clear = new Button ( "clear" ); buttonPanel = new Panel (); drawPanel = new MyPanel (); clear.setBackground ( new Color (187, 00, 00) ); clear.setForeground ( Color.white ); buttonPanel.setBackground ( new Color (00, 102, 155) ); buttonPanel.add ( clear ); drawPanel.setBackground ( Color.white ); drawPanel.setForeground ( Color.black ); setLayout ( new BorderLayout () ); add ( "North", buttonPanel ); add ( "Center", drawPanel ); drawPanel.addMouseListener ( drawPanel ); drawPanel.addMouseMotionListener ( drawPanel ); } // end init } // end Step2
class MyPanel extends Panel implements MouseListener, MouseMotionListener { private int last_x, last_y, x, y; //********************** Interface Methods// MouseListener Methods public void mousePressed ( MouseEvent e ) { last_x = e.getX(); last_y = e.getY(); } // end mousePressed public void mouseReleased ( MouseEvent e ) { } // end mouseReleased public void mouseEntered ( MouseEvent e ) { requestFocus (); } // end mouseEntered public void mouseExited ( MouseEvent e ) { } // end mouseExited public void mouseClicked ( MouseEvent e ) { } // end mouseClicked // MouseMotionListener Methods public void mouseDragged ( MouseEvent e ) { Graphics g; x = e.getX(); y = e.getY(); g = this.getGraphics (); g.drawLine ( last_x, last_y, x, y ); last_x = x; last_y = y; } // end mouseDragged public void mouseMoved ( MouseEvent e ) { } // end mouseMoved } // end MyPanelRun the applet