Results 1 to 5 of 5
  1. #1
    MXA92 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Adding a mouse listener to my Draughts game

    Ok, so I have the GUI set up and classes for the pieces and the board but I am struggling to employ the Mouse Listener.

    I want the grey squares to turn yellow whenever the mouse enters one, and to revert back to grey whenever the mouse exits one (I already have these methods created).

    Java Code:
    BOARD CLASS
    
    package draughts;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    
    
    public class Board extends JPanel implements MouseListener
    {
        private Square[][] squares;
        private ArrayList<DraughtsPiece> lightPieces;
        private ArrayList<DraughtsPiece> darkPieces;
    
        public Board(int numRows)
        {
            super(new GridLayout(numRows, numRows));
            squares = new Square[numRows][numRows];
            lightPieces = new ArrayList<DraughtsPiece>();
            darkPieces = new ArrayList<DraughtsPiece>();
            
            setupBoard(numRows);
            setupPieces(numRows);
    
            allocatePieces();
        }
    
        private void setupBoard(int numRows)
        {
            boolean lightSquare = true;
    
            for (int row = 0; row < numRows; row++)
            {
                for (int col = 0; col < numRows; col++)
                {
                    if (lightSquare)
                    {
                        squares[row][col] = new Square(Square.LIGHT_SQUARE_COLOUR, row + 1, col + 1);
                        
                    }
                    else
                    {
                        squares[row][col] = new Square(Square.DARK_SQUARE_COLOUR, row + 1, col + 1);
                    }
                    add(squares[row][col]);
                    
                    lightSquare = !lightSquare;
                }
                lightSquare = !lightSquare;
                
            }
        }
    
        private void setupPieces(int numRows)
        {
            int numPieces = ((numRows * numRows) - (2 * numRows)) / 4;
            for (int i = 0; i < numPieces; i++)
            {
                DraughtsPiece p = new DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR);
                lightPieces.add(p);
    
                p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR);
                darkPieces.add(p);
                
                
            }
        }
    
        private void allocatePieces()
        {
            int row = squares.length - 1;
            int col = 0;
    
            for (DraughtsPiece p : lightPieces)
            {
                squares[row][col].setPiece(p);
                col += 2;
                if (col >= squares[0].length)
                {
                    col = row % 2;
                    row--;
                }
            }
    
            row = 0;
            col = squares[0].length - 1;
            for (DraughtsPiece p : darkPieces)
            {
                squares[row][col].setPiece(p);
                col -= 2;
                if (col < 0)
                {
                    row++;
                    col = squares[0].length - 1 - (row % 2);
                }
            }
        }
        
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        
        @Override
        public void mouseEntered(MouseEvent e)
        {
            
        }
        
        @Override
        public void mouseExited(MouseEvent e)
        {
            
        }
        public void mouseClicked(MouseEvent e){}
    }
    Java Code:
    SQUARE CLASS
    package draughts;
    import java.awt.*;
    import javax.swing.*;
    
    /**
     * Describes a square on the board
     * @author OOEDP people
     * @see Board
     */
    public class Square extends JPanel
    {
        public static final Color LIGHT_SQUARE_COLOUR = new Color(0xdfdfdf);
        public static final Color DARK_SQUARE_COLOUR = new Color(0x333333);
        public static final Color SELECTED_DARK_SQUARE_COLOUR = Color.YELLOW;
        
        private Color background;
        private int row, column;
        private Color selectedBackground;
        private DraughtsPiece piece;
        private boolean selected = false;
        
        /**
         * Describes a square on the board
         * @param c This is the color of the specific square
         * @param row This is the row the square is located
         * @param col This is the column the square is located
         */
        public Square(Color c, int row, int col)
        {
            super(new BorderLayout());
            background = c;
            this.row = row;
            this.column = col;
            setBackground(background);
            if (background == DARK_SQUARE_COLOUR)
            {
                selectedBackground = SELECTED_DARK_SQUARE_COLOUR;
            }
            piece = null;
        }
        
        /**
         * This gets the row the square is on
         * @return returns the row number the square is on
         */
        public int getRow()
        {
            return row;
        }
        
         /**
         * This gets the column the square is on
         * @return returns the column number the square is on
         */
        public int getColumn()
        {
            return column;
        }
        
        /**
         * This either places the piece on a square, or removes the piece
         * from a square.
         * @param piece This is the piece that is currently being moved
         */
        public void setPiece(DraughtsPiece piece)
        {
            if (piece == null && this.piece != null)
            {
                remove(this.piece);
                this.piece.setSquare(null);
                this.piece = null;
            }
            else if (piece != null && this.piece == null)
            {
                this.piece = piece;
                piece.setSquare(this);
                add(piece);
            }
        }
        
        /**
         * This gets the piece you are choosing
         * @return the piece you are choosing
         */
        public DraughtsPiece getPiece()
        {
            return piece;
        }
        
        /**
         * If the mouse enters the square, set the background
         */
        protected void enter()
        {
            setBackground(selectedBackground);
        }
        
        /**
         * If the mouse exits the square, set the background back
         */
        protected void exit()
        {
            setBackground(background);
        }
        
        /**
         * This sets the selected square
         * @param b True is the square is wanting to be selected, false otherwise
         */
        protected void setSelected(boolean b)
        {
            selected = b;
            setBackground(b == false ? background : selectedBackground);
        }
        
        /**
         * This checks whether or not a square is selected
         * @return This returns true is the square is selected, and false otherwise
         */
        protected boolean  isSelected()
        {
            return selected;
        }
    }
    I've implemented the mouse listener to the board, but I don't know where to add the mouse listener. I've tried:

    Java Code:
    Square.DARK_SQUARE_COLOUR.addMouseListener(this);
    but I get an error line under (.addMouseListener) this way....

    I also know I need to use MouseEntered(MouseEvent e) and MouseExited(MouseEvent e) and e.getX() and e.getY() some how..... but I can't get it working....

    Thank you for any help! :)


  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Adding a mouse listener to my Draughts game

    Moved from New to Java.

    You add a MouseListener to a Component -- not to a Color.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    MXA92 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Adding a mouse listener to my Draughts game

    Quote Originally Posted by DarrylBurke View Post
    Moved from New to Java.

    You add a MouseListener to a Component -- not to a Color.

    db
    I've managed to add the Mouse Listener now, so whenever the mouse enters a dark grey square it calls the mouseEntered method.

    Java Code:
    ....else
                    {
                        squares[row][col] = new Square(Square.DARK_SQUARE_COLOUR, row + 1, col + 1);
                        squares[row][col].addMouseListener(this);
                    }
    Now, I just don't understand how to change the dark square colour to Yellow :/
    I know I need to use:

    Java Code:
    protected void enter()
        {
            setBackground(selectedBackground);
        }
    from the Square Class...

    Thank you for the help


    EDIT: I've worked out I need the row and column of the square the mouse is on (using e.getX() and e.getY() somehow?) and then I can just call the enter() method.

    ie. squares[1][2].enter() changes the dark square on row 1 and column 2 to yellow whenever the mouse enters any dark square..... I don't know how to change the specific dark square the mouse is on... thank you



    EDIT 2: Done it!

    I just used a nested for loop to cycle through every square, and check whether the mouse event matched any of those squares and changed that squares colour.

    Java Code:
    public void mouseEntered(MouseEvent e)
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (e.getSource() == squares[i][j])
                    {
                        squares[i][j].enter();
                    }
                }
                
            }
        }
    Probably not the most efficient way... but it works.
    Last edited by MXA92; 03-22-2012 at 11:28 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Adding a mouse listener to my Draughts game

    Probably not the most efficient way...
    That's correct.

    Cast the event source to Square and set its color.
    Java Code:
    ((Square) e.getSource()).enter();
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    MXA92 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Adding a mouse listener to my Draughts game

    Quote Originally Posted by DarrylBurke View Post
    That's correct.

    Cast the event source to Square and set its color.
    Java Code:
    ((Square) e.getSource()).enter();
    db
    Thank you very very much Darryl!
    That looks substantially better! :)

Similar Threads

  1. Help with mouse listener please.
    By fatabass in forum AWT / Swing
    Replies: 1
    Last Post: 02-23-2012, 04:39 PM
  2. Need some help with mouse listener
    By Serialcek in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2012, 10:20 AM
  3. Mouse listener
    By chetoos in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2011, 09:23 PM
  4. Need Help with Mouse Listener!!!!
    By Doggir in forum New To Java
    Replies: 11
    Last Post: 05-05-2011, 11:48 PM
  5. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM

Posting Permissions

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