Saturday, October 9, 2010



HOME PAGE

DESIGNING OF CHESSBOARD INTERFACE







The main purpose of the assignment was to design the chessboard interface using the tools learnt in the  course of Programming Visual Interfaces with java. The chessboard interface includes the following categories of images(Playing Items):  Queen,King, Bishop, Horse, Castle and Pawn. 

The interface has been developed mainly by using the graphics tools  available in java packages.The graphics toolbox within the Abstract Windowing Toolkit (AWT) made it possible for me draw simple geometric shapes and position images within the borders of the panel.  From this assignment, I have learnt that Graphics class provides the framework for all graphics operations within the AWT. The Graphics class provides methods for drawing simple geometric shapes, text, and images to the graphics destination.

I have also learnt that the Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering two-dimensional shapes, text and images.


To draw the grid of lines that form different rectangles, I have used the drawLine() method that connects different lines and form a grid of rows and columns. This method requires the Graphics2D object and it requires four parameters. The first and second parameters represent the starting point (X, Y) of the line to be drawn and the last two parameters represent the end point of the line.


To distinguish the black and white boxes, I have used the fillRect() method to specify those rectangular boxes that are to be filled with black color. This method requires the Graphics2D object and it requires four parameters.The first and second parameters represent the starting point (X, Y) of the line to be drawn and the last two parameter represent the length and width of the rectangle.

I have also used the ImageIcon class to create image objects and then be able to draw those images inside the rectangular boxes on the panel. This has helped to load and draw images of the chess items like king, queen, etc. The only method from this class that I have used is drawImage()   which gets image from the specified location("c:\\Dogs\\imagename.png") and  places it on the specified location that is specified within its argument.

THE SOURCE CODE IN JAVA PROGRAMMING LANGUAGE
import java.awt.*;
import javax.swing.*;

public class Chess_Innocent_Patrick extends JFrame {

    private JPanel mainPanel;
    private DrawingPanel drawPanel;
     static Chess_Innocent_Patrick myChess;
     private int x = 100, y = 60;

    public Chess_Innocent_Patrick() {
   
           super("This Chess Board is developed by Innocent Patrick Mapwetekere");
   
        mainPanel = new JPanel();
        mainPanel.setLayout(null);
        mainPanel.setBackground(Color.BLUE);

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

    }

    public static void main(String[] args) {
         myChess = new  Chess_Innocent_Patrick();
           }


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

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

       private void drawingMethod(Graphics2D g2d) {
          
            for (int i = 0; i <= 8; ++i) {
                g2d.drawLine(x, (y * i) + 60, x + 800, (y * i) + 60);
                }
           
         for (int j = 0; j <= 8; ++j) {
                    g2d.drawLine((x * j) + 100, y, (x * j) + 100, y + 480);
               }
      
          g2d.setColor(Color.BLACK);
          g2d.fillRect(200, 60, 100, 60);         
       
          ImageIcon myImage3 = new ImageIcon("c:\\Dogs\\chess2.png");
          Image picImage3 = myImage3.getImage();
          g2d.drawImage(picImage3,130,65, this);
         
          ImageIcon myImage = new ImageIcon("c:\\Dogs\\chess1.png");
          Image picImage = myImage.getImage();
          g2d.drawImage(picImage, 230,65, this);
         
          ImageIcon myImage6 = new ImageIcon("c:\\Dogs\\chess3.png");
          Image picImage6 = myImage6.getImage();
          g2d.drawImage(picImage6, 330,65, this);
         
         g2d.fillRect(400, 60, 100, 60);
          ImageIcon myImage8 = new ImageIcon("c:\\Dogs\\chess5.png");
          Image picImage8 = myImage8.getImage();
          g2d.drawImage(picImage8, 430,61, this);         
                  
         
          ImageIcon myImage9 = new ImageIcon("c:\\Dogs\\chess2.png");
          Image picImage9 = myImage9.getImage();
         
          g2d.fillRect(100, 120, 100, 60);
          g2d.fillRect(300, 120, 100, 60);
          g2d.fillRect(500, 120, 100, 60);
         g2d.fillRect(700, 120, 100, 60);

          g2d.drawImage(picImage9, 130,125, this);         
          g2d.drawImage(picImage9, 230,125, this);         
          g2d.drawImage(picImage9, 330,125, this);         
          g2d.drawImage(picImage9, 430,125, this);         
          g2d.drawImage(picImage9, 530,125, this);       
          g2d.drawImage(picImage9, 630,125, this);         
          g2d.drawImage(picImage9, 730,125, this);
          g2d.drawImage(picImage9, 830,125, this);           

          g2d.fillRect(200, 180, 100, 60);
          g2d.fillRect(400, 180, 100, 60);
          g2d.fillRect(600, 180, 100, 60);
          g2d.fillRect(800, 180, 100, 60);           
       
          g2d.fillRect(100, 240, 100, 60);
          g2d.fillRect(300, 240, 100, 60);
          g2d.fillRect(500, 240, 100, 60);
          g2d.fillRect(700, 240, 100, 60);
           
          g2d.fillRect(200, 300, 100, 60);
          g2d.fillRect(400, 300, 100, 60);
          g2d.fillRect(600, 300, 100, 60);
          g2d.fillRect(800, 300, 100, 60);
           
          g2d.fillRect(100, 360, 100, 60);
          g2d.fillRect(300, 360, 100, 60);
          g2d.fillRect(500, 360, 100, 60);
          g2d.fillRect(700, 360, 100, 60);

         g2d.fillRect(800, 60, 100, 60);
           
          ImageIcon myImage4 = new ImageIcon("c:\\Dogs\\chess2.png");
          Image picImage4 = myImage4.getImage();
          g2d.drawImage(picImage4, 830,65, this);
         
          ImageIcon myImage2 = new ImageIcon("c:\\Dogs\\chess1.png");
          Image picImage2 = myImage2.getImage();
          g2d.drawImage(picImage2,730,65, this);
         
          g2d.fillRect(600, 60, 100, 60);
          ImageIcon myImage5 = new ImageIcon("c:\\Dogs\\chess3.png");
          Image picImage5 = myImage5.getImage();
          g2d.drawImage(picImage5, 630,65, this);
         
          ImageIcon myImage7 = new ImageIcon("c:\\Dogs\\chess4.png");
          Image picImage7 = myImage7.getImage();
          g2d.drawImage(picImage7, 530,61, this);


                   
          ImageIcon myImage17 = new ImageIcon("c:\\Dogs\\chess6.png");
          Image picImage17 = myImage17.getImage();
          g2d.drawImage(picImage17,130,428, this);
         
          g2d.fillRect(200, 420, 100, 60);
          g2d.fillRect(400, 420, 100, 60);
          g2d.fillRect(600, 420, 100, 60);
          g2d.fillRect(800, 420, 100, 60);
          g2d.fillRect(100, 480, 100, 60);
             
          g2d.drawImage(picImage17,230,428, this);
          g2d.drawImage(picImage17,330,428, this);
          g2d.drawImage(picImage17,430,428, this);       
          g2d.drawImage(picImage17,530,428, this);
          g2d.drawImage(picImage17,630,428, this);
          g2d.drawImage(picImage17,730,428, this);
          g2d.drawImage(picImage17,830,428, this);
          g2d.drawImage(picImage17,130,486, this);
         
       
          ImageIcon myImage28 = new ImageIcon("c:\\Dogs\\chess7.png");
          Image picImage28 = myImage28.getImage();
          g2d.drawImage(picImage28,230,486, this);
         
          g2d.fillRect(300, 480, 100, 60);
          ImageIcon myImage29 = new ImageIcon("c:\\Dogs\\chess8.png");
          Image picImage29 = myImage29.getImage();
          g2d.drawImage(picImage29,330,486, this);
         
          ImageIcon myImage25 = new ImageIcon("c:\\Dogs\\chess6.png");
          Image picImage25 = myImage25.getImage();
          g2d.drawImage(picImage25,830,486, this);
       
          g2d.fillRect(700, 480, 100, 60);
          ImageIcon myImage27 = new ImageIcon("c:\\Dogs\\chess7.png");
          Image picImage27 = myImage27.getImage();
          g2d.drawImage(picImage27,730,486, this);
         
          ImageIcon myImage30 = new ImageIcon("c:\\Dogs\\chess8.png");
          Image picImage30 = myImage30.getImage();
          g2d.drawImage(picImage30,630,486, this);
         
          g2d.fillRect(500, 480, 100, 60);
          ImageIcon myImage31 = new ImageIcon("c:\\Dogs\\chess9.png");
          Image picImage31 = myImage31.getImage();
          g2d.drawImage(picImage31,530,480, this);
         
          ImageIcon myImage32 = new ImageIcon("c:\\Dogs\\chess10.png");
          Image picImage32 = myImage32.getImage();
          g2d.drawImage(picImage32,430,480, this);
             
             }
         }
    }