/home/akc/NetBeansProjects/CS1/src/npw/Number4.java
  1 /*
  2  *Program to paint hirst dots based on user's input.
  3  */
  4 package npw;
  5 
  6 import java.awt.Color;
  7 import java.util.Random;
  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 akc
 17  */
 18 public class Number4 {
 19 
 20     /**
 21      * @param args the command line arguments
 22      */
 23     public static void main(String[] args) {
 24         SwingUtilities.invokeLater(new Runnable() {
 25             public void run() {
 26                 new Number4();
 27             }
 28         });
 29     }
 30 
 31     public Number4() {
 32         paintTheImage();
 33     }
 34 
 35     // THE SOLUTION TO THE GRAPICAL PROBLEM
 36     private void paintTheImage() {
 37         // GRAB THE INPUT INFORMATION
 38         int nrOfRows = getNumber("rows");
 39         int nrOfColumns = getNumber("columns");
 40         int sizeOfCircle = getNumber("circle radius");
 41         String whichColor = getColor("Red, Blue or Green");
 42         // ESTABLISH THE PAINTER
 43         int height = nrOfRows * sizeOfCircle;
 44         int width = nrOfColumns * sizeOfCircle;
 45         SPainter miro = new SPainter("Number 4", width + 50, height + 50);
 46         miro.setBrushWidth(4);
 47         SCircle circle = new SCircle(sizeOfCircle);
 48         // PAINT THE RECTANGLES
 49         paintRectangle(miro, circle, nrOfRows, nrOfColumns, whichColor);
 50     }
 51 
 52     private static int getNumber(String prompt) {
 53         String nss = JOptionPane.showInputDialog(null, prompt + "?");
 54         Scanner scanner = new Scanner(nss);
 55         return scanner.nextInt();
 56     }
 57     
 58     private static String getColor(String prompt) {
 59         String nss = JOptionPane.showInputDialog(null, prompt + "?");
 60         Scanner scanner = new Scanner(nss);
 61         return scanner.nextLine();
 62     }
 63 
 64     private static void paintRectangle(SPainter miro, SCircle circle,
 65             int nrOfRows, int nrOfColumns, String whichColor) {
 66         // POSITION THE PAINTER TO PAINT THE RECTANGLE OF CIRCLES
 67         miro.mlt(((nrOfColumns - 1) / 2.0) * circle.radius());
 68         miro.mbk(((nrOfRows - 1) / 2.0) * circle.radius());
 69         // PAINT THE RECTANGLE OF CIRCLES
 70         int i = 1;
 71         while (i <= nrOfRows) {
 72             paintOneRow(miro, nrOfColumns, circle, whichColor);
 73             miro.mfd(circle.radius());
 74             i = i + 1;
 75         }
 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 whichColor) {
 83         int i = 1;
 84         while (i <= nrOfColumns) {
 85             paintOneCircle(miro, circle, whichColor);
 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 whichColor) {
 93         circle.s3();
 94         if (whichColor.equalsIgnoreCase("blue")) {
 95             miro.setColor(Color.BLUE);
 96         } else if (whichColor.equalsIgnoreCase("green")) {
 97             miro.setColor(Color.GREEN);
 98         } else if (whichColor.equalsIgnoreCase("red")) {
 99             miro.setColor(Color.RED);
100         } else {
101             miro.setColor(Color.BLACK);
102         }
103         miro.paint(circle);
104         circle.x3();
105         
106     }
107 }
108