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