import java.applet.*; import java.awt.*; import java.util.Vector; public class Drawing extends Applet { static DrawPanel dp; public void init() { setLayout(new BorderLayout()); add("Center", dp = new DrawPanel()); add("South", new Controls(dp)); } public void start() { dp.start(); } // start the dp=DrawPanel ie run() it public void stop() { dp.stop(); } // kill the dp=DrawPanel thread public static void main(String args[]) // only for stand-alone program { Frame f = new Frame("Drawing"); Drawing scr = new Drawing(); scr.init(); scr.start(); dp.start(); dp.run(); f.add("Center", scr); f.show(); } } //---------------------------------------------------------------------------- class Curve { public boolean closed; public Vector points; //of class Point (see awt) public Curve(boolean closed, int x0, int y0) { this.closed = closed; points = new Vector(); points.addElement(new Point(x0, y0)); } } //----------------------------------------------------------------------------- class DrawPanel extends Panel implements Runnable // the drawing area { private Thread kicker; private int last_x = 0, last_y = 0, x=0, y=0; private int pause = 100; private boolean isClosed = false; // ??? closed or open lines private Vector lines = new Vector(); // Vector of Curve public void faster() { pause = (pause*9)/10; if(pause < 10) pause=10; repaint(); } public void slower() { pause = (pause*10)/9; repaint(); } public void closedOpen() { isClosed = ! isClosed; repaint(); } public void clear () { for(int i = 0; i < lines.size(); i++) ((Curve)lines.elementAt(i)).points.removeAllElements(); lines.removeAllElements(); repaint(); } public void start() { kicker = new Thread(this); kicker.start(); //ie run() } public void run() { Graphics g = getGraphics(); while(true) { try{kicker.sleep(pause); } catch (Exception e) { break; } if((x != last_x) || (y != last_y)) { ((Curve)lines.lastElement()).points.addElement(new Point(x, y)); repaint(pause); // within this mSec last_x = x; last_y = y; } } } public void stop() { kicker.stop(); } public boolean mouseDown(Event e, int xPos, int yPos) { x = xPos; y = yPos; last_x = x; last_y = y; Curve c = new Curve(false, x, y); lines.addElement(c); return true; // indicate Event was handled } public boolean mouseDrag(Event e, int xPos, int yPos) { x = xPos; y = yPos; return true; // indicate Event was handled } public boolean mouseUp(Event e, int xPos, int yPos) { x = xPos; y = yPos; ((Curve)lines.lastElement()).closed = isClosed; repaint(); return true; // indicate Event was handled } public void paint(Graphics g) { Dimension d = size(); g.drawRect(0, 0, d.width-1, d.height-1); // an enclosing rectangle if(isClosed) g.drawString("(Closed)", d.width-80, d.height-2); else g.drawString("(Open)", d.width-80, d.height-2); g.drawString("Pause=" + Integer.toString(pause) + "msec", 2, d.height-2); int nLines = lines.size(); for(int i = 0; i < nLines; i++) // each Curve { Curve c = (Curve)lines.elementAt(i); int nPts = c.points.size(); Point p1 = (Point)c.points.elementAt(0); for (int j = 0; j < nPts; j++) // each Point { Point p2 = (Point)c.points.elementAt(j); g.drawLine( p1.x, p1.y, p2.x, p2.y ); g.drawLine( p2.x-1, p2.y, p2.x+1, p2.y ); // draw a g.drawLine( p2.x, p2.y-1, p2.x, p2.y+1 ); // small + p1 = p2; } if( c.closed ) { Point p2 = (Point)c.points.elementAt(0); g.drawLine(p1.x, p1.y, p2.x, p2.y); } } } // end paint() } //----------------------------------------------------------------------------- class Controls extends Panel // the control panel for faster, slower and clear { DrawPanel drawPanel; Button faster, slower, closedOpen, clear; public Controls(DrawPanel d) { drawPanel = d; setLayout(new FlowLayout()); add(faster = new Button("faster")); add(slower = new Button("slower")); add(closedOpen = new Button("closed/open")); add(clear = new Button("clear" )); } public boolean action(Event e, Object arg) { if( e.target == faster ) drawPanel.faster(); else if( e.target == slower ) drawPanel.slower(); else if( e.target == clear ) drawPanel.clear(); else if( e.target == closedOpen ) drawPanel.closedOpen(); return true; } } // L.Allison 1996 // Dept. Computer Science, Monash University, Australia 3168 // home page: http://www.cs.monash.edu.au/~lloyd/