Number3.java
1    /* 
2     * Program to paint a rectangle, centered in the canvas, made up of randomly 
3     * colored, spaced circles. 
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.SCircle;
16   
17   public class Number3 {
18       // The solution to the graphical problem.
19       private void paintTheImage() {
20           // Get the input information fron the user.
21           int nrOfRows = getNumber("rows");
22           int nrOfColumns = getNumber("columns");
23           int radius = getNumber("radius");
24           // Establish the painter.
25           int height = nrOfRows * 2 * radius;
26           int width = nrOfColumns * 2 * radius;
27           SPainter miro = new SPainter("Number 3",width+50,height+50);
28           miro.setBrushWidth(4);
29           SCircle circle = new SCircle(radius);
30           // Paint the rectangles.
31           paintRectangle(miro,circle,nrOfRows,nrOfColumns);
32       }
33   
34       private static int getNumber(String prompt) {
35           String nss = JOptionPane.showInputDialog(null,prompt+"?");
36           Scanner scanner = new Scanner(nss);
37           return scanner.nextInt();
38       }
39   
40       private static void paintRectangle(SPainter miro, SCircle circle,
41                                          int nrOfRows, int nrOfColumns) {
42           // Position the painter to paint the rectangle of squares.
43           miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter());
44           miro.mbk(((nrOfRows-1)/2.0) * circle.diameter());
45           // Paint the rectangle of squares.
46           int i = 1;
47           while ( i <= nrOfRows) {
48               paintOneRow(miro,nrOfColumns,circle);
49               miro.mfd(circle.diameter());
50               i = i + 1;
51           }
52           // Make the method invariant with respect to painter position.
53           miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter());
54           miro.mfd(((nrOfRows-1)/2.0) * circle.diameter());
55       }
56   
57       private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) {
58           int i = 1;
59           while ( i <= nrOfColumns ) {
60               paintOneCircle(miro,circle);
61               miro.mrt(circle.diameter());
62               i = i + 1;
63           }
64           miro.mlt(nrOfColumns*circle.diameter());
65       }
66   
67       private static void paintOneCircle(SPainter miro, SCircle circle) {
68           miro.setColor(randomColor());
69           circle.s2();
70           miro.paint(circle);
71           circle.x2();
72       }
73       private static 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   
85       public static void main(String[] args){
86           SwingUtilities.invokeLater(new Runnable() {
87               @Override
88               public void run() {
89                   new Number3();
90               }
91           });
92       }
93   }
94