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