Number4.java
1    /* 
2      //      * Program to paint a rectangle, centered in the canvas, made up of blue, red, green and black circles 
3     
4       //     */
5    
6    package npw;
7    
8    import java.awt.Color;
9    import java.util.Scanner;
10   import javax.swing.JOptionPane;
11   import javax.swing.SwingUtilities;
12   
13   import painter.SPainter;
14   import shapes.SCircle;
15   
16   public class Number4 {
17       // The solution to the graphical problem.
18       private void paintTheImage() {
19   
20           // Get the input information from the user. //CHANGED
21           int nrOfRows = getNumber("rows");
22           int nrOfColumns = getNumber("columns");
23           int sizeOfCircle = getNumber("circle diameter ");
24           String color = JOptionPane.showInputDialog("color");
25   
26           // Establish the painter.     // CHANGED
27           int height = nrOfRows * sizeOfCircle;
28           int width = nrOfColumns * sizeOfCircle;
29           SPainter miro = new SPainter("Number 3",width+50,height+50);
30           miro.setBrushWidth(4);
31           SCircle circle = new SCircle(sizeOfCircle);
32   
33           if (color.equalsIgnoreCase("blue")) {
34               miro.setColor(Color.blue);
35           } else if (color.equalsIgnoreCase("red")) {
36               miro.setColor(Color.red);
37           } else if (color.equalsIgnoreCase("green")) {
38               miro.setColor(Color.green);
39           }else if (color.equalsIgnoreCase("black")) {
40               miro.setColor(Color.black);
41           }
42   
43   
44           // Paint the circles.
45           paintRectangle(miro,circle,nrOfRows,nrOfColumns);
46       }
47   
48       private static int getNumber(String prompt) {
49           String nss = JOptionPane.showInputDialog(null,prompt+"?");
50           Scanner scanner = new Scanner(nss);
51           return scanner.nextInt();
52       }
53   
54       private static void paintRectangle(SPainter miro, SCircle circle,
55                                          int nrOfRows, int nrOfColumns) {
56           // Position the painter to paint the rectangle of squares.
57   
58           miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter());
59           miro.mbk(((nrOfRows-1)/2.0) * circle.diameter());
60   
61           // Paint the rectangle of squares.
62           int i = 1;
63           while ( i <= nrOfRows) {
64               paintOneRow(miro,nrOfColumns,circle);
65               miro.mfd(circle.diameter());
66               i = i + 1;
67           }
68           // Make the method invariant with respect to painter position.
69           miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter());
70           miro.mfd(((nrOfRows-1)/2.0) * circle.diameter());
71       }
72   
73       private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) {
74           int i = 1;
75           while ( i <= nrOfColumns ) {
76               paintOneCircle(miro,circle);
77               miro.mrt(circle.diameter());
78               i = i + 1;
79           }
80           miro.mlt(nrOfColumns*circle.diameter());
81       }
82   
83       private static void paintOneCircle(SPainter miro, SCircle circle) { // CHANGED
84           circle.s3();
85           miro.paint(circle);
86           miro.draw(circle);
87           circle.x3();
88       }
89   
90       public Number4(){
91           paintTheImage();
92       }
93   
94       public static void main(String[] args){
95           SwingUtilities.invokeLater(new Runnable() {
96               @Override
97               public void run() {
98                   new npw.Number4();
99               }
100          });
101      }
102  }