/* * A program to paint, centered on the canvas, a circle of single colored, non-framed circles. */ package npw; import painter.SPainter; import shapes.SCircle; import javax.swing.*; import java.awt.*; import java.util.Scanner; public class SimpleDots { private void paintTheImage(){ // Get the input information int radius = getNumber("circle radius"); int radius2 = getNumber("circles radius"); // Establish the painter SPainter painter = new SPainter("Simple Dots", radius*2+50, radius*2+50); painter.setColor(randomColor()); painter.setBrushWidth(3); SCircle circle = new SCircle(radius); SCircle circle2 = new SCircle(radius2); // Paint the circles paintCircleOfCircles(painter, circle, circle2); } private void paintCircleOfCircles(SPainter painter, SCircle circle, SCircle circle2){ // Position the painter to begin drawing the rows painter.mfd(circle.radius()); painter.tr(); // Paint the circle of squares double moved = 0; while (moved < circle.diameter()) { double chord = chordLength(circle.radius() - moved, circle); int circles = squaresOnLineCount(chord, circle2.radius()); paintRow(painter, circle2, circles); nextRow(painter, circle2.radius()); moved = moved + circle2.radius(); } // Make the method invariant with respect to painter position painter.tl(); painter.mfd(circle.radius()); } // Move to the next row private void nextRow(SPainter painter, double rowHeight){ painter.tr(); painter.mfd(rowHeight); painter.tl(); } // Assumes the painter is at the center of the row to paint, facing right. private void paintRow(SPainter painter, SCircle circle2, int circlesToPaint){ // Move backward 1/2 of the length we're painting to get ready to paint the row. double centerOffset = ( (circlesToPaint * circle2.radius()) / 2) - circle2.radius()/2; painter.mbk(centerOffset); // Paint the row of squares. int painted = 0; while (painted < circlesToPaint){ paintOneCircle(painter, circle2); painter.mfd(circle2.radius()); painted = painted + 1; } // Make the method invariant with respect to painter position. painter.mbk(centerOffset + circle2.radius()); } private void paintOneCircle(SPainter painter, SCircle circle2){ circle2.s5(); painter.paint(circle2); // REMOVED BLACK OUTLINE painter.draw(circle2); circle2.x5(); } private static int squaresOnLineCount(double lineLength, double sideLength){ int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); return circles; } private double chordLength(double yOffset, SCircle circle){ double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); double chordLength = xLength * 2; return chordLength; } private static int getNumber(String prompt) { String nss = JOptionPane.showInputDialog(null,prompt+"?"); Scanner scanner = new Scanner(nss); return scanner.nextInt(); } private static Color randomColor() { while (true) { String command = JOptionPane.showInputDialog(null, "Command? Red, Green, or Blue"); if (command == null) { command = "exit"; } // user clicked on Cancel if (command.equalsIgnoreCase("blue")) { return Color.blue; } else if (command.equalsIgnoreCase("red")) { return Color.red; } else if (command.equalsIgnoreCase("green")) { return Color.green; } else { return Color.black; } } } public SimpleDots() { paintTheImage(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SimpleDots(); } }); } }