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 6 of 9 FirstFirst ... 45678 ... LastLast
Results 126 to 150 of 219

Thread: Chess Program

  1. #126
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    I can't see what is wrong with the GUI. Its too small.
    Can you make a small simple program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #127
    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

    huh? if you click on the image it enlarge. but the chessboard is crappy normally you see the squares but when want to add piece images it is...well

    Screen Shot 2013-08-12 at 5.36.59 PM.jpg Screen Shot 2013-08-15 at 12.18.15 AM.jpg
    System.out.println(" dream in code ");

  3. #128
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    I have no idea what the code does to change the GUI like shown in the post.
    One thing that can be seen is that the color of the squares changes so that the squares in a column are the same color. Does the code set the color of the squares more than once? The first time makes the diagonals and the second time makes the columns the same color.

    You'll have to make a small, simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #129
    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

    Does the code set the color of the squares more than once?
    Yes, the code two JLabels at the same place. With a if-statement i check the square
    System.out.println(" dream in code ");

  5. #130
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    Why do it a second time? If the first time does it correctly, why do it a second time?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #131
    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

    It works!!!!!! but still need to implement exceptions. Is that difficult? dont close thread yet.
    System.out.println(" dream in code ");

  7. #132
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts
    If you don't understand my answer, don't ignore it, ask a question.

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

    Any idea how i can add the ActionListener in the controller package but the buttons are in the view package. different classes

    controller.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(1050, 1050);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            setResizable( false );
     
            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){
     
        }
    }

    view.ControlPanel

    package view;
     
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
     
    import controller.Controller;
    import model.ChessBoard;
    import model.ChessPiece;
    import model.Fen;
    import model.Square;
     
    import view.ChessBoardView;
     
    public class ControlPanel extends JPanel
    {
    	private JPanel buttonpanel;
        private JButton btnEmptyBoard, btnInitialPosition;
    	private JSlider sldVolume;
     
    	private Fen fen;
    	private ChessBoardView chessboardview;
    	private InfoPanel infopanel;
     
    	public ControlPanel (Controller controller)
    	{
    		setLayout(new GridBagLayout());
    		setBackground(new Color(204,229,255));
     
    		GridBagConstraints gbc = new GridBagConstraints();
    		gbc.insets = new Insets(10,0,0,30);
     
     
    		btnEmptyBoard = new JButton("Empty Board");
    		gbc.gridy = 0;
    		btnEmptyBoard .setPreferredSize(new Dimension(180, 25));
    		//btnEmptyBoard .addActionListener(controller);
    		btnEmptyBoard .setRequestFocusEnabled(false);
    		add(btnEmptyBoard, gbc);
     
    		btnInitialPosition = new JButton("Initial Position");
    		gbc.gridy =1;
    		btnInitialPosition .setPreferredSize(new Dimension(180, 25));
    		//btnInitialPosition .addActionListener(controller);
    		btnInitialPosition .setRequestFocusEnabled(false);
    		add(btnInitialPosition, gbc);
     
    		}	
    }


    all ready made a actionlistener and parameter the controller class. but how can i add the actionevent e in the controller class and add it to the buttons in the controlpanel class

    --- Update ---

    has i something to do with e.getSource()
    System.out.println(" dream in code ");

  9. #134
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    To use the contents of a class, you need a reference to an instance of the class.

    The following code in class3 this would add a listener in class2 to a component in class1

    class1Ref.component1.AddActionListener(class2Ref);


    The getSource() method returns a reference to the component that caused the event. For example the button that was clicked.

    	//btnInitialPosition .addActionListener(controller);
    Why is that statement commented out? It adds the controller class as the listener for the button.

    It looks like you are writing code BEFORE you have designed what the program is supposed to do. What do you want the listener to do?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #135
    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,

    component is that a method or a button? can you give an example with the code i posted above?

    Now i use Class Controller implements ActionListener

    The class controlpanel use the controller class as parameter. i can use @override but i dont know how to use that:p
    System.out.println(" dream in code ");

  11. #136
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    component is that a method or a button?
    It's a reference to a class object that extends the Component class.

    an example with the code i posted above?
    btnInitialPosition .addActionListener(controller);

    What is the listener supposed to do? Is it in the correct class to do what it is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #137
    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 code you posted btnInitialPosition .addActionListener(controller)..i was that far but now i want to add the ActionEvent in the controller class because that class implements ActionListener Class but i dont know how to do that

    What is the listener supposed to do? Is it in the correct class to do what it is supposed to do?
    it has to read the FEN notation scan it and place the pieces on the board
    System.out.println(" dream in code ");

  13. #138
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    i want to add the ActionEvent in the controller class
    The ActionEvent object is passed by the JVM to the listener method when it is called.
     public void actionPerformed(ActionEvent e){
    You don't create it or pass it. It is passed to the above method.

    it has to read the FEN notation
    Where is the FEN string? Can the listener get to it?
    If you don't understand my answer, don't ignore it, ask a question.

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

    yes the listener can get to it.

    Question: I understand the principal
    class1Ref.component1.AddActionListener(class2Ref);
    I want to use that to so,
    class controlpanel has
    btnInitialPosition .addActionListener(controller);
    class controller has
    public class Controller implements ActionListener{
     
          public void ActionPerformed(ActionEvent e){
     
          }
    }

    but my controlpanel class has two buttons with actionlisteners

    tnInitialPosition .addActionListener(controller);
    btnEmptyBoard.addActionListener(controller);
    the controller class has one @override actionperformed. How can i do separate things for each button in the controlpanel? while i have one actionperformed in the controller class
    System.out.println(" dream in code ");

  15. #140
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    How can i do separate things for each button in the controlpanel?
    Have a separate listener for each button. Each listener will do the task to be done when its button is pressed: gather the data and call methods as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #141
    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 know but i want to implement in the controller class what needs to done. But i only have one actionperformerd. How will the button know which task to perform
    System.out.println(" dream in code ");

  17. #142
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    Several ways:
    1)Have one listener for each button. Put the listeners in the class with the buttons.
    Have the listener call methods in the controller class to do the task.

    2)Use the JComponent class's client properties to store data in the button object that the listener can extract to know what button was pressed.

    3)Extend the JButton class with your own class. Have your class contain data that the listener can use to know what button was pressed.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Have the listener call methods in the controller class to do the task.
    how can i build that? call methods?
    System.out.println(" dream in code ");

  19. #144
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    call methods?
    You call a method in another class by having a reference to that class:
    refToOtherClass.theMethodToCall(theArgsHere...);
    If you don't understand my answer, don't ignore it, ask a question.

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

    tnInitialPosition .addActionListener(controller); i have this. now i want in the controller class the actionperformed but i have two buttons how can i separate them so the
    btnEmptyBoard.addActionListener(controller); also has a actionperformed in the controller class but different operations
    System.out.println(" dream in code ");

  21. #146
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    See post#142 for several ways.
    If you don't understand my answer, don't ignore it, ask a question.

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

    sorry but i dont understand that post.

    i have to buttons with addActionListeners in my package view.ControlPanel;
    tnInitialPosition .addActionListener(controller);
    btnEmptyBoard. addActionListener(controller);
    My controller class implements ActionListener and has one @override public void ActionPerformed(ActionEvent e){
    }

    but i have two buttons in my ControlPanel class. How can i separate the operations for the buttons in the Controller class.
    can you code an example?
    System.out.println(" dream in code ");

  23. #148
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    The three solutions I gave say how to do it:
    1) Put the listeners in the class with the buttons. Have the listeners call the controller class's methods
    Don't have the listener inside the controller class.

    or 2) & 3) Put data in the object that is passed in the ActionEvent object to the listener that can be used to know which button was pressed.
    If you don't understand my answer, don't ignore it, ask a question.

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

    controller class's methods
    the listener gets as parameter controller class, in the controller class is the actionperformed. I dont think i understand it.

    --- Update ---

    how can i give a string parameter to a method, like i fill in the FEN notation, press initial position and the FEN parameter goes into the variable fen in the fen class

    --- Update ---

    this is in my infopanel class its for reading the FEN notation and pass it to the FEN class to scan the String

    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == fenbar){
    			String input = fenbar.getText();
    			fen.scanner(input);
     
    			fen.scanParser();
    			chessboardview = new ChessBoardView();
    			chessboardview.addSquaresAndPiecesToContentPane();
    		}	
    	}  
    }

    how can perform this action when i push the button initial position in the ControlPanel Class
    	btnInitialPosition = new JButton("Initial Position");
    		gbc.gridy =1;
    		btnInitialPosition .setPreferredSize(new Dimension(180, 25));
    		btnInitialPosition .addActionListener(this);	
    		btnInitialPosition .setRequestFocusEnabled(false);
    		add(btnInitialPosition, gbc);
    		}	
     
     
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == btnInitialPosition){
     
    		}


    --- Update ---

    can someone help me i cant get it to work
    System.out.println(" dream in code ");

  25. #150
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,613
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Chess Program

    Is this what is supposed to happen?
    The user enters a FEN string and presses a button
    The button's listener gets the FEN String and calls a method to parse the string and position the pieces on the board.
    If you don't understand my answer, don't ignore it, ask a question.

Page 6 of 9 FirstFirst ... 45678 ... 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, 07:42 PM
  3. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 01:40 AM
  4. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 11:07 AM
  5. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM