/home/ssingh6/NetBeansProjects/CS1/src/npw/Number2.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  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.SSquare;
 15 
 16 /**
 17  *
 18  * @author ssingh6
 19  */
 20 public class Number2 {
 21 
 22     /**
 23      * @param args the command line arguments
 24      */
 25     public static void main(String[] args) {
 26       
 27      SwingUtilities.invokeLater(new Runnable() {
 28               public void run() {
 29                   new Number2();
 30               }
 31           });
 32       }
 33       
 34       public Number2() {
 35           paintTheImage();
 36       }
 37       
 38       // THE SOLUTION TO THE GRAPHICAL PROBLEM
 39       
 40       private void paintTheImage() {
 41           // GRAB THE INPUT INFORMATION
 42           int nrOfRows = getNumber("rows");
 43           int nrOfColumns = getNumber("columns");
 44           int sizeOfSquare = getNumber("square side length");
 45          // ESTABLISH THE PAINTER
 46           int height = nrOfRows * sizeOfSquare;
 47           int width = nrOfColumns * sizeOfSquare;
 48           SPainter micro = new SPainter("Number 2",width+50,height+50);
 49          micro.setBrushWidth(4);
 50           SSquare square = new SSquare(sizeOfSquare);
 51           // PAINT THE RECTANGLES
 52           paintRectangle(micro,square,nrOfRows,nrOfColumns);
 53       }
 54   
 55       private int getNumber(String prompt) {
 56           String nss = JOptionPane.showInputDialog(null,prompt+"?");
 57           Scanner scanner = new Scanner(nss);
 58           return scanner.nextInt();
 59       }
 60   
 61       private void paintRectangle(SPainter micro, SSquare square, int nrOfRows, int nrOfColumns) {
 62           // POSITION THE PAINTER TO PAINT THE RECTANGLE OF SQUARES
 63           micro.mlt(((nrOfColumns-1)/2.0) * square.side());
 64           micro.mbk(((nrOfRows-1)/2.0) * square.side());
 65           // PAINT THE RECTANGLE OF SQUARES
 66           int i = 1;
 67           while (i <= nrOfRows) {
 68               paintOneRow(micro,nrOfColumns,square);
 69               micro.mfd(square.side());
 70               i = i + 1;
 71           }
 72           micro.mrt(((nrOfColumns-1)/2.0) * square.side());
 73           micro.mfd(((nrOfRows-1)/2.0) * square.side());
 74       }
 75   
 76       private void paintOneRow(SPainter micro, int nrOfColumns, SSquare square) {
 77           int i = 1;
 78           while ( i <= nrOfColumns ) {
 79               paintOneSquare(micro,square);
 80               micro.mrt(square.side());
 81              i = i + 1;
 82           }
 83           micro.mlt(nrOfColumns * square.side());
 84       }
 85   
 86       private void paintOneSquare(SPainter micro, SSquare square) {
 87           square.s2();
 88           micro.setColor(randomColor());
 89           micro.paint(square);
 90           micro.setColor(Color.BLACK);
 91           micro.draw(square);
 92           square.x2();
 93          
 94       }
 95   
 96       private Color randomColor() {
 97           Random rgen = new Random();
 98           int r = rgen.nextInt(256);
 99           int g = rgen.nextInt(256);
100           int b = rgen.nextInt(256);
101           return new Color(r,b,g);
102      }
103      
104  }   
105        
106     
107     
108 
109