Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Need help in chess project

  1. #1
    Hussam_7102 is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Post Need help in chess project

    Hi guys m new to this forums, I need your help in my chess game project.... :)
    Can anyone tell me how do I give direction to each button, How do I move a button from one panel to another??
    And plzz help me in this project as much as u can thanks :)


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class board2 implements MouseMotionListener, MouseListener, ActionListener{
    
    JFrame frame;
    JPanel boxes[][] = new JPanel[8][8];
    JButton buttons[][] = new JButton[4][8];
    
    public board2() {
        frame = new JFrame("Chess Project By HUSSAM");
        frame.setSize(500,500);
        frame.setLayout(new GridLayout(8,8));
    
        //Creation of box panels
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                boxes[i][j] = new JPanel();
    
                if ((i + j) % 2 == 0) {
                    boxes[i][j].setBackground(Color.black);
                } else {
                    boxes[i][j].setBackground(Color.white);
                }   
                frame.add(boxes[i][j]);
            }
        }
        
        
        //Creation of Buttons
         for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 8; j++) {
                buttons[i][j] = new JButton();
                buttons[i][j].addMouseMotionListener(this);
                buttons[i][j].addMouseListener(this);
                buttons[i][j].addActionListener(this);
            }
            
         }//end outer loop
    
        boxes[0][0].add(buttons[0][0]);
        boxes[0][1].add(buttons[0][1]);
        boxes[0][2].add(buttons[0][2]);
        boxes[0][3].add(buttons[0][3]);
        boxes[0][4].add(buttons[0][4]);
        boxes[0][5].add(buttons[0][5]);
        boxes[0][6].add(buttons[0][6]);
        boxes[0][7].add(buttons[0][7]);
    
        boxes[7][0].add(buttons[2][0]);
        boxes[7][1].add(buttons[2][1]);
        boxes[7][2].add(buttons[2][2]);
        boxes[7][3].add(buttons[2][3]);
        boxes[7][4].add(buttons[2][4]);
        boxes[7][5].add(buttons[2][5]);
        boxes[7][6].add(buttons[2][6]);
        boxes[7][7].add(buttons[2][7]);
    
        for (int i = 0; i < 8; i++) {
            boxes[1][i].add(buttons[1][i]);
            boxes[6][i].add(buttons[3][i]);
        }
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
        
        
    }//end method board2
    
    public void mouseDragged(MouseEvent e){
      
      
      
    }//end mouse dragging
    
    
    public void mouseMoved(MouseEvent e){
      
      
      
    }//end mouse moving
    
    public void mousePressed(MouseEvent e){
      
      
       if(e.getSource()==buttons[1][0]){
        
        boxes[2][0].add(buttons[1][0]); 
    
      
        }
      
    }//end mouse pressing
    
    public void mouseClicked(MouseEvent e){
      
      
    
      
    }//end mouse clicking
    
    public void mouseReleased(MouseEvent e){
      
      
      
      
    }//end mouse releasing
    
    public void mouseEntered(MouseEvent e){
      
      
    }//end mouse entering
    
    public void mouseExited(MouseEvent e){
      
      
    }//end mouse exiting
    
    public void actionPerformed(ActionEvent e){
      
      
      
     
      
      
      
    }//end action performing
    
    
    public static void main(String[] args) {
        new board2();
    }
    }//end board2 class
    Last edited by Hussam_7102; 12-21-2013 at 08:12 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    How do I move a button from one panel to another??
    Can you explain why you want to move the button? Would moving the contents of a button to be in another button do what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Hussam_7102 is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Need help in chess project

    I am making chess so I used 64 panels as boxes, Now I want to move buttons. :)
    How do I move a button from one panel to another panel by clicking on it??

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    What about moving the contents of the button vs the button?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Hussam_7102 is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Need help in chess project

    Obviously content of the button will be along with that button when it moves!!
    I just want movement of buttons like chess pieces in chess game :)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    Consider the buttons as the squares of the board. They do not move.
    And the chess pieces are the contents of each square. They do move.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Hussam_7102 is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Need help in chess project

    How do they move?? :D :P

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    To swap two components in a container, remove all of the components in the container and then add them all back in the order that they are to be positioned in the container.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Need help in chess project

    Just a piece of advice coming through. 64 JPanels is an awful way to go about this. As well as the JButtons. I would use some graphics. That would require 1 JPanel and 0 JButtons.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,346
    Rep Power
    6

    Default Re: Need help in chess project

    Quote Originally Posted by zFollette View Post
    64 JPanels is an awful way to go about this. As well as the JButtons.
    Why awful?

    I would use some graphics. That would require 1 JPanel and 0 JButtons.
    and understanding of a low level subject as custom painting and event handling that is probably far beyond the OP's skill level at this point in time.
    Norm likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Need help in chess project

    Quote Originally Posted by gimbal2 View Post
    Why awful?



    and understanding of a low level subject as custom painting and event handling that is probably far beyond the OP's skill level at this point in time.
    I guess I'm just saying that a chess board is too simple to use anything more complicated than graphics IMHO. Unless OP is making it complicated on purpose.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    Having the JVM tell which square was clicked on may be easier that taking a mouse click's x,y position and converting it to a row and column.
    Swapping the contents of two components will be much simpler than using the x,y locations on the full board for drawing pieces in their locations.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Need help in chess project

    Quote Originally Posted by Norm View Post
    Having the JVM tell which square was clicked on may be easier that taking a mouse click's x,y position and converting it to a row and column.
    Swapping the contents of two components will be much simpler than using the x,y locations on the full board for drawing pieces in their locations.
    It all depends on how you do it I guess. You could make a crappy chess game or a good chess game with both methods, and considering the insignificance of the graphics, it wouldn't make too much of a difference RAM wise.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    wouldn't make too much of a difference RAM wise.
    Not sure what that has to do with writing a GUI for a game. Using existing software to detect mouse clicks and to change the locations of pieces seems a better usage of a programmer's time than working with x,y locations in a window.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Need help in chess project

    Im saying that both of the methods (if done right) will be equally, or somewhat equally efficient.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: Need help in chess project

    equally efficient.
    In most cases efficiency is not an issue. Ease of coding and maintaining is much more important.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Need help in chess project

    I wouldn't say either of the two methods are difficult to code. But I see where you're coming from.

Similar Threads

  1. Chess in Java
    By altair69 in forum New To Java
    Replies: 14
    Last Post: 01-25-2012, 03:31 PM
  2. Help with Chess GUI
    By Lord Voldemort in forum AWT / Swing
    Replies: 9
    Last Post: 01-24-2011, 06:38 PM
  3. Chess program
    By Chro in forum New To Java
    Replies: 1
    Last Post: 10-26-2010, 10:20 AM
  4. chess game
    By michail in forum New To Java
    Replies: 13
    Last Post: 01-12-2010, 08:24 AM
  5. Chess game
    By michail in forum New To Java
    Replies: 4
    Last Post: 12-23-2009, 07:51 PM

Tags for this Thread

Posting Permissions

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