Number3.java
1    /* 
2     * Program to paint a rectangle, centered on the canvas, made up of randomly 
3     * colored circle, with white space in between them 
4     */
5    
6    package npw;
7    
8    import painter.SPainter;
9    import shapes.SCircle;
10   
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Random;
14   import java.util.Scanner;
15   
16   public class Number3 {
17       //the solution to the graphical problem
18       private void paintTheImage() {
19           //get the input information from the user
20           int nrOfRows = getNumber("rows");
21           int nrOfColumns = getNumber("columns");
22           int sizeOfCircle = getNumber("circle radius length");
23           //establish the painter
24           int height = (2 * nrOfRows * sizeOfCircle);
25           int width = (2 * nrOfColumns * sizeOfCircle);
26           SPainter miro = new SPainter("Number3",width + 50,height + 50);
27           miro.setBrushWidth(4);
28           SCircle circle = new SCircle(sizeOfCircle);
29           //paint the rectangles
30           paintTheRectangle(miro,circle,nrOfRows,nrOfColumns);
31       }
32   
33       private 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 void paintTheRectangle(SPainter miro, SCircle circle, int nrOfRows, int nrOfColumns) {
40           //position the painter to paint the rectangle of squares
41           miro.mlt(((nrOfColumns-1)/2.0) * 2 * circle.radius());
42           miro.mbk(((nrOfRows-1)/2.0) * 2 * circle.radius());
43           //paint the rectangle of squares
44           int i = 1;
45           while (i <= nrOfRows) {
46               paintOneRow(miro,nrOfColumns,circle);
47               miro.mfd(2 * circle.radius());
48               i = i+1;
49           }
50           //make the method invariant with respect to painter position
51           miro.mrt(((nrOfColumns-1)/2.0) * 2 * circle.radius());
52           miro.mfd(((nrOfRows-1)/2.0) * 2 * circle.radius());
53       }
54   
55       private void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) {
56           int k = 1;
57           while (k <= nrOfColumns) {
58               paintOneSquare(miro,circle);
59               miro.mrt(2*circle.radius());
60               k = k+1;
61           }
62           //make the method invariant with respect to painter position
63           miro.mlt(2*nrOfColumns*circle.radius());
64       }
65   
66       private void paintOneSquare(SPainter miro, SCircle circle) {
67           circle.s2();
68           miro.setColor(randomColor());
69           miro.paint(circle);
70           circle.x2();
71       }
72   
73       private Color randomColor() {
74           Random rgen = new Random();
75           int r = rgen.nextInt(256);
76           int g = rgen.nextInt(256);
77           int b = rgen.nextInt(256);
78           return new Color(r,b,g);
79       }
80   
81       public Number3() {
82           paintTheImage();
83       }
84       public static void main(String[] args) {
85           SwingUtilities.invokeLater(new Runnable() {
86               @Override
87               public void run() {
88                   new Number3();
89               }
90           });
91       }
92   
93   }
94