The following text was written to the standard output stream when the Number1 program was executed from Netbeans.
/*
* Program to paint a rectangle, centered in the canvas, made up of randomly
* colored, black framed squares.
*/
package npw;
import java.awt.Color;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import painter.SPainter;
import shapes.SSquare;
/**
*
* @author dmaslows
*/
public class Number1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Number1();
}
});
}
public Number1() {
paintTheImage();
}
private void paintTheImage() {
int nrofrows = getNumber("rows");
int nrofcolumns = getNumber("columns");
int sizeofsquare = getNumber("square side length");
int height = nrofrows * sizeofsquare;
int width = nrofcolumns * sizeofsquare;
SPainter miro = new SPainter("Number 1",width+50,height+50);
miro.setBrushWidth(4);
SSquare square = new SSquare(sizeofsquare);
paintRectangle(miro,square,nrofrows,nrofcolumns);
}
private static int getNumber(String prompt) {
String nss = JOptionPane.showInputDialog(null,prompt+"?");
Scanner scanner = new Scanner(nss);
return scanner.nextInt();
}
private static void paintRectangle(SPainter miro, SSquare square, int nrofrows, int nrofcolumns) {
miro.mlt(((nrofcolumns-1)/2.0)* square.side());
miro.mbk(((nrofrows-1)/2.0)* square.side());
int i = 1;
while ( i <= nrofrows) {
paintOneRow(miro,nrofcolumns,square);
miro.mfd(square.side());
i = i+1;
}
miro.mrt(((nrofcolumns-1)/2.0) * square.side());
miro.mfd(((nrofrows-1)/2.0) * square.side());
}
private static void paintOneRow(SPainter miro, int nrofcolumns, SSquare square){
int i = 1;
while ( i <= nrofcolumns) {
paintOneSquare(miro,square);
miro.mrt(square.side());
i = i+1;
}
miro.mlt(nrofcolumns*square.side());
}
private static void paintOneSquare(SPainter miro, SSquare square) {
miro.setColor(randomColor());
miro.paint(square);
miro.setColor(Color.BLACK);
miro.draw(square);
}
private static Color randomColor() {
Random rgen = new Random();
int r = rgen.nextInt(256);
int g = rgen.nextInt(256);
int b = rgen.nextInt(256);
return new Color(r,b,g);
}
}