Results 1 to 6 of 6
  1. #1
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Question Sluggish MouseListener

    Hey all,
    Im trying to make a simple sudoku problem solver.
    What I've got at the moment is a set of 9 JPanels as "Grids" each containing 9JPanels as the "Squares".
    When someone clicks on a square, it should be highlighted.

    I think ive properly added a mouseListener on each square, and if you click on one it *does* highlight. However, if you try to double click or click on another square within about 3-4 seconds, nothing happens.

    Im fairly sure its not the repainting, as I introduced a println for every click - which doesnt occur when it isnt highlighted.

    Any tips on how to speed this up?

    Thanks in advance,
    Berkeleybross

    Java Code:
        public Square (Grid parent) {
            myGrid = parent;
            myValue = 1;
            possMoves = new ArrayList <Integer> ();
            myPanel = new JPanel () {
    
                @Override
                public void paintComponent (Graphics g) {
                    Graphics2D g2d = (Graphics2D) g;
                    if (selected) {
                        g2d.setColor (Color.BLUE);
                    } else {
                        g2d.setColor(myGrid.getBackgroundColour());
                    }
                    g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
                    paintPanel (g2d, this.getWidth(), this.getHeight());
                }
            };
            myPanel.addMouseListener(new MouseListener () {
                public void mouseClicked(MouseEvent e) {
                    System.out.println ("Clicked");
                    selected = !selected;
                    myPanel.repaint();
                    myPanel.revalidate();
                }
    
                public void mousePressed(MouseEvent e) {
                    // Do nothing!
                }
    
                public void mouseReleased(MouseEvent e) {
                    // Do nothing!
                }
    
                public void mouseEntered(MouseEvent e) {
                    // Do nothing!
                }
    
                public void mouseExited(MouseEvent e) {
                    // Do nothing!
                }
            });
    
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't guess the cause of your delay based on the code presented, but do wonder just what this method does:

    Java Code:
    paintPanel (g2d, this.getWidth(), this.getHeight());
    Much luck!

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Hi,

    Thanks for your reply, much appreciated :).

    The method merely paints the value of the square or the possible values of the square, whilst centring them.

    As I said, the program paints pretty much instantly so i dont *think* thats the problem.

    I spent some time thinking about it last night, and I was wondering if having 81 of these mouse listeners would be causing some kind of problem?

    Thanks for reading,
    Berkeleybross

    Java Code:
    public void paintPanel (Graphics2D g, int width, int height) {
            g.setColor(Color.black);
            // Check if myValue has been set
            if (myValue == -1) {
                // List all possible values (1-9) in a three by three grid in the
                // centre of the square
                for (int i = 1; i < 4; i++) {
                    for (int j = 0; j < 3; j++) {
                        String st = String.valueOf (i + j*3);
                        Font myFont = new Font ("Dialog.plain", 0, 12);
                        g.setFont(myFont);
                        FontMetrics gFM = g.getFontMetrics();
                        if (possMoves.contains(i + j*3)) {
                            g.drawString(st, i *(width - gFM.stringWidth(st)) / 4, (j + 1)*(height + gFM.getHeight())/4);
                        }
                        
                    }
                }
                
            } else {
                // display the value of the square in a large font, in the centre
                String st = String.valueOf (myValue);
                Font myFont = new Font ("Dialog.plain", 0, 20);
                g.setFont(myFont);
                FontMetrics gFM = g.getFontMetrics();
                g.drawString(st, (width - gFM.stringWidth(st)) / 2, (height + gFM.getHeight())/2);
            }
        }

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

    Default

    No having 81 listeners should be no problem.

    Try adding some more println() debug statements to see where the code is executing.
    Also in the empty listener methods.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by berkeleybross View Post
    H
    I spent some time thinking about it last night, and I was wondering if having 81 of these mouse listeners would be causing some kind of problem?
    I very much doubt that that's the problem, but we may need to see more of the code to know for sure. You could make it into a Jar file, change the extension to zip and post the zip file here.

  6. #6
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    :o

    Ok I've found the problem,
    I tried Norm's suggestion and discovered the the mouse pressed and released were being activated, but not the click. I reckon the mouse is moving a pixel or so by accident ^_^

    Thanks for your time, sorry for the wild goose chase.

    Berkeleybross

Similar Threads

  1. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 05:57 PM
  2. MouseListener & GUI
    By Suurisa in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 01:52 AM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 04:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 09:10 AM
  5. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 04:13 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
  •