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