Tuesday, October 19, 2010

2D CHESS BOARD AND CHESS PIECES-By Samson C Mkandawire

THE 2D CHESS BOARD AND CHESS PIECES

Introduction
Chess is both an indoor and outdoor game played by two individuals on either a board or a loan. The players of the chess game move chess pieces in order to have a checkmate move which aims at the king piece of the opponent. Currently, electronic chess boards and chess pieces have been developed allowing individuals to play the chess game on a computer against an opponent which can either be the computer itself or a fellow individual. In this outline, a brief description through snapshots of outputs of a program that is designed and coded to produce an electronic two dimension (2D) chess board with chess pieces arranged on it is discussed.
  
The snapshots below have been produced by a Java program. The program has been developed by using the Java Runtime Environment (JRE) which has the Java Development Kit (JDK) as the compiler and the jGrasp as the text editor. The outline below include a snapshot of a 2D chess board with chess pieces arranged on the board ready for a game and the java code that has generated the chess board.

Attributes
After running the program, the code produces a 2D chess board with the chess pieces arranged on it. The chess board is defined with two colours namely black and white. On the other hand , the chess pieces which have just been imported from a file (file path c:\\Chesspieces\\) have not been generated by the java code. The pieces are saved in the PNG file format.

Apart from the chess board and the chess pieces, there is an inscription on the board which describes the chess board as a product of Samson C Mkandawire. This inscription has been produced generated on the drawing canvas by a drawing method from a drawString class.



SNAPSHOTS 
Once the program has been run, a 2D chess board is generated by the code and produced on the desktop. The frame interface where the board is generated can be either minimized or maximized depending on the preference of the individual. On the other hand, the actual board has a fixed dimension of 1000 * 700.

The interface on which the chess board is can be closed by clicking the mouse on the X button on the far right corner of the interface or placing the control key (Ctrl+X). Figure 1.0 below shows the chess board without chess pieces arranged on it.

                                               Table 1.0: 2D Chess board and Chess pieces

   

                                      

           
            The figure 1.0 above shows a 2D chess board without chess pieces arranged on it.

For the game of chess, there are thirty two pieces that are arranged on the above chess board. these pieces are played by two sides (individuals or an inidividual against a computer). Figure 2.0 below shows a 2D chess board with pieces arranged on it.

                               Table 2.0: A 2D Chess board with chess pieces arranged on it

         






EXPERIENCES LEARNT
The above displayed snapshots have been produced by a program coded as part of an assessment task for the final grade for the award of a Bachelor of Sciences in Mathematical Sciences Education. At the same time, the task was given as course work for understanding how graphical user interfaces are produced using java language. As part of course work, they are several experiences learnt from the process of coding the program and at the same time compiling and running the code for the above snapshots to be produced. Some of the experiences learnt include debugging of a java code, use of the draw method, drawing of 2D diagrams on a canvas, importing of images form a file saved on a disk. These experiences are outlined below;

Debugging of a java 
Development of any computer code (program) involves the programmer making a lot of errors. These errors are both compiler and run time errors. It takes a great deal to learn and understand these errors in order to come up with a code which can run and at the same time produce the desired output. With this assignment, debugging of codes that involve the production of graphical user interfaces in two dimensions has been learnt and at the same time mastered such that using this experience, codes which produce 2D images can be produced.

Use of draw method in java
The draw method in java is responsible for generating graphic images in a drawing canvas. These images are both graphic images and the graphic 2D images.

Drawing of 2D graphic images
The chess board is a 2D graphic image generated from a graphic object. The 2D graphic image is a generated by a 2D objected which has been casted.

Importing of graphic images from a file
The chess board has been generated by the cod but the chess pieces are png images which have been saved in a file. These chess pieces have been imported and position and placed on the board for the display.



Conclusion
The major objective of the program was to develop a 2D chess board with chess pieces arranged on it (using Java Programming Language). The out put just display a chess board and the chess pieces arranged on it. There is, however, a need to develop it further so that the pieces can be moved from one position to the other in order to play the game. At the same time, some pieces have been duplicated due to shortage of other pieces hence there is a need to either have uniform pieces or improve the code so that the code ha to generate the pieces.

The Java Code
The code that generated the above snaphot is as below;

import java.awt.*;
import javax.swing.*;

public class SamChess extends JFrame {
    private JLabel myLabel;
    private JPanel myPanel;
    private DrawingPanel drawPanel;
  static SamChess myChessBoard;
  private int x = 100, y = 60;

    public SamChess() {
 
     super("CHESS BOARD AND CHESS PIECES DEVELOPED BY SAMSON C. MKANDAWIRE");
 
        myPanel = new JPanel();
        myPanel.setLayout(null);
        myPanel.setBackground(Color.GREEN);

        drawPanel= new DrawingPanel();
        drawPanel.setBounds(myPanel.getWidth() / 2, myPanel.getHeight() / 2, 1000, 1000);
        drawPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
        myPanel.add(drawPanel, BorderLayout.CENTER);
      setContentPane(myPanel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000,700);
        setLocation(10,10);
   
        setVisible(true);
      
    }

    public static void main(String[] args) {
         myChessBoard = new  SamChess();
           }

  class DrawingPanel extends JPanel {
     private int x = 100, y = 70;   

     protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         Graphics2D g2d = (Graphics2D) g;
         drawingMethod(g2d);
     }

    private void drawingMethod(Graphics2D g2d) {
 
 
     g2d.drawRect(100,75,800,500);
    
     
    g2d.setColor(Color.BLACK);
   
     g2d.setFont(new Font("Bradley Hand ITC", Font.BOLD, 20));
    g2d.drawString("  2D CHESS GAME:A product of Samson C Mkandawire-MSE/07/ME/050",100,50);
  

    g2d.fillRect(200, 75, 100, 65);
   
    ImageIcon myImage3 = new ImageIcon("f:\\Chesspieces\\chess2.png");
    Image picImage3 = myImage3.getImage();
    g2d.drawImage(picImage3,130,75, this);
   
    ImageIcon myImage = new ImageIcon("f:\\Chesspieces\\chess1.png");
    Image picImage = myImage.getImage();
    g2d.drawImage(picImage, 230,75, this);
   
    ImageIcon myImage6 = new ImageIcon("f:\\Chesspieces\\chess3.png");
    Image picImage6 = myImage6.getImage();
    g2d.drawImage(picImage6, 330,75, this);
   
      g2d.fillRect(400, 75, 100, 65);
    ImageIcon myImage8 = new ImageIcon("f:\\Chesspieces\\chess5.png");
    Image picImage8 = myImage8.getImage();
    g2d.drawImage(picImage8, 430,75, this);   
      
   
    ImageIcon myImage9 = new ImageIcon("f:\\Chesspieces\\chess2.png");
    Image picImage9 = myImage9.getImage();
   
    g2d.fillRect(100, 140, 100, 55);
    g2d.fillRect(300, 140, 100, 55);
    g2d.fillRect(500, 140, 100, 55);
      g2d.fillRect(700, 140, 100, 55);
    g2d.drawImage(picImage9, 130,142, this);   
    g2d.drawImage(picImage9, 230,142, this);   
    g2d.drawImage(picImage9, 330,142, this);   
    g2d.drawImage(picImage9, 430,142, this);   
    g2d.drawImage(picImage9, 530,142, this);  
    g2d.drawImage(picImage9, 630,142, this);   
    g2d.drawImage(picImage9, 730,142, this);
    g2d.drawImage(picImage9, 830,142, this);   
    g2d.fillRect(200, 195, 100, 65);
    g2d.fillRect(400, 195, 100, 65);
    g2d.fillRect(600, 195, 100, 65);
    g2d.fillRect(800, 195, 100, 65);  
  
    g2d.fillRect(100, 255, 100, 65);
    g2d.fillRect(300, 255, 100, 65);
    g2d.fillRect(500, 255, 100, 65);
    g2d.fillRect(700, 255, 100, 65);
   
    g2d.fillRect(200, 315, 100, 65);
    g2d.fillRect(400, 315, 100, 65);
    g2d.fillRect(600, 315, 100, 65);
    g2d.fillRect(800, 315, 100, 65);
   
    g2d.fillRect(100, 375, 100, 65);
    g2d.fillRect(300, 375, 100, 65);
    g2d.fillRect(500, 375, 100, 65);
    g2d.fillRect(700, 375, 100, 65);
      g2d.fillRect(800, 75, 100, 65);
  
    ImageIcon myImage4 = new ImageIcon("f:\\Chesspieces\\chess2.png");
    Image picImage4 = myImage4.getImage();
    g2d.drawImage(picImage4, 830,75, this);
   
    ImageIcon myImage2 = new ImageIcon("f:\\Chesspieces\\chess1.png");
    Image picImage2 = myImage2.getImage();
    g2d.drawImage(picImage2,730,75, this);
   
    g2d.fillRect(600, 75, 100, 65);
    ImageIcon myImage5 = new ImageIcon("f:\\Chesspieces\\chess3.png");
    Image picImage5 = myImage5.getImage();
    g2d.drawImage(picImage5, 630,75, this);
   
    ImageIcon myImage7 = new ImageIcon("f:\\Chesspieces\\chess4.png");
    Image picImage7 = myImage7.getImage();
    g2d.drawImage(picImage7, 530,71, this);

   g2d.fillRect(100, 505, 100, 65);
     ImageIcon myImage17 = new ImageIcon("f:\\Chesspieces\\sam2.png");
    Image picImage17 = myImage17.getImage();
     g2d.drawImage(picImage17,130,505, this);
            
   
     ImageIcon myImage18 = new ImageIcon("f:\\Chesspieces\\sam5.png");
          Image picImage18 = myImage18.getImage();
     g2d.drawImage(picImage18,130,438, this);
   
    g2d.fillRect(200, 440, 100, 65);
    g2d.fillRect(400, 440, 100, 65);
    g2d.fillRect(600, 440, 100, 65);
    g2d.fillRect(800, 440, 100, 65);
  
    g2d.drawImage(picImage18,230,438, this);
    g2d.drawImage(picImage18,330,438, this);
    g2d.drawImage(picImage18,430,438, this); 
    g2d.drawImage(picImage18,530,438, this);
    g2d.drawImage(picImage18,630,438, this);
    g2d.drawImage(picImage18,730,438, this);
    g2d.drawImage(picImage18,830,438, this);
      
  
    ImageIcon myImage28 = new ImageIcon("f:\\Chesspieces\\sam3.png");
    Image picImage28 = myImage28.getImage();
    g2d.drawImage(picImage28,230,505, this);
   
    g2d.fillRect(300, 505, 100, 65);
    ImageIcon myImage29 = new ImageIcon("f:\\Chesspieces\\sam1.png");
    Image picImage29 = myImage29.getImage();
    g2d.drawImage(picImage29,330,505, this);
   
    ImageIcon myImage25 = new ImageIcon("f:\\Chesspieces\\sam2.png");
    Image picImage25 = myImage25.getImage();
    g2d.drawImage(picImage25,830,505, this);
  
   
    g2d.fillRect(700, 505, 100, 65);
    ImageIcon myImage27 = new ImageIcon("f:\\Chesspieces\\sam3.png");
    Image picImage27 = myImage27.getImage();
    g2d.drawImage(picImage27,730,505, this);
   
      
      ImageIcon myImage30 = new ImageIcon("f:\\Chesspieces\\sam1.png");
    Image picImage30 = myImage30.getImage();
    g2d.drawImage(picImage30,630,505, this);
   
    g2d.fillRect(500, 505, 100, 65);
    ImageIcon myImage31 = new ImageIcon("f:\\Chesspieces\\sam4.png");
    Image picImage31 = myImage31.getImage();
    g2d.drawImage(picImage31,530,505, this);
   
    ImageIcon myImage32 = new ImageIcon("f:\\Chesspieces\\sam6.png");
    Image picImage32 = myImage32.getImage();
    g2d.drawImage(picImage32,430,505, this);
     
    }
   }
 }
  
 

No comments:

Post a Comment