Number4.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.Objects;
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   
18   public class Number4 {
19       // The solution to the graphical problem.
20       private void paintTheImage() {
21           // Get the input information from the user.
22           int nrOfRows = getNumber("rows");
23           int nrOfColumns = getNumber("columns");
24           int sizeOfDiameter = getNumber("circle diameter");
25           Color color = getColor("color");
26   
27           // Establish the painter.
28           int height = nrOfRows * sizeOfDiameter;
29           int width = nrOfColumns * sizeOfDiameter;
30           SPainter miro = new SPainter("Number 3",width+50,height+50);
31           miro.setColor(color);
32           miro.setBrushWidth(4);
33           SCircle circle = new SCircle(sizeOfDiameter / 2.0);
34           // Paint the rectangles.
35           paintRectangle(miro,circle,nrOfRows,nrOfColumns);
36       }
37   
38       private static Color getColor(String prompt) {
39           String nss = JOptionPane.showInputDialog(null,prompt+"?");
40           Scanner scanner = new Scanner(nss);
41           String input = scanner.next();
42           Color color;
43   
44           if(Objects.equals(input, "red")){
45               color = Color.RED;
46           }else if(Objects.equals(input, "blue")){
47               color = Color.BLUE;
48           }else if(Objects.equals(input, "green")){
49               color = Color.GREEN;
50           }else{
51               color = Color.BLACK;
52           }
53           return color;
54       }
55   
56       private static int getNumber(String prompt) {
57           String nss = JOptionPane.showInputDialog(null,prompt+"?");
58           Scanner scanner = new Scanner(nss);
59           return scanner.nextInt();
60       }
61   
62       private static void paintRectangle(SPainter miro, SCircle circle,
63                                          int nrOfRows, int nrOfColumns) {
64           // Position the painter to paint the rectangle of circles.
65           miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter());
66           miro.mbk(((nrOfRows-1)/2.0) * circle.diameter());
67           // Paint the rectangle of circles.
68           int i = 1;
69           while ( i <= nrOfRows) {
70               paintOneRow(miro,nrOfColumns,circle);
71               miro.mfd(circle.diameter());
72               i = i + 1;
73           }
74           // Make the method invariant with respect to painter position.
75           miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter());
76           miro.mfd(((nrOfRows-1)/2.0) * circle.diameter());
77       }
78   
79       private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) {
80           int i = 1;
81           while ( i <= nrOfColumns ) {
82               paintOneCircle(miro,circle);
83               miro.mrt(circle.diameter());
84               i = i + 1;
85           }
86           miro.mlt(nrOfColumns*circle.diameter());
87       }
88   
89       private static void paintOneCircle(SPainter miro, SCircle circle) {
90           circle.s2();
91           miro.paint(circle);
92           circle.x2();
93       }
94   
95       public Number4(){
96           paintTheImage();
97       }
98   
99       public static void main(String[] args){
100          SwingUtilities.invokeLater(new Runnable() {
101              @Override
102              public void run() {
103                  new Number4();
104              }
105          });
106      }
107  }