// mas964 ps4 // Take three clicks and draw them connected somehow import java.applet.*; import java.awt.*; import java.lang.Math; import DoubleBufferApplet; public class ThreePointStatic extends DoubleBufferApplet { int x[], y[]; int numPoints; public void drawImage() { Graphics g = imageBuffer.getGraphics(); this.clearImageBuffer(); g.setColor(this.getForeground()); if (numPoints != 3) System.out.println("drawImage called without 3 points set."); if (false) { int i; for (i = 0; i < 3; i++) g.fillRect(x[i]-2, y[i]-2, 5, 5); } int d0, d1, a0, a1, maxd, r; // figure out angle and distance from last point to first two d0 = (int)distance(x[2], y[2], x[0], y[0]); a0 = (int)angle(x[2], y[2], x[0], y[0]); d1 = (int)distance(x[2], y[2], x[1], y[1]); a1 = (int)angle(x[2], y[2], x[1], y[1]); // tunable parameters. int ringSpacing = 6, ringThickness = 3, arcLength = 24; double randomArcProbability = 0.7; maxd = Math.max(imageBufferSize.width, imageBufferSize.height); maxd += maxd % (ringSpacing); // round up to nearest for (r = maxd; r > 0; r -= ringSpacing) { // draw rings from outside in if (r <= d0) thickCenteredArc(g, x[2], y[2], r, ringThickness, a0 - arcLength/2, arcLength); if (r <= d1) thickCenteredArc(g, x[2], y[2], r, ringThickness, a1 - arcLength / 2, arcLength); if (Math.random() < randomArcProbability) { int randomAngle = (int) (Math.random() * 360); thickCenteredArc(g, x[2], y[2], r, ringThickness, randomAngle - arcLength/2, arcLength); } } // wedge in the middle thickCenteredArc(g, x[2], y[2], ringSpacing, ringSpacing, a0 - arcLength / 2, arcLength); thickCenteredArc(g, x[2], y[2], ringSpacing, ringSpacing, a1 - arcLength / 2, arcLength); int m; m = (y[0] - y[2]) / (x[2] - x[0]); // g.drawLine(x[2], y[2], x[0], y[0]); // g.drawLine(x[2], y[2], x[1], y[1]); repaint(); } // simple interface to drawing arcs that handles centering, radius, thickness // assumes circular arcs. Must be drawn from the outside in. public void thickCenteredArc(Graphics g, int x, int y, int radius, int thickness, int sa, int ea) { Color c = g.getColor(); g.fillArc(x - radius, y - radius, radius*2, radius*2, sa, ea); g.setColor(this.getBackground()); // fudge the arc a bit wider - necessary to handle edges... g.fillArc(x - (radius - thickness), y - (radius - thickness), (radius-thickness)*2, (radius-thickness)*2, sa - 1, ea + 2); g.setColor(c); } public double distance(int x1, int y1, int x2, int y2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); } // convert the angle to degrees in the format requred by Graphics.drawArc public double angle(int x1, int y1, int x2, int y2) { double radians = Math.atan2(x1 - x2, y1 - y2); return (radians * 180 / Math.PI) + 90; } // collect three mouse clicks public boolean mouseDown(Event e, int mouseX, int mouseY) { x[numPoints] = mouseX; y[numPoints] = mouseY; numPoints++; if (numPoints == 3) { drawImage(); numPoints = 0; } return true; } public void init() { x = new int[3]; y = new int[3]; this.setBackground(Color.white); this.setForeground(Color.black); update(getGraphics()); super.init(); x[0] = 40; y[0] = 150; x[1] = 270; y[1] = 290; x[2] = 150; y[2] = 50; numPoints = 3; drawImage(); numPoints = 0; } }