/home/sjenks/NetBeansProjects/CS1/src/npw/Number3.java
  1 /*
  2  * Program to paint a circle, 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 Number3 {
 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 Number3();
 32         }
 33     });
 34     }
 35     
 36     public Number3() {
 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         //ESTABLISH THE PAINTER
 48         int height = nrOfRows * (2 * sizeOfCircle);
 49         int width = nrOfColumns * (2 * sizeOfCircle);
 50         SPainter miro = new SPainter ("Number 3", width+50,height+50);
 51         miro.setBrushWidth(4);
 52         SCircle circle = new SCircle (sizeOfCircle);
 53         //PAINT THE RECTANGLES
 54         paintCircle(miro,circle,nrOfRows,nrOfColumns);
 55         
 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 paintCircle(SPainter miro, SCircle circle, 
 65     int nrOfRows, int nrOfColumns) {
 66         //POSTION THE PAINTER TO PAINTE THE RECTANGLE OF CIRCLES
 67         miro.mlt(((nrOfColumns-1)/2.0) * circle.diameter());
 68         miro.mbk(((nrOfRows-1)/2.0) * circle.diameter());
 69         //PAINT THE RECTANGLE OF CIRCLES
 70         int i = 1;
 71         while ( i <= nrOfRows) {
 72             paintOneRow (miro,nrOfColumns,circle);
 73             miro.mfd(circle.diameter());
 74             i = i + 1;        
 75         }
 76         
 77         //MAKE THE METHOD INVARIANT WITH RESPECT TO PAINTER POSTION
 78         
 79         miro.mrt(((nrOfColumns-1)/2.0) * circle.diameter());
 80         miro.mfd (((nrOfRows-1)/2.0) * circle.diameter());
 81         
 82     }
 83 
 84     private void paintOneRow(SPainter miro, int nrOfColumns, SCircle circle) {
 85        int i = 1; 
 86        while ( i <= nrOfColumns) {
 87            paintOneCircle(miro,circle);
 88            miro.mrt(circle.diameter());
 89            i = i + 1;
 90        }
 91        miro.mlt(nrOfColumns*circle.diameter());
 92     }
 93 
 94     private void paintOneCircle(SPainter miro, SCircle circle) {
 95         miro.setColor(randomColor());
 96         circle.s3();
 97         miro.paint(circle);
 98         circle.x3();
 99     }
100 
101     private Color randomColor() {
102        Random rgen = new Random();
103        int r = rgen.nextInt(256);
104        int g = rgen.nextInt (256);
105        int b = rgen.nextInt(256);
106        return new Color(r,b,g);
107     }
108 }
109