/home/evankemp/NetBeansProjects/CS1/src/npw/Number4.java
  1 /*
  2  * Program to paint a rectangle made up of not-bonded red, blue or green circles.
  3  */
  4 package npw;
  5 
  6 import java.awt.Color;
  7 import static java.awt.Color.BLUE;
  8 import static java.awt.Color.getColor;
  9 import java.awt.Graphics;
 10 import java.util.Random;
 11 import java.util.Scanner;
 12 import javax.swing.JOptionPane;
 13 import javax.swing.SwingUtilities;
 14 import painter.SPainter;
 15 import shapes.SCircle;
 16 import shapes.SSquare;
 17 
 18 /**
 19  *
 20  * @author evankemp
 21  */
 22 public class Number4 {
 23 
 24     // REQUIRED INFRASTRUCTURE
 25     /**
 26      * @param args the command line arguments
 27      */
 28     public static void main(String[] args) {
 29         SwingUtilities.invokeLater(new Runnable() {
 30             public void run() {
 31                 new Number4();
 32             }
 33         });
 34     }
 35 
 36     public Number4() {
 37         paintTheImage();
 38     }
 39 
 40     // THE SOLUTION TO THE GRAPHICAL PROBLEM
 41     private void paintTheImage() {
 42         //GRAB THE INPUT INFORMATION
 43         int nrOfRows = getNumber("rows");
 44         int nrOfColumns = getNumber("columns");
 45         int sizeOfCircle = getNumber("circle radius length");
 46         Color color = getTheColor("color");
 47         //ESTABLISH THE PAINTER
 48         int height = nrOfRows * sizeOfCircle * 2;
 49         int width = nrOfColumns * sizeOfCircle * 2;
 50         SPainter miro = new SPainter("Number 4", width + 50, height + 50);
 51         miro.setBrushWidth(4);
 52         SCircle circle = new SCircle(sizeOfCircle);
 53         //PAINT THE RECTANGLE
 54         paintRectangle(miro, circle, nrOfRows, nrOfColumns, color);
 55     }
 56 
 57     private static int getNumber(String prompt) {
 58         String nss = JOptionPane.showInputDialog(null, prompt + "?");
 59         Scanner scanner = new Scanner(nss);
 60         return scanner.nextInt();
 61     }
 62 
 63     private static void paintRectangle(SPainter miro, SCircle circle, int nrOfRows, int nrOfColumns, Color color) {
 64         //POSITION THE PAINTER TO PAINT THE RECTANGLE OF CIRCLES
 65         miro.mlt(((nrOfColumns - 1) / 2.0) * circle.diameter());
 66         miro.mbk(((nrOfRows - 1) / 2.0) * circle.diameter());
 67         //PAINT THE RECTANGLE OF CIRCLES
 68         int i = 1;
 69         while (i <= nrOfRows) {
 70             paintOneRow(miro, nrOfColumns, circle, color);
 71             miro.mfd(circle.diameter());
 72             i = i + 1;
 73         }
 74         //MAKE THE METHOD INVARIANT WITH RESPECT TO THE PAINTER POSITION
 75         miro.mrt(((nrOfColumns - 1) / 2.0) * circle.diameter());
 76         miro.mfd(((nrOfRows - 1) / 2.0) * circle.diameter());
 77     }
 78 
 79     private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle, Color color) {
 80         int i = 1;
 81         while (i <= nrOfColumns) {
 82             paintOneCircle(miro, circle, color);
 83             miro.mrt(circle.diameter());
 84             i = i + 1;
 85         }
 86         miro.mlt(nrOfColumns * circle.diameter());
 87     }
 88 
 89     private static void paintOneCircle(SPainter miro, SCircle circle, Color color) {
 90         circle.s2();
 91         miro.setColor(color);
 92         miro.paint(circle);
 93         circle.x2();
 94     }
 95 
 96     private static Color getTheColor(String color) {
 97         while (true) {
 98             String command = JOptionPane.showInputDialog(null, "color" + "?");
 99             if (command.equalsIgnoreCase("blue")) {
100                 return new Color(0, 0, 255);
101             } else if (command.equalsIgnoreCase("red")) {
102                 return new Color(255, 0, 0);
103             } else if (command.equalsIgnoreCase("green")) {
104                 return new Color(0, 255, 0);
105             } else {
106                 return new Color(0, 0, 0);
107             }
108 
109         }
110     }
111 }
112