import java.awt.*; /** Canvas that handles painting and interaction with a Polygontri */ public class PolygonCanvas extends Canvas { PolygonTri p; int startVertex = -1; Graphics xorg; int n; public PolygonCanvas() { } public void setPolygon(PolygonTri p) { if (xorg == null) { xorg = getGraphics().create(); xorg.setXORMode(Color.red); } this.p = p; repaint(); } public void paint(Graphics g) { p.setSize(size()); p.paint(getGraphics()); } int lastx; int lasty; public boolean mouseDown(Event e, int x, int y) { requestFocus(); startVertex = p.pick(x,y); if (startVertex != -1) { Point pt = p.getPoint(startVertex); xorg.drawLine(pt.x, pt.y, x, y); lastx = x; lasty = y; } return true; } public boolean mouseDrag(Event e, int x, int y) { if (startVertex != -1) { Point pt = p.getPoint(startVertex); xorg.drawLine(pt.x, pt.y, lastx, lasty); lastx = x; lasty = y; xorg.drawLine(pt.x, pt.y, x, y); } return true; } public boolean mouseUp(Event e, int x, int y) { if (startVertex != -1) { Point pt = p.getPoint(startVertex); xorg.drawLine(pt.x, pt.y, lastx, lasty); int endVertex = p.pick(x,y); if (endVertex != -1) { p.addEdgeDeleteCross(startVertex, endVertex); repaint(); } } return true; } public boolean MouseEnter(Event e, int x, int y) { requestFocus(); return false; } public boolean keyDown(Event e, int key) { char c = (char) key; if ("cio".indexOf(c) >= 0) { if ((char)key == 'i') { p.zoomIn(e.x,e.y); } else if ((char)key == 'o') { p.zoomOut(e.x,e.y); } else if ((char)key == 'c') { p.centreView(e.x,e.y); } repaint(); } return true; } }