// mas964 ps2 // draw circles on mouse click that demonstrate repetition import java.applet.*; import java.awt.*; import java.lang.Math; import DoubleBufferApplet; public class CircleRepetition extends DoubleBufferApplet { public boolean mouseDrag(Event e, int x, int y) { return mouseDown(e, x, y); } public boolean mouseDown(Event e, int x, int y) { Graphics g = imageBuffer.getGraphics(); this.clearImageBuffer(); g.setColor(this.getForeground()); int maxNumCircles = 30; int circleSizeGradations = 10; int numCircles; int circleSize; float cellSize; // y coordinate controls the number of circles drawn numCircles = maxNumCircles * y / imageBufferSize.height + 2; if (imageBufferSize.width > imageBufferSize.height) cellSize = imageBufferSize.width / numCircles; else cellSize = imageBufferSize.height / numCircles; // x coordinate controls how big the circles are circleSize = (int)(cellSize * x / imageBufferSize.width) + 1; if (circleSize < 2) circleSize = 2; // System.out.println("numCircles " + numCircles + " circleSize " + circleSize); // now draw circles in a hexagonal pattern (some slop on both sides // to handle roundoff error) int i, j; for (i = -1; i < numCircles+4; i++) { for (j = -1; j < numCircles+4; j++) { if (i % 2 == 0) g.fillOval(Math.round(i * cellSize), Math.round(j * cellSize), circleSize, circleSize); else g.fillOval(Math.round(i * cellSize), Math.round(j * cellSize + cellSize/2), circleSize, circleSize); } } repaint(); return true; } public void init() { this.setBackground(Color.white); this.setForeground(Color.black); super.init(); } }