The BoardDrawer: A Visual Representation

The board is functional, and being the enthusiastic person that I am, I've already seen it on the screen on an applet that I created to open the 16 images and draw them for me in 35 unit rectangles forming the 8x8 array that is the board. I got the images out of the images folder of one of the chess programs that I've had for a long time. Seeing the game for the first time made me think, "it was all worth it". It also made me realize how powerful programming really is. Anyone can design and create his old world with his own rules with his own graphics, and it can be as simple or complex as he wishes. Instantly one can become a creator and destroyer, an allocator of scarce resources, and a king of his own kingdom. When I first entered computer science I failed to see the power that it contains and the boundlessness of its interval of creation. With programming you're limited only by your imagination and your ability to think the unthinkable. It is there that the artist is able to design complex solutions to any problem anyone could ever possibly fathom.

On this note I present to you the concept of the BoardDrawer, which is the manifestion of every idea in the program so far. In JAVA, it is a component to be added to the applet in anyway choosen. The BoardDrawer draws the board and everything related to it. As you will see, with the proper knowledge drawing the board is simple, and so is moving the pieces.

What You Need To Know Before You Draw The Board

The screen on the computer you're using right now and every program that you've ever used has every component on it mapped out by a pre-defined coordinate system that curiously resembles a cartesian coordinate plane in that every point has an x and y value that maps to a definite location on the screen. A component can be as simple as a button, as remedial as the text area you use when you're typing documents in word, or as complex as a menu bar. They're all mapped out on the screen using age-old methods developed by some ingenious prehistoric programmers.

The reason that you need to know that is this; in order to draw your board and move your pieces you have to use that coordinate system. In JAVA it's easy because the super component(the container class that your component resides in) has functions to tell you this information and based on that, you can alter the size and position of you board. But, I'm not to familiar with the systems of other languages yet.

What you need to know before drawing your board is this. You need to know where you're going to start drawing in the x-direction and y-direction. You need to know how big each square will be. You need to know how big the board will be. And, you need to know how big your pieces are. If you know this, then you have your board sufficiently mapped out. Drawing it and moving pieces around it will involve simple manipulations of these coordinates.

Drawing the Board

Drawing the board requires a loop that iterates through boards array and decides whether to draw a piece or not. If it does draw a piece than it must load that pieces image and draw the image within the correct square, using prescribed coordinates. In my program, I place all of the images in a 2x6 Image array. The first dimension is for the color and the second for the type of piece. My board is sufficiently mapped out and i use seperate functions to get the square coordinates and the piece coordinates.

    public void drawBoard(Graphics g){
        for(int i=0; i<8; i++){
            for(int j=0; j<8; j++){
                g.setColor(getSquareColor(i, j));
                g.drawRect(getSquareX(i), getSquareY(j), iCellSize, iCellSize, this);
                if(not(board[i][j].getColor() == Board.COLORLESS)
                    g.drawImage(Image[board[i][j].getColor()][board[i][j].getKind()], getPieceX(i), getPieceY(j));
            }
        }
    }

Now I endulge you in the another of the complexities of my chess program, which, aside from the abstract board array, is the hidden secret of how all chess programs manage a user interface. I will not go to deep into it yet, since there is more that needs to be added to this program before the boarddrawer is ready to tell all of its secrets.

Algorithms for the BoardDrawer Placement in a Container
The BoardDrawer as a Class
Looking Ahead