/Users/cappello/NetBeansProjects/56/56-checkerboard/src/pkg56/checkerboard/CheckerBoard.java |
package pkg56.checkerboard;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
@author
public class CheckerBoard
{
private int boardSize;
private int squareSize;
private Image image;
private ImageIcon imageIcon;
private JLabel jLabel;
private JFrame jFrame;
CheckerBoard()
{
boardSize = getInt( "n X n checker board for what value of n? [2 - 10]?" );
squareSize = getInt( "How many pixels per square? [1 - 100]?" );
int imageSize = boardSize * squareSize;
image = new BufferedImage( imageSize, imageSize, BufferedImage.TYPE_INT_ARGB );
imageIcon = new ImageIcon( image );
jLabel = new JLabel( imageIcon );
jFrame = new JFrame( "Checker Board" );
jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container container = jFrame.getContentPane();
container.setLayout( new BorderLayout() );
container.add( jLabel, BorderLayout.CENTER );
jFrame.pack();
}
@param args
public static void main(String[] args)
{
CheckerBoard checkerBoard = new CheckerBoard();
checkerBoard.paint();
checkerBoard.view();
}
private int getInt( String question )
{
String intString = JOptionPane.showInputDialog( question );
return Integer.parseInt( intString );
}
Paint the checker board onto the Image.
private void paint()
{
Graphics graphics = image.getGraphics();
graphics.setColor( Color.red );
graphics.fillRect( 0, 0, boardSize * squareSize, boardSize * squareSize);
graphics.setColor( Color.black );
for ( int row = 0; row < boardSize; row++ )
{
for ( int col = row % 2; col < boardSize; col += 2 )
{
graphics.fillRect( row * squareSize, col * squareSize, squareSize, squareSize );
}
}
}
private void view() { jFrame.setVisible( true ); }
}