Number4.java
1    /* 
2     * Program to paint a rectangle, centered on the canvas, made up of 
3     * colored circles, with white space in between them. 
4     * The number of rows, columns, radius of the circles, and colors 
5     * are all taken from the standard input stream. 
6     * The recognized inputs for color are 
7     *  - RED 
8     *  - GREEN 
9     *  - BLUE 
10    * any other input will output black circles. 
11    */
12   
13   package npw;
14   
15   import painter.SPainter;
16   import shapes.SCircle;
17   
18   import javax.swing.*;
19   import java.awt.*;
20   import java.util.Scanner;
21   
22   public class Number4 {
23       //the solution to the graphical problem
24       private void paintTheImage() {
25           //get the input information from the user
26           int nrOfRows = getNumber("rows");
27           int nrOfColumns = getNumber("columns");
28           int sizeOfCircle = getNumber("circle radius length");
29           String colorOfCirlce = getColor("circle colors");
30           //establish the painter
31           int height = (nrOfRows * 2 * sizeOfCircle);
32           int width = (nrOfColumns * 2 * sizeOfCircle);
33           SPainter miro = new SPainter("Number4",width + 50,height + 50);
34           miro.setBrushWidth(4);
35           SCircle circle = new SCircle(sizeOfCircle);
36           //paint the rectangles
37           paintTheRectangle(miro,circle,nrOfRows,nrOfColumns,colorOfCirlce);
38       }
39   
40       private int getNumber(String prompt) {
41           String nss = JOptionPane.showInputDialog(null,prompt+"?");
42           Scanner scanner = new Scanner(nss);
43           return scanner.nextInt();
44       }
45       private String getColor(String prompt) {
46           String strong = JOptionPane.showInputDialog(null,prompt+"?");
47           Scanner scunner = new Scanner(strong);
48           return scunner.next();
49       }
50   
51       private void paintTheRectangle(SPainter miro, SCircle circle, int nrOfRows,
52                                      int nrOfColumns, String colorOfCircle) {
53           //position the painter to paint the rectangle of squares
54           miro.mlt(((nrOfColumns-1)/2.0) * 2 * circle.radius());
55           miro.mbk(((nrOfRows-1)/2.0) * 2 * circle.radius());
56           //paint the rectangle of squares
57           int i = 1;
58           while (i <= nrOfRows) {
59               paintOneRow(miro,nrOfColumns,circle,colorOfCircle);
60               miro.mfd(2*circle.radius());
61               i = i+1;
62           }
63           //make the method invariant with respect to painter position
64           miro.mrt(((nrOfColumns-1)/2.0) * 2 * circle.radius());
65           miro.mfd(((nrOfRows-1)/2.0) * 2 * circle.radius());
66       }
67   
68       private void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle, String colorOfCircle) {
69           int k = 1;
70           while (k <= nrOfColumns) {
71               paintOneSquare(miro,circle,colorOfCircle);
72               miro.mrt(2* circle.radius());
73               k = k+1;
74           }
75           //make the method invariant with respect to painter position
76           miro.mlt(nrOfColumns* 2 * circle.radius());
77       }
78   
79       private void paintOneSquare(SPainter miro, SCircle circle, String colorOfCircle) {
80           circle.s2();
81           miro.setColor(miroColor(colorOfCircle));
82           miro.paint(circle);
83           circle.x2();
84       }
85   
86       private Color miroColor(String colorOfCircle) {
87           if (colorOfCircle.equalsIgnoreCase("blue")) {
88               return Color.BLUE;
89           }
90           if (colorOfCircle.equalsIgnoreCase("red")) {
91               return Color.RED;
92           }
93           if (colorOfCircle.equalsIgnoreCase("green")) {
94               return Color.GREEN;
95           }
96           else {
97               return Color.BLACK;
98           }
99       }
100  
101  
102      public Number4() {
103          paintTheImage();
104      }
105      public static void main(String[] args) {
106          SwingUtilities.invokeLater(new Runnable() {
107              @Override
108              public void run() {
109                  new Number4();
110              }
111          });
112      }
113  
114  }
115