/* Illustrates the use of the: Color constructor Color(int, int, int) increment operator */ import java.awt.*; import java.applet.*; import java.awt.event.*; // import the window event package public class redblueboard extends Applet implements ActionListener { Label sizeL = new Label("Edge length"), numL = new Label("Number"); TextField sizeT = new TextField("40", 2), numT = new TextField("8", 2); int num = 8, size = 40; // size of square public void init() { add(numL); add(numT); add(sizeL); add(sizeT); // "this" applet handles ActionEvents for all TextFields sizeT.addActionListener( this ); numT.addActionListener( this ); } public void paint(Graphics g) { int col, xOffset = 40, // horizontal offset from left yOffset = 40, delta = 255/num; // vertical offset from top Color c; int row = -1; while (++row < num) { col = -1; while (++col < num) { if ((row + col) % 3 == 0) c = new Color(row* delta, 0, col*delta); else if ((row + col) % 3 == 1) c = new Color(0, col*delta, row*delta); else c = Color.black; g.setColor(c); g.fillRect(xOffset + col*size, yOffset + row*size, size, size); } } } public void actionPerformed( ActionEvent e ) { size = Integer.parseInt( sizeT.getText() ); num = Integer.parseInt( numT.getText() ); repaint(); } }