Results 1 to 5 of 5
  1. #1
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Question Chess Piece Painting Location on Board

    Hello, I am working on a chess GUI and so far i have gotten the board drawn out, and the pieces, but they do not display properly. I want them
    to display with the black pieces on the top two rows, and the white on the bottom two rows. However, this is how they appear:
    Chess Piece Painting Location on Board-chessfail.png

    I cannot find what makes them appear like this, and the code looks ok to me, but I guess i am not seeing something. Maybe you guys can figure it out.
    Here is the code:

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Chess extends JApplet
    {
        private Container table;
        		JPanel[][] board;
        		int[][] pieces;
        private JPanel under;
        private int k;
        private int turn;
        private int count;
        private boolean firstTurn;
    
        public void init()
        {
            table = getContentPane();
            table.setSize(800, 800);
            table.setLayout(null);
            k=50;
            firstTurn=true;
            resetBoard();
            turn = 0;
        }
    
        public void resetBoard()
        {
            if (firstTurn) 
            	turn =0;
            else 
            	turn=1;
            
            firstTurn=false;
            
            table.setBackground(new Color(222, 184, 135));
            table.removeAll();
            
            board = new JPanel[8][8];
            count = 0;
            
            //Creates the board squares as JPanels
            for (int r=0; r<8; r++)
            {
               count++;
            	for (int c=0; c<8; c++)
                {
                    count++;
                    
                	board[r][c] = new JPanel();
                    board[r][c].setSize(k, k);
                    table.add(board[r][c]);
                    board[r][c].setLocation(35 +(k+1)*(r)+1, 100+(k+1)*(c)+1);
                    
                    if(count % 2 == 0)
                    	board[r][c].setBackground(new Color(250, 250, 205));
                    else
                    	board[r][c].setBackground(Color.gray);
                }
            }
            under = new JPanel();
            under.setSize(409, 409);
            table.add(under);
            under.setLocation(35, 100);
            under.setBackground(Color.black);
            
           
            //Adds the chess pieces as JLabels to the JPanels
            for(int i = 0; i < 8; i++)
            {
            	for(int j = 0; j < 8; j++)
            	{
            		if(piece[i][j] == 0)
            		{
            			JLabel empty = new JLabel();
            			board[i][j].add(empty);
            		}
            		else if(piece[i][j] == 1)
            		{
            			JLabel wpawn = new JLabel(WPAWN);
            			board[i][j].add(wpawn);
            		}
            		else if(piece[i][j] == 2)
            		{
            			JLabel wknight = new JLabel(WKNIGHT);
            			board[i][j].add(wknight);
            		}
            		else if(piece[i][j] == 3)
            		{
            			JLabel wbishop = new JLabel(WBISHOP);
            			board[i][j].add(wbishop);
            		}
            		else if(piece[i][j] == 4)
            		{
            			JLabel wrook = new JLabel(WROOK);
            			board[i][j].add(wrook);
            		}
            		else if(piece[i][j] == 5)
            		{
            			JLabel wqueen = new JLabel(WQUEEN);
            			board[i][j].add(wqueen);
            		}
            		else if(piece[i][j] == 6)
            		{
            			JLabel wking = new JLabel(WKING);
            			board[i][j].add(wking);
            		}
            		else if(piece[i][j] == -1)
            		{
            			JLabel bpawn = new JLabel(BPAWN);
            			board[i][j].add(bpawn);
            		}
            		else if(piece[i][j] == -2)
            		{
            			JLabel bknight = new JLabel(BKNIGHT);
            			board[i][j].add(bknight);
            		}
            		else if(piece[i][j] == -3)
            		{
            			JLabel bbishop = new JLabel(BBISHOP);
            			board[i][j].add(bbishop);
            		}
            		else if(piece[i][j] == -4)
            		{
            			JLabel brook = new JLabel(BROOK);
            			board[i][j].add(brook);
            		}
            		else if(piece[i][j] == -5)
            		{
            			JLabel bqueen = new JLabel(BQUEEN);
            			board[i][j].add(bqueen);
            		}
            		else if(piece[i][j] == -6)
            		{
            			JLabel bking = new JLabel(BKING);
            			board[i][j].add(bking);
            		}
            		else{}
            	}
            } 
            table.validate();
        }
     
       
        // Returns an ImageIcon, or null if the path is invalid. 
        // Gets images
        protected static ImageIcon createImageIcon(String path, String description) 
        {
            java.net.URL imgURL = Chess.class.getResource(path);
            if (imgURL != null) 
            {
                return new ImageIcon(imgURL, description);
            }
            else
            {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
        
            //Loads images from file
        	ImageIcon BBISHOP = createImageIcon("http://www.java-forums.org/images/bb.gif", "Black Bishop");
            ImageIcon BKING = createImageIcon("http://www.java-forums.org/images/bk.gif", "Black King");
            ImageIcon BKNIGHT = createImageIcon("http://www.java-forums.org/images/bn.gif", "Black Knight");
            ImageIcon BPAWN = createImageIcon("http://www.java-forums.org/images/bp.gif", "Black Pawn");
            ImageIcon BQUEEN = createImageIcon("http://www.java-forums.org/images/bq.gif", "Black Queen");
            ImageIcon BROOK = createImageIcon("http://www.java-forums.org/images/br.gif", "Black Rook");
            ImageIcon WBISHOP = createImageIcon("http://www.java-forums.org/images/wb.gif", "White Bishop");
            ImageIcon WKING = createImageIcon("http://www.java-forums.org/images/wk.gif", "White King");
            ImageIcon WKNIGHT = createImageIcon("http://www.java-forums.org/images/wn.gif", "White Knight");
            ImageIcon WPAWN = createImageIcon("http://www.java-forums.org/images/wp.gif", "White Pawn");
            ImageIcon WQUEEN = createImageIcon("http://www.java-forums.org/images/wq.gif", "White Queen");
            ImageIcon WROOK = createImageIcon("http://www.java-forums.org/images/wr.gif", "White Rook");
        	
        
        
         	
            //The number representations of the chess pieces
            
        	/*-6 = Black King		6 = White King
         	 *-5 = Black Queen		5 = White Queen
         	 *-4 = Black Rook		4 = White Rook
         	 *-3 = Black Bishop		3 = White Bishop
         	 *-2 = Black Knight		2 = White Knight
         	 *-1 = Black Pawn		1 = White Pawn
         	 *
         	 * 0 = EMPTY
         	 */
        	
            //piece layout
            int[][] piece = {  
         	    {-4,-2,-3,-5,-6,-3,-2,-4},
        		{-1,-1,-1,-1,-1,-1,-1,-1},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{1, 1, 1, 1, 1, 1, 1, 1},
        		{4, 2, 3, 5, 6, 3, 2, 4},
         		};
    }
    Probably, the location of the problem might be here:

    Java Code:
    //Adds the chess pieces as JLabels to the JPanels
            for(int i = 0; i < 8; i++)
            {
            	for(int j = 0; j < 8; j++)
            	{
            		if(piece[i][j] == 0)
            		{
            			JLabel empty = new JLabel();
            			board[i][j].add(empty);
            		}
            		else if(piece[i][j] == 1)
            		{
            			JLabel wpawn = new JLabel(WPAWN);
            			board[i][j].add(wpawn);
            		}
            		.....
    Where it is reading the piece array:

    Java Code:
     //piece layout
            int[][] piece = {  
         	    {-4,-2,-3,-5,-6,-3,-2,-4},
        		{-1,-1,-1,-1,-1,-1,-1,-1},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{0, 0, 0, 0, 0, 0, 0, 0},
        		{1, 1, 1, 1, 1, 1, 1, 1},
        		{4, 2, 3, 5, 6, 3, 2, 4},
         		};
    Last edited by 5myl; 01-02-2012 at 12:01 AM. Reason: Clarify Possible location of problem

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Chess Piece Painting Location on Board

    You appear to be making the board array have elements that correspond to the piece array correctly. By "correctly" I mean you haven't confused row and column indexes (very easy to do - consider using r and c index variables throughout to make the intention clearer and consistent).

    So you might want to check that you are setting the location of the board panels correctly. Use System.out.println() for this:

    Java Code:
    for (int r=0; r<8; r++)
    {
        count++;
        for (int c=0; c<8; c++)
        {
            count++;
                     
            board[r][c] = new JPanel();
            board[r][c].setSize(k, k);
            table.add(board[r][c]);
            
            //board[r][c].setLocation(35 +(k+1)*(r)+1, 100+(k+1)*(c)+1);
            int xPos = 35 +(k+1)*(r)+1;
            int yPos = 100+(k+1)*(c)+1);
            System.out.println("At row number " + r + ", column number " + c);
            System.out.println("About to set position of panel to x=" + xPos + ",y=" + yPos):
            System.out.println("----------------");         
            board[r][c].setLocation(xPos, yPos);
    
            if(count % 2 == 0)
                board[r][c].setBackground(new Color(250, 250, 205));
            else
                board[r][c].setBackground(Color.gray);
        }
    }

    board[r][c].setLocation(35 +(k+1)*(r)+1, 100+(k+1)*(c)+1);

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Chess Piece Painting Location on Board

    Probably, the location of the problem might be here:
    Again, use System.out.println() to check. Ie print the values of i and j and a small string corresponding to the piece you are adding. Make sure you are clear what i and j are supposed to be. (I called them "row number" and "column number" above).

    -----

    That code cries out for a switch statement.

  4. #4
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Re: Chess Piece Painting Location on Board

    Alright, thanks... It turns out that the row(r) and column(c) variables needed to be switched to represent column(c) for the xPosition and row(r) for the yPosition.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Chess Piece Painting Location on Board

    You're welcome.

Similar Threads

  1. making a chess board gui
    By yemista in forum AWT / Swing
    Replies: 11
    Last Post: 10-13-2011, 07:05 PM
  2. Replies: 2
    Last Post: 06-29-2011, 10:08 AM
  3. Java Chess piece Array
    By obious in forum New To Java
    Replies: 1
    Last Post: 05-04-2011, 02:20 PM
  4. Working out chess piece location / piece name
    By danborgir in forum New To Java
    Replies: 5
    Last Post: 04-20-2011, 11:14 AM
  5. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •