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 25 of 25

Thread: Draughts using mouse listener problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Draughts using mouse listener problem

    Hey there,

    new to Java, so excuse my noobness, but I'm having problem creating a simple method within my Draughts game. atm I have the entire board spawning a 8x8 board, half of them being white and the other half being darker.

    Now what I need to do is make it so when the mouse goes over the darker squares, it 'highlights' them turning them yellow. I have done everything I can, I have implemented the mouselistener and a method which changes the "DARK_SQURES" to "SELECTED_DARK_SQURES", although it doesn't seem to work, I feel Im missing something very simple.

    So I am wondering if I can get some insight into what Im doing wrong.

    Board.java
    package draughts;
     
    import java.awt.*;
    import java.util.ArrayList;
    import javax.swing.*;
    import java.awt.event.*;
     
    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);
                        squares[row][col].addMouseListener(this);
                    }
                    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 mouseEntered(MouseEvent e)
        {
             ((Square)e.getSource()).enter();
        }
     
        @Override
        public void mouseClicked(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        @Override
        public void mousePressed(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        @Override
        public void mouseReleased(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        @Override
        public void mouseExited(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    Square.java
    package draughts;
    import java.awt.*;
    import javax.swing.*;
     
    /**
     * a class called Square which extends to JPanel, allowing the programmer to
     * access JPanel functions 
     * @author Tim
     */
    public class Square extends JPanel
    {
     
        /**
         * represents the colour of the light square on the board
         */
        public static final Color LIGHT_SQUARE_COLOUR = new Color(0xdfdfdf);
     
        /**
         * represents the colour of the dark square on the board
         */
        public static final Color DARK_SQUARE_COLOUR = new Color(0x333333);
     
        /**
         * represents the colour of the selected dark square on the board
         */
        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;
     
        /**
         * the constructor for the Square class
         * @param c - represents Color as c, this is the colour of the square
         * @param row - represents row as an int, this is how many squares there are
         * going in a row
         * @param col - represents col as an int, this is how many squares there are
         * going in a column
         */
        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;
        }
     
        /**
         * a public int method called getRow
         * @return - returns the int representing row
         */
        public int getRow()
        {
            return row;
        }
     
        /**
         * a public int method called getColumn
         * @return - returns the int representing column
         */
        public int getColumn()
        {
            return column;
        }
     
        /**
         * a public void method called setPiece
         * @param piece - Calls in the class DraughtsPiece and represents a variable
         * called piece
         */
        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);
            }
        }
     
        /**
         * a public method calling the DraughtsPiece class, called getPiece
         * @return - returns the piece variable called from the DraughtsPiece class
         */
        public DraughtsPiece getPiece()
        {
            return piece;
        }
     
        /**
         * a protected method called enter, which doesn't return anything as it's void
         * but sets the background which is selected by the mouse. Making it yellow
         */
        protected void enter()
        {
            setBackground(selectedBackground);
        }
     
        /**
         * a protected method called exit, which doesn't return anything as it's void
         * but sets the background back to black when the mouse leaves.
         */
        protected void exit()
        {
            setBackground(background);
        }
     
        /**
         * a protected method called setSelected, which doesn't return anything as it's
         * void. This method changes the background
         * @param b - a boolean value set as 'b'
         */
        protected void setSelected(boolean b)
        {
            selected = b;
            setBackground(b == false ? background : selectedBackground);
        }
     
        /**
         * a protected method called isSelected, which returns a boolean value
         * @return - returns a boolean value called selected
         */
        protected boolean isSelected()
        {
            return selected;
        }
    }

    Sorry about the javaDoc!

    Thanks,


    Xooch


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    For a listener to work, the component must have the focus. Do the components with the listeners have the focus when the code is exectued?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    No I dont think they do, altho Im very new to java, and not sure what focus is.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    Go to the API doc for the Component class and Find Focus. There is a link that will tell you about the Focus subsystem
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    ok I understand I have to implement a FocusManager into my code, I dont understand where and what exactly I have to write out.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    I don't know what a FocusManager is and don't think I have ever used one. I have added focus listeners for debugging purposed to keep track of when components get the focus.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    I know you're trying to help, I think it might be just how I am wording it lol, but I only want it to change the black squares to yellow when my cursor highlights them, I really have no idea how to fix it from this point, I honestly think it should work as how it is now, but it isn't. So I'm completely confused

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    I do not see a main() method in the code you have posted.
    Please make a small complete program that compiles, executes and shows the problem. Remove any code not related to this problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    There isn't a problem, I dont have any errors. It just doesn't do what I want it to do.

    DraughtsApplication.java
     
    package draughts;
     
    public class DraughtsApplication
    {
        public static void main(String args[])
        {
            try
            {
                new DraughtsJFrame(8);
            }
            catch (Exception e)
            {
                System.out.println("ERROR: " + e.getMessage());
            }
        }
    }

    DraughtsJFrame.java
     
    package draughts;
    import java.awt.*;
    import javax.swing.*;
     
    public class DraughtsJFrame extends JFrame
    {
        Board board;
     
        public DraughtsJFrame(int numRows) throws Exception
        {
            super("GDM's draughts board");
            setSize(600, 626);
            setLocation(100, 100);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            if (numRows > 0)
            {
                setupGUI(numRows);
            }
            else
            {
                throw new Exception("The number of rows cannot be negative");
            }
     
            setVisible(true);
        }
     
        private void setupGUI(int numRows)
        {
            board = new Board(numRows%2==0? numRows : numRows + 1);
     
            getContentPane().add(board, BorderLayout.CENTER);
        }
    }

    DraughtsPiece.java
     
    package draughts;
    import java.awt.*;
    import javax.swing.*;
     
    public class DraughtsPiece extends JComponent
    {
        public static final Color LIGHT_PIECE_COLOUR = new Color(0xccccff);
        public static final Color DARK_PIECE_COLOUR = new Color(0x3333ff);
     
        private Color colour;
        private Square square;
     
        public DraughtsPiece(Color c)
        {
            super();
            colour = c;
        }
     
        public void setSquare(Square s)
        {
            square = s;
        }
     
        public Square getSquare()
        {
            return square;
        }
     
        @Override
        public void paint(Graphics g)
        {
            super.paint(g);
            g.setColor(colour);
            g.fillOval(5, 5, getWidth()-10, getHeight()-10);
        }
    }


    and then Square.java and Board.java are the same as in the original post


    Thanks

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    Can you explain what is supposed to happen when the mouse moves over the board? The posted code throws an exception for all the mouse movements.

    When I removed the exceptions and move the mouse, the squares change color from black to yellow as the cursor moves over them.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    basically when the mouse moves over WHITE squares, it does nothing. But when it moves over the dark square, that single dark square which the cursor is over turns yellow. using these pieces.

    public static final Color SELECTED_DARK_SQUARE_COLOUR = Color.YELLOW;
     
     
    if (background == DARK_SQUARE_COLOUR)
            {
                selectedBackground = SELECTED_DARK_SQUARE_COLOUR;
            }
     
     
    protected void enter()
        {
            setBackground(selectedBackground);
        }
     
     
    protected void exit()
        {
            setBackground(background);
        }


    Which exceptions did you remove?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    Which exceptions did you remove?
    All of them in the mouse listener methods.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    hm, can you paste me the code you edited? I just removed my exceptions and it still doesn't turn the dark squares, yellow.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    I only added // in front of the throw statements.

    The code works for me as I described in post #10
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    ok, removing the throw statements make it so the dark square turn yellow when I mouse over them, although the game crashes immediately, after entering a dark square

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    the game crashes immediately, after entering a dark square
    Post the full text of the error messages
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
    	at draughts.Board.mouseExited(Board.java:116)
    	at java.awt.Component.processMouseEvent(Component.java:6511)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4612)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4474)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    .UnsupportedOperationException: Not supported yet.
    That exception is from your code!!!

    Where did you get this program from? Obviously you didn't write it.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    No I didnt write it all, I have been given it from a tutor asking to edit the code so it highlights the dark squares, obviously Im having trouble doing it.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    You seem to have left all that out at the start of this thread.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    I didnt think it was relevant

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    If you had written the code then I would expect you to know about the code and what it contained.
    I was making assumptions that let me waste a lot of time chasing ghosts.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    I do know the code and what it contains, I just had to edit it so the dark squares can be highlighted. I'm new to java and having troubles, hence I came to this forum

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Draughts using mouse listener problem

    I do know the code and what it contains
    Allow me to doubt that. The code has several copies of this statement that you did not know about:
     throw new UnsupportedOperationException("Not supported yet.");
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Draughts using mouse listener problem

    That was because my Board class was giving an error, and Netbeans suggested I either make the class abstract, or implement all the abstract methods. It's not even in the original code I was given.

    http://img854.imageshack.us/img854/9531/lolbso.jpg

    You can doubt anything you want, you are entitled to. But I came to this forum for help with my java, I do not want to start an argument.

Similar Threads

  1. Adding a mouse listener to my Draughts game
    By MXA92 in forum Object Oriented Programming
    Replies: 10
    Last Post: March 22nd, 2012, 06:43 PM
  2. add action listener problem
    By mdhmdh100 in forum AWT / Java Swing
    Replies: 5
    Last Post: February 5th, 2012, 02:53 AM
  3. Key Listener problem!
    By DouboC in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2012, 07:47 PM
  4. implement mouse listener class
    By vnsgbt in forum AWT / Java Swing
    Replies: 1
    Last Post: October 24th, 2011, 06:06 PM
  5. [SOLVED] Is it possible for a mouse listener to exist without a GUI interface?
    By techwiz24 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 3rd, 2011, 02:32 PM