package npw; import painter.SPainter; import shapes.SCircle; import javax.swing.*; import java.awt.*; import java.util.Random; import java.util.Scanner; public class SimpleDots { public void paintTheImage() { int radius = getNumber("circle radius"); // create a new method: getnumber int dotradius = getNumber("dot radius"); String color = getColor(); SPainter painter = new SPainter("Circle of Squares", radius * 2 + 50, radius * 2 + 50); painter.setBrushWidth(3); SCircle circle = new SCircle(radius); SCircle dot = new SCircle(dotradius); if (color.equalsIgnoreCase("red")) { painter.setColor(Color.RED); } else if (color.equalsIgnoreCase("green")) { painter.setColor(Color.GREEN); } else if (color.equalsIgnoreCase("blue")) { painter.setColor(Color.BLUE); } else { painter.setColor(Color.BLACK); } paintCircleOfDot(painter, circle, dot);//create a new method: paintCircleOfSquare } private String getColor() { String color = JOptionPane.showInputDialog(null, "Color?"); return color; } private void paintCircleOfDot(SPainter painter, SCircle circle, SCircle dot) { painter.mfd(circle.radius()); painter.tr(); double moved = 0; while (moved < circle.diameter()) { double chord = chordLength(circle.radius() - moved, circle);//create a new method: chordLength int dots = dotOnLineCount(chord, dot.diameter());//create a new method squareOnLineCount paintRow(painter, dot, dots); // create a new method paintRow nextRow(painter, dot.diameter()); // create a new method nextRow moved = moved + dot.diameter(); } painter.tl(); painter.mfd(circle.radius()); } private void nextRow(SPainter painter, double rowHeight) { painter.tr(); painter.mfd(rowHeight); painter.tl(); } private void paintRow(SPainter painter, SCircle dot, int dotsToPaint) { double centerOffset = ((dotsToPaint * dot.diameter()) / 2) - dot.diameter() / 2; painter.mbk(centerOffset); int painted = 0; while (painted < dotsToPaint) { paintOneDot(painter, dot); painter.mfd(dot.diameter()); painted = painted + 1; } painter.mbk(centerOffset + dot.diameter()); } private void paintOneDot(SPainter painter, SCircle dot) { dot.s2(); painter.paint(dot); painter.draw(dot); dot.x2(); } private static int dotOnLineCount(double lineLength, double sideLength) { int dots = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1); return dots; } 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() { Random rgen = new Random(); int r = rgen.nextInt(256); int g = rgen.nextInt(256); int b = rgen.nextInt(256); return new Color(r, g, b); } public SimpleDots() { paintTheImage(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SimpleDots(); } }); } }