Number2.java
1    /* 
2     * Program to paint a rectangle, centered in the canvas, made up of randomly 
3     * colored, black framed squares, but this time they are spaced evenly. 
4     */
5    
6    package npw;
7    
8    import painter.SPainter;
9    import shapes.SSquare;
10   
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Random;
14   import java.util.Scanner;
15   
16   public class Number2 {
17       // The solution to the graphical problem.
18       private void paintTheImage() {
19           // Get the input information fron the user.
20           int nrOfRows = getNumber("rows");
21           int nrOfColumns = getNumber("columns");
22           int sizeOfSquare = getNumber("square side length");
23           // Establish the painter.
24           int height = nrOfRows * sizeOfSquare;
25           int width = nrOfColumns * sizeOfSquare;
26           SPainter miro = new SPainter("Number 1", width + 50, height + 50);
27           miro.setBrushWidth(4);
28           SSquare square = new SSquare(sizeOfSquare);
29           // Paint the rectangles.
30           paintRectangle(miro, square, nrOfRows, nrOfColumns);
31       }
32   
33       private static int getNumber(String prompt) {
34           String nss = JOptionPane.showInputDialog(null, prompt + "?");
35           Scanner scanner = new Scanner(nss);
36           return scanner.nextInt();
37       }
38   
39       private static void paintRectangle(SPainter miro, SSquare square,
40                                          int nrOfRows, int nrOfColumns) {
41           // Position the painter to paint the rectangle of squares.
42           miro.mlt(((nrOfColumns - 1) / 2.0) * square.side());
43           miro.mbk(((nrOfRows - 1) / 2.0) * square.side());
44           // Paint the rectangle of squares.
45           int i = 1;
46           while (i <= nrOfRows) {
47               paintOneRow(miro, nrOfColumns, square);
48               miro.mfd(square.side());
49               i = i + 1;
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   
66       private static void paintOneSquare(SPainter miro, SSquare square) {
67           miro.setColor(randomColor());
68           square.shrink(square.side()/2);
69           miro.paint(square);
70           miro.setColor(Color.BLACK);
71           miro.draw(square);
72           square.expand(square.side());
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   }
96