Number3.java
1    /* 
2     * Program to paint a rectangle, centered in the canvas, made up of randomly 
3     * colored circles that are spaced evenly. 
4     *//* 
5     * Program to paint a rectangle, centered in the canvas, made up of randomly 
6     * colored, black framed circles, but this time they are spaced evenly. 
7     */
8    
9    package npw;
10   
11   import painter.SPainter;
12   import shapes.SCircle;
13   
14   import javax.swing.*;
15   import java.awt.*;
16   import java.util.Random;
17   import java.util.Scanner;
18   
19   public class Number3 {
20       // The solution to the graphical problem.
21       private void paintTheImage() {
22           // Get the input information fron the user.
23           int nrOfRows = getNumber("rows");
24           int nrOfColumns = getNumber("columns");
25           int sizeOfSquare = getNumber("circle radius length");
26           // Establish the painter.
27           int height = (nrOfRows * sizeOfSquare);
28           int width = (nrOfColumns * sizeOfSquare);
29           SPainter miro = new SPainter("Number 1", (width *2)+ 50, (height * 2) + 50);
30           miro.setBrushWidth(4);
31           SCircle circle = new SCircle(sizeOfSquare);
32           // Paint the rectangles.
33           paintRectangle(miro, circle, nrOfRows, nrOfColumns);
34       }
35   
36       private static int getNumber(String prompt) {
37           String nss = JOptionPane.showInputDialog(null, prompt + "?");
38           Scanner scanner = new Scanner(nss);
39           return scanner.nextInt();
40       }
41   
42       private static void paintRectangle(SPainter miro, SCircle circle,
43                                          int nrOfRows, int nrOfColumns) {
44           // Position the painter to paint the rectangle of circles.
45           miro.mlt(((nrOfColumns - 1) / 2.0) * circle.radius() * 2); //changed to diameter shifted left
46           miro.mbk(((nrOfRows - 1) / 2.0) * circle.radius() * 2); //changed to diameter shifted it down
47           // Paint the rectangle of circles.
48           int i = 1;
49           while (i <= nrOfRows) {
50               paintOneRow(miro, nrOfColumns, circle);
51               miro.mfd(circle.diameter());
52               i = i + 1;
53           }
54           // Make the method invariant with respect to painter position.
55           miro.mrt(((nrOfColumns - 1) / 2.0) * circle.radius());
56           miro.mfd(((nrOfRows - 1) / 2.0) * circle.radius());
57       }
58   
59       private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) {
60           int i = 1;
61           while (i <= nrOfColumns) {
62               paintOneCircle(miro, circle);
63               miro.mrt(circle.diameter()); //moved over too much when * 1.5
64               i = i + 1;
65           }
66           miro.mlt(nrOfColumns * circle.diameter());
67       }
68   
69       private static void paintOneCircle(SPainter miro, SCircle circle) {
70           miro.setColor(randomColor());
71           circle.shrink(circle.radius()/2);
72           miro.paint(circle);
73           circle.expand(circle.radius());
74       }
75   
76       private static Color randomColor() {
77           Random rgen = new Random();
78           int r = rgen.nextInt(256);
79           int g = rgen.nextInt(256);
80           int b = rgen.nextInt(256);
81           return new Color(r, b, g);
82       }
83   
84       public Number3() {
85           paintTheImage();
86       }
87   
88       public static void main(String[] args) {
89           SwingUtilities.invokeLater(new Runnable() {
90               @Override
91               public void run() {
92                   new Number3();
93               }
94           });
95       }
96   }
97