CHESSBOARD using JAVA graphics

codiction 0 Tallied Votes 1K Views Share

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

/*
 * chessboard.java
 *
 * 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();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();
     }      
    
}
/**
     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()
     {            
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
        // center frame in screen       
       setLocation(screenWidth / 4, screenHeight / 4);
       setResizable(true);      
       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();
        contentPane.add(panel);
     }
     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
       System.out.println(XXX);
       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));
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D)g;       
       for(int j=0;j<no;j++)
       {
       if(j%2==0)
        {flag=false;}
       else if(j%2==1)
        {flag=true;}  
       co_x=XXX;
       for(int i=0;i<no;i++)
        {                
        if(flag==true)
        {
        g2.setPaint(Color.WHITE);
        g2.fill(new Rectangle2D.Double(co_x,co_y,size,size));
        flag=false;
        }
        else
        {
        g2.setPaint(Color.BLACK);
        g2.fill(new Rectangle2D.Double(co_x,co_y,size,size));
        flag=true;
        }
        co_x+=size;
       }                       
       co_y+=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 e.tc

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!)
Thanks

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 game...no AI....

thnk you

Be a part of the DaniWeb community

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