Monday, October 18, 2010

CHESS GAME INTERFACE AND ITS SOURCE CODE

THE CHESS BOARD
Introduction
The aim of the assignment was to write a program that would produce a chess board with all its pieces in place. The program was aimed at using the concepts of 2-dimension graphics in Java namely Graphics and Graphics2D. Initially, it was planned that the program will use all the images of a normal chess board. But due to internet service problems, I failed download other images like Queen, King, e.t.c, instead I used few available pieces to fill the board.

Snapshot




The above diagram is the output of running the attached source code below. I have deliberately used the black and blue color (instead of black and white) so that it is different from those that people have already programmed. Unfortunately, if you run it on your computer, the images of the chess board pieces will not be displayed (since they are kept in my computer).



Conclusion
By coming up with the program, I have realized that not only can I write a program that displays field and labels, but also can display real life objects such as chess. I will try as much as possible to see that I extend the program so that people should be able to play it.

Source code
1 import java.awt.*;
2 import java.awt.geom.*;
3 import javax.swing.*;

4 public class YokamChess{
5 public static void main(String[] args){
6 DrawFrame frame = new DrawFrame();
7 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8 frame.setVisible(true);
9 }
10 }

11 class DrawFrame extends JFrame{
12 public DrawFrame(){
13 setSize(700, 700);
14 setLocationRelativeTo(null);
15 setResizable(false);
16 setTitle("tanjibo's chess board");
17 DrawPanel panel = new DrawPanel();
18 Container contentPane = getContentPane();
19 contentPane.add(panel);
20 }
21 }

22 class DrawPanel extends JPanel{
23 public void paintComponent(Graphics g){
24 ImageIcon pawn = new ImageIcon("c:\\Dogs\\pp1.png");
25 ImageIcon horseZ = new ImageIcon("c:\\Dogs\\z1.png");
26 ImageIcon horseX = new ImageIcon("c:\\Dogs\\X.png");
27 ImageIcon horseN = new ImageIcon("c:\\Dogs\\N1.png");
28 ImageIcon horseP = new ImageIcon("c:\\Dogs\\P1.png");
29 ImageIcon horseJ = new ImageIcon("c:\\Dogs\\horse.png");
30 Image horse1 = pawn.getImage();
31 Image horse2 = horseZ.getImage();
32 Image horse3 = horseX.getImage();
33 Image horse4 = horseN.getImage();
34 Image horse5 = horseP.getImage();
35 Image horse6 = horseJ.getImage();

36 int x_cordinate=0;
37 int y_cordinate=0;
38 int boxSize=80;
39 boolean flag=false;
40 super.paintComponent(g);

41 Graphics2D g2 = (Graphics2D)g;
42 for(int j=0;j<8;j++){
43 if(j%2==0){
44 flag=false;
45 }
46 else if(j%2==1)
47 flag=true;
48 x_cordinate=25;

49 for(int i=0;i<8;i++){
50 if(flag==true){
51 g2.setPaint(Color.blue);
52 g2.fill(new Rectangle2D.Double(x_cordinate,y_cordinate,boxSize,boxSize));
53
int loc3=40;
54 for(int c=0;c<8;c++){
55 g2.drawImage(horse2, loc3,480, this);
56 g2.drawImage(horse4, loc3,560, this);
57 g2.drawImage(horse1, loc3+10,80, this);
58 loc3+=80;
59 }
60
int loc12=120;
61 for(int l=0;l<8;l++){
62 g2.drawImage(horse3, loc12,0, this);
63 g2.drawImage(horse5, loc12,560, this);
64 g2.drawImage(horse6, loc12-70,0, this);
65 loc12+=160;
66 }
67 flag=false;
68 }
69 else{
70 g2.setPaint(Color.black);
71 g2.fill(new Rectangle2D.Double(x_cordinate,y_cordinate,boxSize,boxSize));
72 g2.drawImage(horse5,600,560,this);
73 flag=true;
74 }
75 x_cordinate+=boxSize;
76 }
77 y_cordinate+=boxSize;
78 }
79 }
80 }

No comments:

Post a Comment