/home/kchan2/NetBeansProjects/CS1/src/npw/Number4.java
  1 /*
  2  * Program to paint a rectangle, centered in the canvas, made up of unframed 
  3  * hirst circles of either red, blue, or green, or black if user type in 
  4  * something other that one of the three specified color names.
  5  */
  6 
  7 package npw;
  8 
  9 import java.awt.Color;
 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 
 17 /**
 18  *
 19  * @author kchan2
 20  */
 21 public class Number4 {
 22     
 23     // REQUIRED INFRASTRUCTURE
 24 
 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     
 42     private void paintTheImage() {
 43         // GRAB THE INPUT INFORMATION
 44         int nrOfRows = getNumber("rows");
 45         int nrOfColumns = getNumber("columns");
 46         int sizeOfCircle = getNumber("circle diameter");
 47         String color = getColor("color");
 48         // ESTABLISH THE PAINTER
 49         int height = nrOfRows * sizeOfCircle;
 50         int width = nrOfColumns * sizeOfCircle;
 51         SPainter miro = new SPainter("Number 4",width+50,height+50);
 52         miro.setBrushWidth(4);
 53         SCircle circle = new SCircle(sizeOfCircle/2);
 54         // PAINT THE RECTANGLES
 55         paintRectangle(miro,circle,nrOfRows,nrOfColumns,color);
 56     }
 57 
 58     private int getNumber(String prompt) {
 59         String nss = JOptionPane.showInputDialog(null,prompt+"?");
 60         Scanner scanner = new Scanner(nss);
 61         return scanner.nextInt();
 62     }
 63 
 64     private void paintRectangle(SPainter miro, SCircle circle, int nrOfRows, int nrOfColumns, String color) {
 65         // POSITION THE PAINTER TO PAINT THE RECTANGLE OF SQUARES
 66         miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter());
 67         miro.mbk(((nrOfRows-1)/2.0) * circle.diameter());
 68         // PAINT THE RECTANGLE OF SQUARES
 69         int i = 1;
 70         while (i <= nrOfRows) {
 71             paintOneRow(miro,nrOfColumns,circle,color);
 72             miro.mfd(circle.diameter());
 73             i = i + 1;
 74         }
 75         miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter());
 76         miro.mfd(((nrOfRows-1)/2.0) * circle.diameter());
 77     }
 78 
 79     private void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle, String 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 void paintOneCircle(SPainter miro, SCircle circle, String color) {
 90         circle.s2();
 91         if (color.equalsIgnoreCase("red")) {
 92             miro.setColor(Color.RED);
 93         } else if (color.equalsIgnoreCase("blue")) {
 94             miro.setColor(Color.BLUE);
 95         } else if (color.equalsIgnoreCase("green")) {
 96             miro.setColor(Color.GREEN);
 97         } else {
 98             miro.setColor(Color.BLACK);
 99         }
100         miro.paint(circle);
101         circle.x2();
102     }
103 
104     private String getColor(String prompt) {
105         String color = JOptionPane.showInputDialog(null,prompt+"?");
106         Scanner scanner = new Scanner(color);
107         return scanner.next();
108     }
109     
110 }