// File: BezierCanvas.java import java.awt.*; /** Draws the curves and processes the mouse input. **/ public class BezierCanvas extends Canvas { protected State state; protected CurveManager curveManager; protected PointManager pointManager; protected Point[] temporaryCurve = new Point[4]; protected int temporaryLength = 0; public BezierCanvas(State state, CurveManager curveManager, PointManager pointManager) { this.state = state; this.curveManager = curveManager; this.pointManager = pointManager; } public boolean mouseDown(Event e, int x, int y) { Point p = new Point(x,y); if (state.getState()==State.PASSIVE) { if (pointManager.isControlPoint(p)) { ControlPoint cp = pointManager.getControlPoint(p); pointManager.activatePoint(cp); state.setState(State.EDITING); } } else if (state.getState()==State.CREATING) { temporaryCurve[temporaryLength] = p; temporaryLength++; repaint(); } return true; } public boolean mouseUp(Event e, int x, int y) { if (state.getState()==State.CREATING) { if (temporaryLength==4) { Point[] p = temporaryCurve; curveManager.addCurve(p[0],p[1],p[2],p[3]); temporaryLength = 0; state.setState(State.PASSIVE); repaint(); } } else if (state.getState()==State.EDITING) { state.setState(State.PASSIVE); } return true; } public boolean mouseDrag(Event e, int x, int y) { int s = state.getState(); if (s==State.EDITING) { pointManager.setActivePoint(new Point(x,y)); repaint(); } else if (s==State.CREATING) { temporaryCurve[temporaryLength-1]=new Point(x,y); repaint(); } return true; } public void update(Graphics g) { paint(g); } /** Possible improvement: Separate static and dynamic layer; only update curves bein edited and store the rest as an Image. **/ public void paint(Graphics g) { Image offscreen = createImage(size().width, size().height); Graphics og = offscreen.getGraphics(); // Draw all the curves curveManager.paint(og,state.getSamples()); // If a curve is under creation, draw a simple approximation of it: if (temporaryLength != 0) { Point[] p = temporaryCurve; og.setColor(Color.red); for (int i=1; i