CHESSBOARD using JAVA graphics


This particular snippet will display a chessboard in a new frame. Needless to say save it in a file named "".

About the Author
 * Created on August 2, 2008, 12:37 PM
 import java.awt.*;
 import java.awt.geom.*;
 import javax.swing.*;
 * @author Administrator
public class chessboard {
  public static void main(String[] args)
        DrawFrame frame = new DrawFrame();
     A frame that contains a panel with drawings
 class DrawFrame extends JFrame
     static int one_for_all=700; //decides the width and heigth ofthe frame
     Toolkit kit = Toolkit.getDefaultToolkit();
       Dimension screenSize = kit.getScreenSize();
       public int screenHeight = screenSize.height;
       public int screenWidth = screenSize.width;

     public DrawFrame()
        // center frame in screen       
       setLocation(screenWidth / 4, screenHeight / 4);
       setTitle("A Standard Chess Board");       
       setUndecorated(false);//try changing TRUE | FALSE       
       /*The title bar, title, frame border, close(X) button everything will be removed
         What you will see will just be a plain frame,a plain area.

        // add panel to frame

        DrawPanel panel = new DrawPanel();
        Container contentPane = getContentPane();
     public static final int FRAME_WIDTH = one_for_all;
     public static final int FRAME_HEIGHT = one_for_all;

     A panel that displays rectangles and ellipses.
 class DrawPanel extends JPanel
    public void paintComponent(Graphics g)
       DrawFrame frame = new DrawFrame();
       int XXX=25;//x coordinate of the point where drawing starts
       int YYY=25;//x coordinate of the point where drawing starts
       int co_x=XXX;
       int co_y=YYY;
       int no=8;   // value of no should be at the most frameSize/10
       int size=80; //size of each box
       boolean flag=false;
       setBackground(new Color(80,40,40));
       Graphics2D g2 = (Graphics2D)g;       
       for(int j=0;j<no;j++)
       else if(j%2==1)
       for(int i=0;i<no;i++)
        g2.fill(new Rectangle2D.Double(co_x,co_y,size,size));
        g2.fill(new Rectangle2D.Double(co_x,co_y,size,size));
cms271828 2 Junior Poster

I don't get it, its pretty pointless, but does show how to draw things.

I'm building a 3D chessboard which should be useful, but its too big to fit as a code snippet.

srvstv.prnc 0 Newbie Poster

the code is pretty useful for knowing how to draw frames

rpdDW 0 Newbie Poster

Nice code.
Small point: a 'real' chess board needs orientating with a light square in the bottom right hand corner
(so just swop around the colour values in the code will display a properly orientated chessboard-perhaps programmers are not chess players!
Also use different colour keywords for the squares as b & w are a bit dull!
Now all it needs are coordinates adding around the board edge and some draggable pieces graphics and it can become a GUI to add a chess engine to-easy really LOL!)

mundvawala 0 Newbie Poster

any1 plz help me........
where i can find the code of complete simple* chess game.........

*only two player can play the AI....

thnk you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.