View Full Version : JPanel Help
Steve
February 6th, 2011, 05:34
K so i made a little application in Java to grab the ingame coordinates of where you clicked on a runescape world map. To do this i just made a subclass of JPanel, and added a mouselistener to listen for when i click on it, and overrode the paintComponent method to draw my currentX and curretY & the map image. I then added this to a JScrollPane, so i can scroll around the map. My problem is when i add a mouselistener to the subclass of JPanel, when i click, i get a white box from the top left of the panel, to the point i clicked. This only happens when i add a mouse listener. I really have no idea why this is happening. Is this JScrollPane and my JPanel class conflicting? Or is it just some bug with JPanel, or could it be that i made the GUI in Netbeans. Also this bug only happens when i don't run the file in Netbeans. If i run the program with netbeans and i click i don't get a white box, but if i were to run it in Eclipse i would get the white box. Oh and i tried google :cool:.
samuraiblood2
February 6th, 2011, 05:48
Screenshot please.
Steve
February 6th, 2011, 05:50
Screenshot please.
Sorry for the large picture, but here you go.
Only the registered members can see the link.
samuraiblood2
February 6th, 2011, 06:47
Looks more like a rendering problem then anything. Any exceptions being thrown?
Steve
February 6th, 2011, 07:10
Looks more like a rendering problem then anything. Any exceptions being thrown?
Nope, also, when I drag my mouse(with a mousemotionlistener added) and repaint, the white box will dissapear.
Faab234
February 6th, 2011, 10:54
Nope, also, when I drag my mouse(with a mousemotionlistener added) and repaint, the white box will dissapear.
Maybe something with your repaint method.
Steve
February 6th, 2011, 12:36
Maybe something with your repaint method.
Idk, it's only when I add a mouselistener to the panel and click, like I don't even have to call repaint(), it just draws the white box.
Faab234
February 6th, 2011, 12:38
Idk, it's only when I add a mouselistener to the panel and click, like I don't even have to call repaint(), it just draws the white box.
Can you show us the code?
Steve
February 6th, 2011, 12:42
Can you show us the code?
I can later on because I'm not home right now, I think I can just add a mouselistener to the jscrollpane it's in then call the methods in the JPanel class, I hope that fixes it lol.
Steve
February 7th, 2011, 04:14
Ok I figured this out, this can be closed.
Trey
February 7th, 2011, 04:15
Ok I figured this out, this can be closed.
You may want to say what you figured out before I close it. It may benefit others.
Steve
February 7th, 2011, 04:23
You may want to say what you figured out before I close it. It may benefit others.
Oh ok,
Well my problem was on mousePressed I was setting a flag to true so it would draw a box from the point I clicked to the point I where I would release the mouse, then set the x and y for the box to 0. My problem was when u just click your mouse i forgot that mousePressed and mouseReleased still get called, and since I hadnt moved my mouse the x and y for the box was at 0. And when I draw the box I draw from the x and y to the Mouses current x and y, so it would draw a big box to my x and y when I clicked because mouseReleased and mousePressed were still getting called. To fix it I just did some checks to make sure the x and y for the box weren't 0. It was a silly mistake lol. Yeah I guess that's hard to understand lol. I'll be releasing this soon anyway if anyones that interested in the problem.
Trey
February 7th, 2011, 04:24
Oh ok,
Well my problem was on mousePressed I was setting a flag to true so it would draw a box from the point I clicked to the point I where I would release the mouse, then set the x and y for the box to 0. My problem was when u just click your mouse i forgot that mousePressed and mouseReleased still get called, and since I hadnt moved my mouse the x and y for the box was at 0. And when I draw the box I draw from the x and y to the Mouses current x and y, so it would draw a big box to my x and y when I clicked because mouseReleased and mousePressed were still getting called. To fix it I just did some checks to make sure the x and y for the box weren't 0. It was a silly mistake lol.
Oh lol, it happens to the best of us =P.
Anyway, locked as requested.
Powered by vBulletin® Version 4.1.9 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.