Results 1 to 6 of 6
Thread: Sluggish MouseListener
- 06-01-2010, 05:11 AM #1
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! } }); }
-
I can't guess the cause of your delay based on the code presented, but do wonder just what this method does:
Much luck!Java Code:paintPanel (g2d, this.getWidth(), this.getHeight());
- 06-01-2010, 07:44 PM #3
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); } }
- 06-01-2010, 08:41 PM #4
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.
-
- 06-01-2010, 11:33 PM #6
Similar Threads
-
can't get x and y from mouselistener
By j2me64 in forum Java 2DReplies: 3Last Post: 04-24-2010, 04:57 PM -
MouseListener & GUI
By Suurisa in forum New To JavaReplies: 2Last Post: 10-27-2009, 12:52 AM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM -
I need help with my MouseMotionAdapter and MouseListener.
By MurderfaceX4 in forum New To JavaReplies: 1Last Post: 12-07-2007, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks