Number2.java
1    /* 
2     *This program paints randomly colored rectangles, and spaces them out. 
3     * Like Number 1 you write in the number of rows and columns.  
4     */
5    package npw;
6    import java.awt.Color;
7    import java.util.Random;
8    import java.util.Scanner;
9    import javax.swing.JOptionPane;
10   import javax.swing.SwingUtilities;
11   import painter.SPainter;
12   import shapes.SSquare;
13   
14   public class Number2 {
15       //Solution to the graphical problem.
16       private void paintTheImage() {
17           //Get the input info from the user.
18           int numOfRows = getNumber("rows");
19           int numOfColumns = getNumber("columns");
20           int sizeOfSquare = getNumber("square side length");
21           //Establishing the painter.
22           int height = numOfRows * sizeOfSquare;
23           int width = numOfColumns * sizeOfSquare;
24           SPainter leo = new SPainter("Number 2",width+50,height+50);
25           leo.setBrushWidth(4);
26           SSquare square = new SSquare(sizeOfSquare);
27           //paint the rectangles
28           paintRectangle(leo,square,numOfRows,numOfColumns);
29       }
30   
31       private int getNumber(String prompt) {
32           String nss = JOptionPane.showInputDialog(null,prompt+"?");
33           Scanner scan = new Scanner(nss);
34           return scan.nextInt();
35       }
36       private void paintRectangle(SPainter leo, SSquare square, int numOfRows, int numOfColumns) {
37           //Position the painter to paint the rectangle of squares.
38           leo.mlt(((numOfColumns-1)/2.0)*square.side());
39           leo.mbk(((numOfRows-1)/2.0)*square.side());
40           //Paint the rectangle of squares.
41           int i = 1;
42           while (i <= numOfRows) {
43               paintOneRow(leo,numOfColumns,square);
44               leo.mfd(square.side());
45               i = i + 1;
46           }
47           //Used to make method invariant with respect to painter position.
48           leo.mrt(((numOfColumns-1)/2.0)*square.side());
49           leo.mfd(((numOfColumns-1)/2.0)*square.side());
50       }
51   
52       private void paintOneRow(SPainter leo, int numOfColumns, SSquare square) {
53           int i = 1;
54           while (i <= numOfColumns) {
55               paintOneSquare(leo,square);
56               leo.mrt(square.side());
57               i = i + 1;
58           }
59           //invariant
60           leo.mlt(numOfColumns*square.side());
61       }
62   
63       private void paintOneSquare(SPainter leo, SSquare square) {
64           square.resetSide((int) (square.side()/2.0));
65           //square is shrunk by 2
66           leo.paint(square);
67           leo.setColor(randomColor());
68           leo.paint(square);
69           leo.setColor(Color.BLACK);
70           leo.draw(square);
71           leo.setColor(Color.WHITE);
72           square.resetSide((int) (square.side()*2.0));
73           //square expands by 2, causing the painter to space the square that way.
74       }
75   
76       private Color randomColor() {
77           Random randomGenerator = new Random();
78           int r = randomGenerator.nextInt(256);
79           int g = randomGenerator.nextInt(256);
80           int b = randomGenerator.nextInt(256);
81           return new Color(r,b,g);
82       }
83       public Number2(){
84           paintTheImage();
85       }
86       public static  void main(String[] args){
87           SwingUtilities.invokeLater(new Runnable() {
88               @Override
89               public void run() {
90                   new Number2();
91               }
92           });
93       }
94   }
95