Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 9 123 ... LastLast
Results 1 to 25 of 219

Thread: Chess Program

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Chess Program

    Guys,

    I am developing a chess manager program(not an interactive game)!

    I have the following classes in my model package

    ChessPieces
    ChessBoard contains Squares in an ArrayList and chessPieces in an ArrayList
    Square

    Also in my view package i have a chessboardview

    Its a JPanel ill post the code below.
    Is there a way the get all the 64 squares in my JPanel with ImageIcon?
    CHESSBOARD
    package model;
     
    import java.util.Set;
    import java.util.ArrayList;
     
    import javax.swing.ImageIcon;
     
    public class ChessBoard {
     
        private Set<ChessPiece> chessPieces;
        private ArrayList<Square> squares;
        private static final String[] LOCATIONS = {
        "A1","B1","C1","D1","E1","F1","G1","H1",
        "A2","B2","C2","D2","E2","F2","G2","H2",
        "A3","B3","C3","D3","E3","F3","G3","H3",
        "A4","B4","C4","D4","E4","F4","G4","H4",
        "A5","B5","C5","D5","E5","F5","G5","H5",
        "A6","B6","C6","D6","E6","F6","G6","H6",
        "A7","B7","C7","D7","E7","F7","G7","H7",
        "A8","B8","C8","D8","E8","F8","G8","H8",
        };
     
        public ChessBoard(){ 
            squares = new ArrayList<Square>();
            addLocations();
            setSquareColors();
        }
     
        private void addLocations(){
            for(int i = 0; i < LOCATIONS.length; i++){
            	squares.add(new Square(LOCATIONS[i]));    
            }       
        }
     
        private void setSquareColors(){
        	for(int i = 0; i < LOCATIONS.length; i++){
        	    squares.get(i).setColor("BROWN");
        	}
        	for(int i = 1; i < LOCATIONS.length-1; i++){
        	    squares.get(i).setColor("WHITE");
        	}
        }
     
        public Square getSquare(int i){
            return squares.get(i);
        }
     
        public String getSquareColor(int i){
            return squares.get(i).getColor();
        }
     
        public ArrayList<Square> getAllSquares(){
        	return squares;
        }
     
        public void setSquareImage(int i, ImageIcon image){
        	getSquare(i).setImage(image);
        }
    }

    SQUARE
    package model;
     
    import view.assets.*;
     
    import java.util.ArrayList;
     
    import javax.swing.ImageIcon;
     
    public class Square{
     
    	private String location;
    	private String squareColor;
    	private ImageIcon image;
    	private ArrayList<ChessPiece> chessPieces;
     
    	private boolean empty;
     
    	public Square(String location){
    		this.location = location;
    		empty = true;
    	}
     
    	public String getLocation(){
    		return location;
    	}
     
    	public void setImage(ImageIcon image){
    		this.image = image;
    	}
     
    	public ImageIcon getImage(){
    		return image;
    	}
     
    	public boolean isEmpty(){
    		return empty;	
    	}
     
    	public void setColor(String color){
    		squareColor = color;
    	}
     
    	public String getColor(){
    		return squareColor;
    	}
    }

    CHESSBOARDVIEW
    package view;
     
    import java.awt.*;
     
    import java.util.ArrayList;
     
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
     
    import controller.Controller;
    import model.ChessBoard;
    import model.Square;
     
     
    public class ChessBoardView extends JPanel
    {
        //intialize variables
    	private final static ImageIcon DARK_BROWN = new ImageIcon 
    	(ChessBoardView.class.getResource("assets/sqb.gif"));
    	private final static ImageIcon LIGHT_BROWN = new ImageIcon 
        (ChessBoardView.class.getResource("assets/sqw.gif"));
    	//intialize components
    	private JPanel southPanel = new JPanel();
    	private JPanel westPanel = new JPanel();
     
    	//intialize arrays to hold panels and images of the board;
    	private JLabel[] labels = new JLabel[64];
     
    	private ChessBoard chessboard;
     
     
    	public ChessBoardView (Controller controller){
    		chessboard = new ChessBoard();
    		createGUI();
        }
     
    	 private void createGUI() {
    		Dimension boardSize = new Dimension(200, 200);
     
    	    setLayout( new GridLayout(8, 8) );
    	    setPreferredSize( boardSize );
    	    setBounds(10, 10, boardSize.width, boardSize.height);
    	    setBorder(BorderFactory.createLineBorder(Color.BLACK));
     
    	    addImageToSquare();
    	    addSquaresToContentPane();
    	}
     
    	private void addImageToSquare() {
     
    			for(int i = 0; i < 64; i++){
    				if (chessboard.getSquare(i).getColor().equals("WHITE")){
    					chessboard.getSquare(i).setImage(LIGHT_BROWN);
    				}
    				else if (chessboard.getSquare(i).getColor().equals("BROWN")){
    					chessboard.getSquare(i).setImage(DARK_BROWN);
    				}
    			}
    	}
     
    	private void addSquaresToContentPane(){
     
     
     
     
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Location
    uk
    Posts
    15
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Default Re: Chess Program

    hello
    try to make the class square of type jpanel and override the paintComponent method to place the images in it. then you should be able to place as many squares as you want in the chess board.
    thanks

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    What happens when you use the GridLayout class? It could be used to build a 8x8 grid of JPanels that could hold images.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    can you code an example for me?

    --- Update ---

    8x8 grid of JPanels that could hold images.
    i all ready have that right?

    setLayout( new GridLayout(8, 8) );
    setPreferredSize( boardSize );
    setBounds(10, 10, boardSize.width, boardSize.height);
    setBorder(BorderFactory.createLineBorder(Color.BLACK));

    this layout sets the jpanel layout from the class chessboardview. It is actually a super to extends jpanel
    i really cant get the squares in a 64 panel which contains the squares with location an image to represent a chess board

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Your code is close.
    Have the Square class extend JLabel and set its icon to the image you want displayed in that square.
    Then add the Square class objects to the grid layout you have.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    Okay i made my square class JLabel what next? I have 64 different square objects, each with different locations.

    import javax.swing.JLabel;
     
    public class Square extends JLabel{

    but what next to do in the chessboardview class? how to add all 64 squares in the right position

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Use the add() method in a loop to add the Squares to the JPanel with the grid layout.

    For debugging:
    Add a toString() method to the Square class. When the squares object is printed, the toString method will be called and what it returns will be shown.
    Also use the JLabel class's setText() method to display the squares location in the GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    i need something like
    for (int i = 0; i < 64; i++) {
     
     
    	            int row = (i / 8) % 2;
    	            if (row == 0)

    but what next? to add 64 squares alternately coloured brown and white

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Work out the logic using loops and if statements to control which color is assigned to each square.
    Do the location values get set correctly? Should the grid look like this:
    a1 a2 a3 ...
    b1 b2 b3 ...
    ...
    h1 h2 h3 ...
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    the location value and the color and image are set correctly the arraylist contains 64 squares with string location, string color, ImageIcon image. Now in the chessboardview which extends JPanel(chess board) i need to implement those 64 squares in the JPanel which has a gridlayout(8,8) but i i have no idea how? do i have to make 64 JLabels and how can i place them in the right order etc.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    add to the grid layout using loops and an index that goes from 0 to 63 to get the value from the LOCATIONS array.
    Have you tried a simple loop to add the 64 Square objects to the grid to see what happens? When you see where the squares are located in the GUI then you can change the logic so that they are correctly positioned.

    the location value and the color and image are set correctly the arraylist
    How do you know that? What prints out when the squares arraylist is printed?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    you can change the logic so that they are correctly positioned.
    how do i change a grid logic so the labels are set correctly?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    How are they being loaded now? What order are they in?
    Given their current location/order, a way can be found to change the order to the order you want.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    i dont know how to change a gridlayout so it contains 64 jlabels in the right order

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    What order are the Jlabels now? You need to describe what is happening now to know what to change to fix it.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    what do you mean by what order? thanks for you help!
    public class ChessBoardView extends JPanel
    {
        //intialize variables
    	private final static ImageIcon DARK_BROWN = new ImageIcon 
    	(ChessBoardView.class.getResource("assets/sqb.gif"));
    	private final static ImageIcon LIGHT_BROWN = new ImageIcon 
        (ChessBoardView.class.getResource("assets/sqw.gif"));
    	//intialize components
    	private JPanel southPanel = new JPanel();
    	private JPanel westPanel = new JPanel();
     
    	//intialize arrays to hold panels and images of the board;
    	private JLabel[] labels = new JLabel[64];
     
    	private ChessBoard chessboard;
     
     
    	public ChessBoardView (Controller controller){
    		chessboard = new ChessBoard();
    		createGUI();
        }
     
    	 private void createGUI() {
    		Dimension boardSize = new Dimension(200, 200);
     
    	    setLayout( new GridLayout(8, 8) );
    	    setPreferredSize( boardSize );
    	    setBounds(10, 10, boardSize.width, boardSize.height);
    	    setBorder(BorderFactory.createLineBorder(Color.BLACK));
     
    	    addImageToSquare();
    	    addSquaresToContentPane();
    	}
     
    	private void addImageToSquare() {
     
    			for(int i = 0; i < 64; i++){
    				if (chessboard.getSquare(i).getColor().equals("WHITE")){
    					chessboard.getSquare(i).setImage(LIGHT_BROWN);
    				}
    				else if (chessboard.getSquare(i).getColor().equals("BROWN")){
    					chessboard.getSquare(i).setImage(DARK_BROWN);
    				}
    			}
    	}
     
    	private void addSquaresToContentPane(){
     
     
     
     
    	}
    }


    --- Update ---

    how do i place 64 squares in the right order?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    what do you mean by what order?
    What do you see in the GUI? Are all 64 JLabels shown? If not, which are shown and which are missing?
    What JLabels are in the first row? A1 A2 A3 ... or A1 B1 C1 ...
    What JLabels are in the next row?
    What JLabels are in the first column?

    how do i place 64 squares in the right order?
    What needs to be changed?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    I dont know which JLabels
    public class ChessBoard {
     
        private Set<ChessPiece> chessPieces;
        private ArrayList<Square> squares;
        private static final String[] LOCATIONS = {
        "A1","B1","C1","D1","E1","F1","G1","H1",
        "A2","B2","C2","D2","E2","F2","G2","H2",
        "A3","B3","C3","D3","E3","F3","G3","H3",
        "A4","B4","C4","D4","E4","F4","G4","H4",
        "A5","B5","C5","D5","E5","F5","G5","H5",
        "A6","B6","C6","D6","E6","F6","G6","H6",
        "A7","B7","C7","D7","E7","F7","G7","H7",
        "A8","B8","C8","D8","E8","F8","G8","H8",
        };
     
        public ChessBoard(){ 
            squares = new ArrayList<Square>();
            addLocations();
            setSquareColors();
        }
     
        private void addLocations(){
            for(int i = 0; i < LOCATIONS.length; i++){
            	squares.add(new Square(LOCATIONS[i]));    
            }       
        }
     
        private void setSquareColors(){
        	for(int i = 0; i < LOCATIONS.length; i++){
        	    squares.get(i).setColor("BROWN");
        	}
        	for(int i = 1; i < LOCATIONS.length-1; i++){
        	    squares.get(i).setColor("WHITE");
        	}
        }

    Screen Shot 2013-08-12 at 2.56.21 AM.jpg

  19. #19
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    	private void addImageToSquare() {
     
    			for(int i = 0; i < 64; i++){
    				if (chessboard.getSquare(i).getColor().equals("WHITE")){
    					chessboard.getSquare(i).setImage(LIGHT_BROWN);
    				}
    				else if (chessboard.getSquare(i).getColor().equals("BROWN")){
    					chessboard.getSquare(i).setImage(DARK_BROWN);
    				}
    			}
    	}
     
    	private void addSquaresToContentPane(){
    		super.add(new JLabel (chessboard.getSquare(0).getImage()));
     
     
    		}
     
    	}

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    I dont know which JLabels
    Did you try this? It will show the LOCATION value next to the image.
    For debugging:
    Add a toString() method to the Square class. When the squares object is printed, the toString method will be called and what it returns will be shown.
    Also use the JLabel class's setText() method to display the squares location in the GUI.

    Where does the code call the add() method to add the squares to the grid?

    Here's what I see:
    Attached Images Attached Images
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    gokuball (August 11th, 2013)

  22. #21
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    how did you get that? which code?

     private void createGUI() {
    		Dimension boardSize = new Dimension(200, 200);
     
    	    setLayout( new GridLayout(8, 8) );
    	    setPreferredSize( boardSize );
    	    setBounds(10, 10, boardSize.width, boardSize.height);
    	    setBorder(BorderFactory.createLineBorder(Color.BLACK));
     
    	    addImageToSquare();
    	    addSquaresToContentPane();
    	}

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    I added a call to the add() method to add the squares to the container that is using the gridlayout.
    Its in a loop that goes through all 64 squares.
    private void addImageToSquare() {
     
    			for(int i = 0; i < 64; i++){
    at the end of this loop.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    can you post the code?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Chess Program

    Its some thing like this. A simple call to the add() method with a reference to a Square object:
    add(referenceToSquare);
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Chess Program

    which method are you talking about?

    --- Update ---

    can you please post the code you made to get to the point of the grid with all the labels and icons in it

Page 1 of 9 123 ... LastLast

Similar Threads

  1. Simple chess program (no AI) - Need help with structuring my code
    By MineeMo in forum Object Oriented Programming
    Replies: 6
    Last Post: June 18th, 2012, 10:12 AM
  2. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 08:42 PM
  3. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  4. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 12:07 PM
  5. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM