// // another example of the Java AWT, showing use of Graphics and // Canvas classes. // see class definitions for details. // import java.awt.* ; import java.awt.event.* ; // // a Scribble object is a CloseableFrame containing a ScribblePad // (see its definition), a ColorChooser (see its definition), // and a "clear" button. // class Scribble extends CloseableFrame { private ScribblePad pad ; Scribble() { super("Scribble") ; setLayout(new BorderLayout()) ; Panel controlP = new Panel() ; ColorChooser colorC = new ColorChooser() ; controlP.add(colorC) ; Button clearB = new Button("clear") ; controlP.add(clearB) ; pad = new ScribblePad(400, colorC) ; add(pad, "Center") ; add(controlP, "South") ; clearB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pad.clear() ; } } ) ; pack() ; show() ; } // ---- inner classes ------------------------------------------ // // a ScribblePad object is a Canvas on which we can "scribble" // with the mouse. // to "scribble", the user presses the left mouse button // and drags the mouse around the drawing area. drawing // continues until the mouse is released. the color of // the "scribble" is determined by the state of a // ColorChooser object (see its definition). // results accumulate (i.e., one can scribble in many // colors) until a "clear" is performed (when another // enclosing object invokes our clear() method). // class ScribblePad extends Canvas implements MouseListener, MouseMotionListener { // offscreen image and its associated graphics // context. private Image padImage ; private Graphics padGC ; // coordinates of previous mouse position. private int startX, startY ; private ColorChooser colorC ; ScribblePad(int size, ColorChooser c) { super() ; colorC = c ; setSize(new Dimension(size, size)) ; setBackground(Color.white) ; addMouseListener(this) ; addMouseMotionListener(this) ; } // responds to mouse-press events -- sets color // and starts "drawing". public void mousePressed(MouseEvent e) { startX = e.getX() ; startY = e.getY() ; padGC.setColor(colorC.chosenColor()) ; } // responds to mouse-drag events -- draws a line // from previous mouse position to current // position, and resets current position. // as these lines accumulate in the offscreen // image, the effect is a "scribble" tracking // mouse motion. public void mouseDragged(MouseEvent e) { int endX = e.getX() ; int endY = e.getY() ; padGC.drawLine(startX, startY, endX, endY) ; startX = endX ; startY = endY ; repaint() ; } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } // clears drawing area (by clearing offscreen // image and requesting repaint). private void clear() { padGC.clearRect(0, 0, getSize().width, getSize().height) ; repaint() ; } // updates onscreen image. public void update(Graphics g) { paint(g) ; } // creates onscreen image. public void paint(Graphics g) { if (padImage == null) { padImage = createImage(getSize().width, getSize().height) ; padGC = padImage.getGraphics() ; } g.drawImage(padImage, 0, 0, null) ; } } // ---- main --------------------------------------------------- // // no command-line arguments. // public static void main(String[] args) { Scribble r = new Scribble() ; } } // // a ColorChooser object is a Choice whose items represent the // predefined colors of the Color class. it also provides // a chosenColor() method to return the selected color. // class ColorChooser extends Choice { private static final ColorChoice[] choices = { new ColorChoice(Color.black, "black"), new ColorChoice(Color.blue, "blue"), new ColorChoice(Color.cyan, "cyan"), new ColorChoice(Color.darkGray, "darkGray"), new ColorChoice(Color.green, "green"), new ColorChoice(Color.lightGray, "lightGray"), new ColorChoice(Color.magenta, "magenta"), new ColorChoice(Color.orange, "orange"), new ColorChoice(Color.pink, "pink"), new ColorChoice(Color.red, "red"), new ColorChoice(Color.white, "white"), new ColorChoice(Color.yellow, "yellow") } ; ColorChooser() { super() ; for (int i = 0 ; i < choices.length ; i++) add(choices[i].name) ; } Color chosenColor() { return choices[getSelectedIndex()].color ; } // ---- inner classes ------------------------------------------ // a ColorChoice object pairs one of the predefined colors of // the Color class with its (text) name. static class ColorChoice { Color color ; String name ; ColorChoice(Color c, String n) { color = c ; name = n ; } } }