Results 1 to 2 of 2
  1. #1
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    db

Similar Threads

  1. Simple Checkerboard
    By ShortIt in forum New To Java
    Replies: 2
    Last Post: 01-19-2011, 04:03 AM
  2. Checkerboard Program
    By Jnoobs in forum New To Java
    Replies: 9
    Last Post: 09-21-2010, 03:46 AM
  3. Replies: 3
    Last Post: 07-23-2009, 07:45 AM
  4. Replies: 1
    Last Post: 07-22-2009, 10:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •