/home/dmyrie/NetBeansProjects/CS1/src/npw/Number3.java
  1 /*
  2  * Program to paint a SQUARE CANVAS (a row and a column consists of the same number of dots) 
  3  * made up of randomly colored, unframed dots. Each dot is separated by the same
  4  * white (blank) space within the frame of the canvas.
  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  * @author dmyrie
 19  */
 20 public class Number3 {
 21 
 22     // REQUIRED INFRASTRUCTURE 
 23     /**
 24      * @param args the command line arguments
 25      */
 26     public static void main(String[] args) {
 27         SwingUtilities.invokeLater(new Runnable() {
 28             public void run() {
 29                 new Number3();
 30 
 31             }
 32         });
 33 
 34     }
 35 
 36     public Number3() {
 37         paintTheImage();
 38     }
 39 
 40     // THE SOLUTION TO THE GRAPHICAL PROBLEM
 41     private void paintTheImage() {
 42         // GRAB THE INPUT INFORMATION
 43         int nrOfRows = getNumber("rows");
 44         int nrOfColumns = getNumber("columns");
 45         int sizeOfCircle = getNumber("circle diameter");
 46 
 47         // ESTABLISH THE PAINTER
 48         int height = nrOfRows * sizeOfCircle;
 49         int width = nrOfColumns * sizeOfCircle;
 50         SPainter miro = new SPainter("Number 3", width + 50, height + 50);
 51         miro.setBrushWidth(4);
 52         SCircle dot = new SCircle(sizeOfCircle / 2);
 53         
 54         // PAINT THE SQUARES
 55         paintSquare(miro, dot, nrOfRows, nrOfColumns);
 56         System.out.println("");
 57 
 58     }
 59 
 60     private static 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 static void paintSquare(SPainter miro, SCircle dot, int nrOfRows, int nrOfColumns) {
 67         // POSITION THE PAINTER TO PAINT THE SQUARE OF DOTS
 68         miro.mlt(((nrOfColumns - 1) / 2.0) * dot.diameter());
 69         miro.mbk(((nrOfRows - 1) / 2.0) * dot.diameter());
 70         
 71         // PAINT THE SQUARE OF DOTS
 72         int i = 1;
 73         while (i <= nrOfRows) {
 74             paintOneRow(miro, nrOfColumns, dot);
 75             miro.mfd(dot.diameter());
 76             i = i + 1;
 77         }
 78         
 79         // MAKE THE METHOD INVARIANT WITH RESPECT TO PAINTER POSITION
 80         miro.mrt(((nrOfColumns - 1) / 2.0) * dot.diameter());
 81         miro.mfd(((nrOfRows - 1) / 2.0) * dot.diameter());
 82         System.out.println("");
 83 
 84     }
 85 
 86     private static void paintOneRow(SPainter miro, int nrOfColumns, SCircle dot) {
 87         int i = 1;
 88         while (i <= nrOfColumns) {
 89             paintOneCircle(miro, dot);
 90             miro.mrt(dot.diameter());
 91             i = i + 1;
 92         }
 93         miro.mlt(nrOfColumns * dot.diameter());
 94         System.out.println("");
 95 
 96     }
 97 
 98     private static void paintOneCircle(SPainter miro, SCircle dot) {
 99         dot.s2();
100         miro.setColor(randomColor());
101         miro.paint(dot);
102         miro.setColor(Color.WHITE);
103         miro.draw(dot);
104         dot.x2();
105 
106     }
107 
108     private static Color randomColor() {
109         Random rgen = new Random();
110         int r = rgen.nextInt(256);
111         int g = rgen.nextInt(256);
112         int b = rgen.nextInt(256);
113         return new Color(r, b, g);
114     }
115 }
116