The following text was written to the standard output stream when the Number4 program was executed from Netbeans.
/*
* Program to paint a rectangle, centered in the canvas, made up of randomly
* colored circles.
*/
package npw;
import java.awt.Color;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import painter.SPainter;
import shapes.SCircle;
/**
*
* @author dmaslows
*/
public class Number4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Number4();
}
});
}
public Number4() {
paintTheImage();
}
private void paintTheImage() {
int nrofrows = getNumber("rows");
int nrofcolumns = getNumber("columns");
int sizeofcircle = getNumber("circle diameter");
String color = JOptionPane.showInputDialog(null, "Color?");
int height = nrofrows * sizeofcircle;
int width = nrofcolumns * sizeofcircle;
SPainter miro = new SPainter("Number 4",width +300,height +300);
miro.setBrushWidth(4);
SCircle circle = new SCircle(sizeofcircle);
paintRectangle(miro, circle, nrofrows, nrofcolumns, color);
}
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, SCircle circle, int nrofrows, int nrofcolumns, String color) {
miro.mlt(((nrofcolumns - 1) / 2.0) * circle.diameter());
miro.mbk(((nrofrows - 1) / 2.0) * circle.diameter());
int i = 1;
while (i <= nrofrows) {
paintOneRow(miro, nrofcolumns, circle, color);
miro.mfd(circle.diameter());
i = i + 1;
}
miro.mrt(((nrofcolumns - 1) / 2.0) * circle.diameter());
miro.mfd(((nrofrows - 1) / 2.0) * circle.diameter());
}
private static void paintOneRow(SPainter miro, int nrofcolumns, SCircle circle, String color) {
int i = 1;
while (i <= nrofcolumns) {
paintOneCircle(miro, circle, color);
miro.mrt(circle.diameter());
i = i + 1;
}
miro.mlt(nrofcolumns * circle.diameter());
}
private static void paintOneCircle(SPainter miro, SCircle circle, String color) {
circle.s2();
if (color == null) {
color = "exit";
} // user clicked on Cancel
if (color.equalsIgnoreCase("blue")) {
miro.setColor(Color.BLUE);
miro.paint(circle);
circle.x2();
} else if (color.equalsIgnoreCase("red")) {
miro.setColor(Color.RED);
miro.paint(circle);
circle.x2();
}
if (color.equalsIgnoreCase("green")) {
miro.setColor(Color.GREEN);
miro.paint(circle);
circle.x2();
} else if (color.equalsIgnoreCase("black")) {
miro.setColor(Color.BLACK);
miro.paint(circle);
circle.x2();
}
}
}