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.

Results 1 to 2 of 2

Thread: Chess Game. Code to move pieces.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Chess Game. Code to move pieces.

    Dear friends,

    I currently am working on programming a chess game. Nothing difficult, just a board with pieces that can be moved around. I got the first part working (draw board with pieces), but I am having problems with writing the methods for the pieces to be moved (MousePressed, mouseDragged, mouseReleased). Could any of you please help me? I am still a beginner, so lots of help would be appreciated. Thank you in advance.

    P.S. I know this sounds horrible, but if the answer could contain a finished version of this code that would be heavens... but maybe thats really me asking too much already (i just learn better that way)

    import javax.swing.*;
    import java.awt.*;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.*;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
     
    public class Chess2 extends JFrame implements MouseListener, MouseMotionListener 
    {
        JFrame frame;
        JLayeredPane chessPane;
        JLabel chessPiece;
        JPanel chessBoard;
        JPanel squares[][] = new JPanel[8][8];
        int xCoordinate;
        int yCoordinate;
     
     
        public Chess2() 
        {
            frame = new JFrame("Chess2");
            frame.setSize(600, 600);
            frame.setLayout(new GridLayout(8, 8));
            chessPane = new JLayeredPane();
            getContentPane().add(chessPane);
     
            chessPane.addMouseListener(this);
            chessPane.addMouseMotionListener(this);
     
            //drawing the board
     
            for (int a = 0; a < 8; a++) 
            {
                for (int b = 0; b < 8; b++) 
                {
                    squares[a][b] = new JPanel();
     
                    if ((a + b) % 2 == 0) 
                    {
                        squares[a][b].setBackground(new Color(238, 221, 187));
                    } 
                    else 
                    {
                        squares[a][b].setBackground(new Color(204,136,68));
                    }   
                    frame.add(squares[a][b]);
                }
            }
                //first row top
                    squares[0][0].add(new JLabel(new ImageIcon("Rooka8.png")));
                    squares[0][1].add(new JLabel(new ImageIcon("Knightb8.png")));
                    squares[0][2].add(new JLabel(new ImageIcon("Bishopc8.png")));
                    squares[0][3].add(new JLabel(new ImageIcon("Queend8.png")));
                    squares[0][4].add(new JLabel(new ImageIcon("Kinge8.png")));
                    squares[0][5].add(new JLabel(new ImageIcon("Bishopf8.png")));
                    squares[0][6].add(new JLabel(new ImageIcon("Knightg8.png")));
                    squares[0][7].add(new JLabel(new ImageIcon("Rookh8.png")));
                //second row top a
                    for (int x = 0; x < 8; x++) 
                    {
                       squares[1][x].add(new JLabel(new ImageIcon("Pawna7.png")));
                    }
                //third row bottom
                    for (int x = 0; x < 8; x++)
                    {
                        squares[6][x].add(new JLabel(new ImageIcon("Pawna2.png")));
                    }
                //fourth row bottom
                    squares[7][0].add(new JLabel(new ImageIcon("Rooka1.png")));
                    squares[7][1].add(new JLabel(new ImageIcon("Knightb1.png")));
                    squares[7][2].add(new JLabel(new ImageIcon("Bishopc1.png")));
                    squares[7][3].add(new JLabel(new ImageIcon("Queend1.png")));
                    squares[7][4].add(new JLabel(new ImageIcon("Kinge1.png")));
                    squares[7][5].add(new JLabel(new ImageIcon("Bishopf1.png")));
                    squares[7][6].add(new JLabel(new ImageIcon("Knightg1.png")));
                    squares[7][7].add(new JLabel(new ImageIcon("Rookh1.png")));
     
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
        }
     
       public void mousePressed(MouseEvent event)
      {
          Component component =  frame.findComponentAt(event.getX(), event.getY());
     
          if (component instanceof JPanel) 
          return;
     
          Point parentLocation = component.getParent().getLocation();
          xCoordinate = parentLocation.x - event.getX();
          yCoordinate = parentLocation.y - event.getY();
          chessPiece = (JLabel)component;
          chessPiece.setLocation(event.getX() + xCoordinate, event.getY() + yCoordinate);
          chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
          chessPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
      }
     
      //move pieces
     
      public void mouseDragged(MouseEvent me) 
      {
         if (chessPiece == null) return;
         chessPiece.setLocation(me.getX() + xCoordinate, me.getY() + yCoordinate);
      }
     
      //drop a piece when mouse is released
     
      public void mouseReleased(MouseEvent e) 
      {
           if(chessPiece == null) return;
     
          chessPiece.setVisible(false);
          Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
     
          if (c instanceof JLabel)
          {
              Container parent = c.getParent();
              parent.remove(0);
              parent.add( chessPiece );
          }
          else 
          {
              Container parent = (Container)c;
              parent.add( chessPiece );
            }
     
      chessPiece.setVisible(true);
      }
     
      public void mouseClicked(MouseEvent e) 
      {
          }
      public void mouseMoved(MouseEvent e) 
      {
          }
      public void mouseEntered(MouseEvent e)
      {
          }
      public void mouseExited(MouseEvent e) 
      {  
          }
     
        public static void main(String[] args) 
        {
            JFrame frame = new Chess2();
            frame.setResizable(true);
            frame.setLocationRelativeTo(null);
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,446
    My Mood
    Mellow
    Thanks
    86
    Thanked 476 Times in 467 Posts

    Default Re: Chess Game. Code to move pieces.

    Welcome! Please read this topic for info useful to new members.

    Thank you for taking the time to learn to post code correctly.

    There are tutorials online that go through what you've asked. Search for those, go through them, then let us know where you still need help.

Similar Threads

  1. Help! Error with a save and load feature of a java chess game
    By davidchan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 22nd, 2013, 07:27 AM
  2. [SOLVED] Tile puzzle game getting tiles to move (help)
    By thatguy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 5th, 2013, 05:18 PM
  3. Replies: 20
    Last Post: December 26th, 2012, 09:49 PM
  4. 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
  5. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 07:42 PM

Tags for this Thread