// file: EZdraw.java // adapted from code in "Java in a Nutshell" // by David Flanagan, O'Reilly & Associates, Inc. import java.applet.*; import java.awt.*; /** Class for a drawing tablet applet **/ public class EZdraw extends Applet { private int last_x = 0, last_y = 0; // The previous click or drag point private Button clear_button; // A button to clear the screen /** init is called before the applet is started **/ public void init() { make_button(); set_drawing_colors(); } /** Create button and add to applet **/ public void make_button() { clear_button = new Button("Clear"); clear_button.setForeground(Color.black); clear_button.setBackground(Color.lightGray); add(clear_button); } /** set the colors of background and pen **/ public void set_drawing_colors() { this.setBackground(Color.blue); this.setForeground(Color.white); } /* remember the current mouse coordinates */ private void remember(int x, int y) { last_x = x; last_y = y; } /** mouseDown is called when user clicks the mouse to start a drawing **/ public boolean mouseDown(Event e, int x, int y) { remember(x, y); return true; } /** mouseDrag is called when user drags the mouse **/ public boolean mouseDrag(Event e, int x, int y) { Graphics g = getGraphics(); g.drawLine(last_x, last_y, x, y); remember(x, y); return true; } /** action is called when the user clicks a button **/ public boolean action(Event e, Object arg) { if( e.target == clear_button ) { // Handle clear button Graphics g = getGraphics(); Rectangle r = bounds(); g.setColor(getBackground()); g.fillRect(r.x, r.y, r.width, r.height); return true; } return super.action(e, arg); } }