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