• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrong Mouse Positions

 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I need help, because another problem appeared:

If I move the JFrame I'm drawing in, the MousePositions are moving weirdly in the direction, i'm moving the jframe

how can I get the positions Correct, even if I move the JFrame?
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but your question makes no sense to me. You say another problem has appeared, does this mean this relates to an earlier post you have made? if so can you provide a link to the other thread.
Also can you show some code that demonstrates the problem you are describing along with instructions on how to make the issue appear.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, of course we know what you are doing, what mouse listener code you have, how you move the JFrame and what you mean by drawing. Oh, and we also know what behaving weirdly means.

I jest of course. We have no idea how to fix your issue because you don't show us anything. Be could make a guess but 99.99% chance that the guess would be unrelated to your code.

Why don't you write a small SSCCE (<-click) which shows the problem you are having and explain what is actually behaving incorrectly (i.e. what you expect to happen and the difference between that and what is happening).
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree. not enough information.

however, you don't draw in a JFrame
 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


own defined:


You have to resize the frame once.
In the beginning, the blue square is on my cursor, but if I move the JFrame it's drawn anywhere.
 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i remove the /100 at the q variable in the paint method, i dont work either
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for one thing, in a JPanel, you do not overide paint(),rather you override paintComponent()

and call super.paintComponent() instead of super.paint()
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is MouseInfo.getPointerInfo(); returns the mouse position relative to the top left of the screen whereas the graphics drawing methods draw relative to the top left of the component you are drawing in. You need to subtract the location of the component from the mouse position to get the correct relative value.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to subtract the location of the component from the mouse position to get the correct relative value.

Or better still, use the information that can be gleaned from the MouseEvent.
 
Steven Joost
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The problem is MouseInfo.getPointerInfo(); returns the mouse position relative to the top left of the screen whereas the graphics drawing methods draw relative to the top left of the component you are drawing in. You need to subtract the location of the component from the mouse position to get the correct relative value.


And how Im doing this?
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the API for MouseEvent? There are two methods that return a Point. One of those is what you need.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic