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 2 of 9 FirstFirst 1234 ... LastLast
Results 26 to 50 of 219

Thread: Chess Program

  1. #26
    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

    which method are you talking about?
    I thought my post said the Container class's add() method.

    Can you explain what problems you are having using the add() method?
    You must already use the add() method in your GUI to show components in a JFrame's window.
    Its the same method, used the same way. The container you are adding to is the ChessBoardView that extends JPanel (a Container).

    Your code gets references to Square objects using the ChessBoard class's getSquare() method.

    You need to add all 64 Square objects to the container with gridlayout. Use a loop that goes around 64 times.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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 fixed it! the squares are in the right position. But how do i set the color right for each square. A1 = brown for instance B1 = white C1 = brown etc

  3. #28
    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 is the pattern for the colors?
    What are the rules for setting the colors?
    How are they set for the first row?
    How are they set for the second row?

    One way would to be to use nested loops. The outer loop would go through the rows, the inner loop would go over the squares on a row.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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 is the pattern for the colors?
    What are the rules for setting the colors?
    How are they set for the first row?
    How are they set for the second row?
    previous page shows the code of the classes

    One way would to be to use nested loops. The outer loop would go through the rows, the inner loop would go over the squares on a row.
    and how do i do that? the gridlayout doesn't know which row or collum i am in right?
    thanks for you help!

    --- Update ---

    I go to test all squares on location with the method .matches("A1|C1") etc and give them a color

  5. #30
    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

    gridlayout doesn't know which row or collum i am in
    There is an equation you can use to convert indexes for a two dim array to that for a one dim array:
    oneDimIdx = rowidx * nbrColsInRow + colIdx;

    There is a similar equation for converting a one dim index to two dim indexes. It involves using: % or /

    Have you worked out the algorithm/rules for setting the colors of the squares?
    What color is the first square on the first row?
    What color is the next square on the first row;
    etc for all the squares on the first row.
    What color is the first square on the second row?
    etc for the rest of the squares on that row
    etc for the rest of the rows
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    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 setSquareColors(){
        	for(Square square: squares){
        		if(square.toString().matches("A1|C1|E1|G1|B2|D2|F2|"
        				+ "H2|A3|C3|E3|G3|B4|D4|F4|H4|A5|C5|E5|G5|"
        				+ "B6|D6|F6|H6|A7|C7|E7|G7|B8|D8|F8|H8")){
        			square.setColor("BROWN");
        		}
        		else if(square.toString().matches("A2|C2|E2|G2|B1|D1|F1|"
            			+ "H1|A4|C4|E4|G4|B3|D3|F3|H3|A6|C6|E6|G6|"
            			+ "B5|D5|F5|H5|A8|C8|E8|G8|B7|D7|F7|H7")){
            		square.setColor("WHITE");
        		}
        	}
        }

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

    Default Re: Chess Program

    Screen Shot 2013-08-12 at 4.48.37 PM.jpg

    any idea how i can get rid of the white space between the jlabels

  8. #33
    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

    Can you post the code that creates the white spaces?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Chess Program

    Ofcourse, shall i post the chessboardview class this is the class that contains the gridlayout(8,8) and contains the JLabels

    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(400, 400);
     
    	    setLayout( new GridLayout(8, 8) );
    	    setPreferredSize( boardSize );
    	    setBounds(0, 0, 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(){
    		for (int i = 0; i <64; i++){
     
    			JLabel sqr = new JLabel(chessboard.getAllSquares().get(i).getImage());
    			sqr.setBounds(0,0,0,0);
     
    			super.add(sqr);
                }
     
     
    		}
    }

  10. #35
    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

    It needs a JFrame and a main() method so it can be executed and show the problem.
    JLabel sqr = new JLabel(chessboard.getAllSquares().get(i).getImage());
    If the Square class extends JLabel why does the code create a new JLabel to add to the GUI? Why not add the Square object itself?

    Also why getAllSquares().get() vs just getSquare()?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    I made a new account but i deleted the extends JFrame

    --- Update ---

    Controller
    package controller;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;
     
    import javax.swing.*;
     
    import view.ControlPanel;
    import view.InfoPanel;
    import view.ChessBoardView;
     
     
    public class Controller extends JFrame implements ActionListener
    {
    	private InfoPanel infopanel;
    	private ControlPanel controlpanel;
        private ChessBoardView chessboardview;
     
     
        public Controller()
        {
            super("Chess Manager");
            makeFrame();   
            makeMenuBar();
            this.pack();   
        }
     
        private void makeFrame(){
            setSize(650, 650);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            Container contentpane = super.getContentPane();
            contentpane.setLayout(new BorderLayout()); 
            contentpane.setBackground(new Color(204,229,255)); 
     
            controlpanel = new ControlPanel(this);
            contentpane.add(controlpanel, BorderLayout.EAST);
     
            infopanel = new InfoPanel(this);
            contentpane.add(infopanel, BorderLayout.SOUTH);
     
            chessboardview = new ChessBoardView(this);
            contentpane.add(chessboardview, BorderLayout.WEST);
     
        }
     
        private void makeMenuBar(){
            JMenuBar menubar = new JMenuBar();
            super.setJMenuBar(menubar);
     
            JMenu fileMenu = new JMenu("File");
            menubar.add(fileMenu);
     
            JMenu editMenu = new JMenu("Edit");
            menubar.add(editMenu);
     
            JMenu optionMenu = new JMenu("Options");
            menubar.add(optionMenu);
     
            JMenu extraMenu = new JMenu("Extra");
            menubar.add(extraMenu);
        }
     
       public void setupProgram(){
     
     
        }
     
        public void actionPerformed(ActionEvent e){
     
        }
     
     
    }

    Main
    package controller;
     
    import controller.Controller;
     
    public class Main {
     
    	public static void main (String[] args)
    	{
    			new Controller().setVisible(true);
    	}
     
     
    }

  12. #37
    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

    Can you make a minimal program that compiles, executes and shows the problem? Put most of the logic in one class. There are too many parts to work with that are not part of the GUI problem.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    the problem is when i change the size of the frame(window) i get a weird gridlayout i want that to stay the same size so the jlabels stay the same

    Screen Shot 2013-08-12 at 5.36.59 PM.jpg Screen Shot 2013-08-12 at 5.37.05 PM.jpg

  14. #39
    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

    when i change the size of the frame
    I think the JFrame class has methods that can prevent the user from changing the size of the frame.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #40
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    fixed it thanks! But do you wanna know what my biggest problem is?? and i have no idea how i can make that which techniques and which code i need to use

  16. #41
    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

    Do you have a link to the tutorials?
    The Really Big Index
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    You mean the exercise? i can post some or ill explain what i have to do and what my biggest problem is

  18. #43
    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

    If you have any specific questions about your program, post them.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #44
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    Okay,

    My program is not an actual interactive chess game. Its a manager program. The program uses a FEN-notation to place the pieces on the board. The default position is also the default chess game position when the chess game starts. The FEN looks default like this: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR

    the lowercase characters represent the blackpieces
    the uppercase characters represent the whitepieces
    8 means 8 empty squares in that row.

    if you change the FEN-Notation in the program press enter like this one: rnbqkbnr/pp1ppppp/2p5/8/8/8/PPPPPPPP/RNBQKBNR
    the imageicon in this case black piece moves to that location.

    How can i build this. Do i need a FEN class which read the fen notation? what more..

    the difficult part is the 8 normally it stands for 8 empty squares but if the user moves a pawn it has to show for instance 2 empty square a pawn and then 5 empty squares.

    I think this is the most difficult part of the program to build
    System.out.println(" dream in code ");

  20. #45
    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

    Is this the problem: Given the positions of all the pieces in a FEN-notation String, how to put the pieces where they go on the board?
    The FEN-notation gives the piece and its location. The FEN-notation should map all 64 squares of the board.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #46
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    exactly! The FEN-notation string shows the position of the pieces. The gridlayout we build earlier shows the pieces(on a board) on the screen.
    System.out.println(" dream in code ");

  22. #47
    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

    Are the pieces are positioned on the board starting at the top left corner, row by row, to the bottom right corner?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #48
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    dont thinks so look:

    private Set<ChessPiece> chessPieces;
        private ArrayList<Square> squares;
        private static final String[] LOCATIONS = {
        "A8","B8","C8","D8","E8","F8","G8","H8",
        "A7","B7","C7","D7","E7","F7","G7","H7",
        "A6","B6","C6","D6","E6","F6","G6","H6",
        "A5","B5","C5","D5","E5","F5","G5","H5",
        "A4","B4","C4","D4","E4","F4","G4","H4",
        "A3","B3","C3","D3","E3","F3","G3","H3",
        "A2","B2","C2","D2","E2","F2","G2","H2",
        "A1","B1","C1","D1","E1","F1","G1","H1",
        };

    private void addLocations(){
            for(int i = 0; i < LOCATIONS.length; i++){
            	squares.add(new Square(LOCATIONS[i]));    
            }       
        }

    i putted a String Array with locations in an ArrayList<Square> the class square ask for a string location in the constructor
    System.out.println(" dream in code ");

  24. #49
    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

    dont thinks so look:
    What am I supposed to see?

    Are you trying to argue against what I said in post#47? Isn't that the order of the pieces shown in the FEN-notation? Top left to lower right.
    If that is not the order described in FEN-notation, please explain what is the correct order.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #50
    Member Wolverine89's Avatar
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    101
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chess Program

    The squares are stored in a ArrayList, so top left (first item in squares arraylist)to lower right(last item in square arraylist)
    System.out.println(" dream in code ");

Page 2 of 9 FirstFirst 1234 ... 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