import java.applet.*; import java.awt.*; import java.lang.*; import java.util.Vector; public class VectorTest extends Applet { DrawingRegion region; public void init() { setBackground(Color.lightGray); setLayout(new BorderLayout()); DrawingRegion region = new DrawingRegion(); add("Center", region); add("South", new myCustomButtons(region)); /* region = new DrawingRegion(); add(region); add(new myCustomButton(region, "Clear")); add(new myCustomButton(region, "Clear All")); add(new myCustomButton(region, "Add")); */ } } class DrawingRegion extends Canvas { /* public DrawingRegion() { resize(480,400); } */ public int gNvectors = 0; public static final int x0 = 200; public static final int y0 = 200; public Vector gPoints = new Vector(); Color gColors[] = {Color.blue, Color.red}; int x1,y1; int x2,y2; int xl, yl; boolean in = false; Font boldFont = new Font("Helvetica", Font.BOLD, 10); Font plainFont = new Font("Helvetica", Font.PLAIN, 10); Font numFont = new Font("Symbol", Font.PLAIN, 12); public boolean handleEvent(Event e) { switch (e.id) { case Event.MOUSE_DOWN: if(gNvectors<2) if(e.x<300 && e.y <300 && e.x > 100 && e.y >100) { Graphics g = getGraphics(); x1 = x0; y1 = y0; x2 = e.x; y2 = e.y; g.setColor(Color.black); g.setPaintMode(); drawVector(g, x0, y0, e.x, e.y); } return true; case Event.MOUSE_UP: if(gNvectors<2) { Graphics g = getGraphics(); if(e.x<300 && e.y <300 && e.x > 100 && e.y >100) { g.setColor(gColors[gNvectors]); gPoints.addElement(new Point(e.x,e.y)); drawVector(g, x0, y0, e.x, e.y); writeCoords(g, gNvectors, e.x, e.y); gNvectors++; } else if(in == true) { in = false; repaint(); } } return true; case Event.MOUSE_DRAG: if(gNvectors<2) { if(e.x<300 && e.y <300 && e.x > 100 && e.y >100) { Graphics g = getGraphics(); in = true; xl = x2; yl = y2; x2 = e.x; y2 = e.y; g.setColor(Color.black); g.setXORMode(Color.white); drawVector(g, x0, y0, xl, yl); g.setPaintMode(); g.setColor(Color.white); writeCoords(g,gNvectors, xl,yl); g.setColor(gColors[gNvectors]); writeCoords(g,gNvectors, x2,y2); g.setColor(Color.black); g.setXORMode(Color.white); drawVector(g, x0, y0, x2, y2); } } return true; case Event.WINDOW_DESTROY: System.exit(0); return true; default: return false; } } public void drawVector(Graphics g, int x1, int y1, int x2, int y2) { double theta; long x, y; theta = Math.atan2(y1-y2,x2-x1); g.drawLine(x1, y1, x2, y2); x = Math.round(6*Math.cos(theta+Math.PI/10.)); y = Math.round(6*Math.sin(theta+Math.PI/10.)); g.drawLine(x2, y2, x2-(int)x, y2+(int)y); x = Math.round(6*Math.cos(theta-Math.PI/10.)); y = Math.round(6*Math.sin(theta-Math.PI/10.)); g.drawLine(x2, y2, x2-(int)x, y2+(int)y); } public void paint(Graphics g) { g.setPaintMode(); g.setColor(Color.white); g.fillRect(0,0,400,400); g.setColor(Color.black); g.drawRect(0,0,400,400); drawGrid(g); drawText(g); for (int i = 0; ix0) theta = -theta; else theta = theta-180; } else if(x30) { String choice = (String)label; if(choice.equals("Clear")) { target.clear(); target.repaint(); } else if(choice.equals("Clear All")) { while(target.gNvectors>0) target.clear(); target.repaint(); } else if(choice.equals("Add")) { if(target.gNvectors == 2) { target.gNvectors +=1; target.drawSum(); } } } return true; } }