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