Number1.java
1    /* 
2    *Program to paint a rectangle, centered in the canvas, made up of randomly 
3    *colored, black framed squares. 
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 Number1 {
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 1",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            leo.setColor(randomColor());
65            leo.paint(square);
66            leo.setColor(Color.BLACK);
67            leo.draw(square);
68        }
69   
70        private Color randomColor() {
71            Random randomGenerator = new Random();
72            int r = randomGenerator.nextInt(256);
73            int g = randomGenerator.nextInt(256);
74            int b = randomGenerator.nextInt(256);
75            return new Color(r,b,g);
76        }
77        public Number1(){
78            paintTheImage();
79        }
80        public static  void main(String[] args){
81            SwingUtilities.invokeLater(new Runnable() {
82                @Override
83                public void run() {
84                    new Number1();
85                }
86            });
87        }
88    }
89