// mas964 ps4 // Take two clicks and draw them connected somehow // this applet prefers to be 354x354 import java.applet.*; import java.awt.*; import java.lang.Math; import DoubleBufferApplet; public class TwoPointStatic extends DoubleBufferApplet { int x1, x2, y1, y2; int numPoints = 0; int numCircles, maxDimension, spacing, circleRadius; public void drawImage() { Graphics g = imageBuffer.getGraphics(); this.clearImageBuffer(); g.setColor(this.getForeground()); maxDimension = Math.max(imageBufferSize.width, imageBufferSize.height); numCircles = 16; spacing = maxDimension / numCircles; // should divide size circleRadius = 9; // draw a grid of circles int x, y; if (true) for (x = 0; x < numCircles; x++) for (y = 0; y < numCircles; y++) drawOpen(g, x, y); // find the nearest places to where was clicked ("logical coordinates") int logX1, logY1, logX2, logY2; logX1 = x1 / spacing; logY1 = y1 / spacing; logX2 = x2 / spacing; logY2 = y2 / spacing; // draw two clicked on points filled. drawFill(g, logX1, logY1); drawFill(g, logX2, logY2); // now draw a line that connects the two points in pieces // we are trying to draw from logX1, logY1 to logX2, logY2 // now draw vertically from first - but overshoot int overY; if (logY2 > logY1) { overY = logY2 + (numCircles - logY2) / 2; for (y = logY1; y <= overY; y++) drawFill(g, logX1, y); } else { overY = logY2 / 2; for (y = logY1; y >= overY; y--) drawFill(g, logX1, y); } // now draw horizontally from second - but overshoot int overX; if (logX2 > logX1) { overX = logX2 + (numCircles - logX2) / 2; for (x = logX1; x <= overX; x++) drawFill(g, x, overY); } else { overX = logX2/2; for (x = logX1; x >= overX; x--) drawFill(g, x, overY); } // now draw back up to the right level if (logY2 > overY) for (y = overY; y <= logY2; y++) drawFill(g, overX, y); else for (y = overY; y >= logY2; y--) drawFill(g, overX, y); // and come back in if (logX2 > overX) for (x = overX; x <= logX2; x++) drawFill(g, x, logY2); else for (x = overX; x >= logX2; x--) drawFill(g, x, logY2); repaint(); } public void drawOpen(Graphics g, int x, int y) { thickCenteredCircle(g, x * spacing + spacing/2, y * spacing + spacing/2, circleRadius, 4); } public void drawFill(Graphics g, int x, int y) { thickCenteredCircle(g, x * spacing + spacing/2, y * spacing + spacing/2, circleRadius, -1); } // draw a circle of given thickness, etc. If thickness is 0 or radius, // then just draw a filled circle public void thickCenteredCircle(Graphics g, int x, int y, int radius, int thickness) { Color c = g.getColor(); g.fillOval(x - radius, y - radius, radius * 2 + 1, radius * 2 + 1); if (thickness > 0 && thickness < radius) { g.setColor(this.getBackground()); g.fillOval(x - (radius - thickness), y - (radius - thickness), (radius - thickness) * 2 + 1, (radius - thickness) * 2 + 1); g.setColor(c); } } // collect three two clicks public boolean mouseDown(Event e, int mouseX, int mouseY) { if (numPoints == 0) { x1 = mouseX; y1 = mouseY; numPoints++; } else { x2 = mouseX; y2 = mouseY; drawImage(); numPoints = 0; } return true; } public void init() { this.setBackground(Color.white); this.setForeground(Color.black); super.init(); update(getGraphics()); x1 = 100; y1 = 100; x2 = 200; y2 = 200; drawImage(); numPoints = 0; } }