Results 1 to 2 of 2
Thread: checkerboard help
- 04-03-2011, 06:53 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
checkerboard help
Hi i am very new to java still and i cannot figure out what is wrong with this java program.
I will need 3 classes: Checkerboard, CheckerboardComponent, and CheckerBoardViewer.
[code]import java.awt.Graphics2D;
import java.awt.Rectangle;
/**
This class displays a checkerboard with squares,
alternating between white and black.
*/
public class CheckerBoard
{
/**
Creates a CheckerBoard object with a given number of squares.
@param aNumSquares the number of squares in each row
@param aSize the size of each square
*/
public CheckerBoard(int aNumSquares, int aSize)
{
numSquares = aNumSquares;
size = aSize;
}
/**
Method used to draw the checkerboard.
@param g2 the graphics content
*/
public void draw(Graphics2D g2)
{
//
}
private int numSquares;
private int size;
}
CheckerBoardComponent.java
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class CheckerBoardComponent extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
final int NSQUARES = 8;
int size = Math.min(getWidth(), getHeight()) / NSQUARES;
CheckerBoard cb = new CheckerBoard(NSQUARES, size);
cb.draw(g2);
}
}
CheckerBoardViewer.java
import javax.swing.JFrame;
/**
This program displays a checkerboard.
*/
public class CheckerBoardViewer
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int FRAME_WIDTH = 330;
final int FRAME_HEIGHT = 360;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("CheckerBoardViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
CheckerBoardComponent component = new CheckerBoardComponent();
frame.add(component);
frame.setVisible(true);
}
}[code]Last edited by armedrabbit; 04-03-2011 at 08:12 PM.
- 04-03-2011, 07:23 AM #2
Similar Threads
-
Simple Checkerboard
By ShortIt in forum New To JavaReplies: 2Last Post: 01-19-2011, 04:03 AM -
Checkerboard Program
By Jnoobs in forum New To JavaReplies: 9Last Post: 09-21-2010, 03:46 AM -
Creating Checkerboard from a 2 dimensional 'for' loop.
By New2Java in forum New To JavaReplies: 3Last Post: 07-23-2009, 07:45 AM -
Creating Checkerboard from a 2 dimensional 'for' loop
By New2Java in forum New To JavaReplies: 1Last Post: 07-22-2009, 10:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks