/home/sjenks/NetBeansProjects/CS1/src/npw/Number4.java
  1 /*
  2  * Program to paint a cirlce, centered in the canvas, made up of randomly
  3  * colored, that are not touching each other.
  4  */
  5 
  6 package npw;
  7 
  8 import java.awt.Color;
  9 import java.util.Random;
 10 import java.util.Scanner;
 11 import javax.swing.JOptionPane;
 12 import javax.swing.SwingUtilities;
 13 import painter.SPainter;
 14 import shapes.SCircle;
 15 
 16 
 17 /**
 18  *
 19  * @author sjenks
 20  */
 21 public class Number4 {
 22 
 23     //REQUIRED INFASTURCTURE
 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 IMPUT INFORMATION
 44         int nrOfRows = getNumber ("rows");
 45         int nrOfColumns = getNumber ("columns");
 46         int sizeOfCircle = getNumber ("circle diameter");   
 47         String colorOfDots = getColor();
 48         
 49         //ESTABLISH THE PAINTER
 50         int height = nrOfRows * (2 * sizeOfCircle);
 51         int width = nrOfColumns * (2 * sizeOfCircle);
 52         SPainter miro = new SPainter ("Number 4", width+50,height+50);
 53         miro.setBrushWidth(4);
 54         SCircle circle = new SCircle (sizeOfCircle);
 55         //PAINT THE RECTANGLES
 56         paintCircle(miro,circle,nrOfRows,nrOfColumns, colorOfDots);
 57         
 58     }
 59 
 60     private int getNumber(String prompt) {
 61         String nss = JOptionPane.showInputDialog(null,prompt+"?");
 62         Scanner scanner = new Scanner(nss);
 63         return scanner.nextInt();
 64 }
 65 
 66     private void paintCircle(SPainter miro, SCircle circle, 
 67     int nrOfRows, int nrOfColumns, String colorOfDots) {
 68         //POSTION THE PAINTER TO PAINTE THE RECTANGLE OF CIRCLES
 69         miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter());
 70         miro.mbk(((nrOfRows-1)/2.0) * circle.diameter());
 71         //PAINT THE RECTANGLE OF CIRCLES
 72         int i = 1;
 73         while ( i <= nrOfRows) {
 74             paintOneRow (miro,nrOfColumns,circle, colorOfDots);
 75             miro.mfd(circle.diameter());
 76             i = i + 1;        
 77         }
 78         
 79         //MAKE THE METHOD INVARIANT WITH RESPECT TO PAINTER POSTION
 80         
 81         miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter());
 82         miro.mfd (((nrOfRows-1)/2.0) * circle.diameter());
 83         
 84     }
 85 
 86     private void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle, String colorOfDots) {
 87        int i = 1; 
 88        while ( i <= nrOfColumns) {
 89            paintOneCircle(miro,circle, colorOfDots);
 90            miro.mrt(circle.diameter());
 91            i = i + 1;
 92        }
 93        miro.mlt(nrOfColumns*circle.diameter());
 94     }
 95 
 96     private void paintOneCircle(SPainter miro, SCircle circle, String colorOfDots) {
 97        setColor (colorOfDots, circle, miro);
 98         circle.s3();
 99         miro.paint(circle);
100         circle.x3();
101     }
102 
103     private void setColor(String colorOfDots, SCircle circle, SPainter miro) {
104         if (colorOfDots.equalsIgnoreCase("blue")) {
105                 miro.setColor(Color.BLUE);
106             } else if (colorOfDots.equalsIgnoreCase("red")) {
107                 miro.setColor(Color.RED);
108             } else if (colorOfDots.equalsIgnoreCase("green")) {
109                 miro.setColor(Color.GREEN);
110     }
111     }
112 
113     private String getColor() {
114         String nss = JOptionPane.showInputDialog("Red, Blue, Green, or Black?");
115         Scanner scanner = new Scanner(nss);
116         return scanner.next();
117     }
118 }