aspose file tools*
The moose likes Beginning Java and the fly likes Wrong Mouse Positions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrong Mouse Positions" Watch "Wrong Mouse Positions" New topic
Author

Wrong Mouse Positions

Steven Joost
Greenhorn

Joined: Sep 29, 2012
Posts: 28
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

Joined: Aug 07, 2007
Posts: 2317
    
  49
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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).


Steve
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i agree. not enough information.

however, you don't draw in a JFrame


SCJP
Visit my download page
Steven Joost
Greenhorn

Joined: Sep 29, 2012
Posts: 28


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

Joined: Sep 29, 2012
Posts: 28
if i remove the /100 at the q variable in the paint method, i dont work either
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Aug 07, 2007
Posts: 2317
    
  49
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

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Steven Joost
Greenhorn

Joined: Sep 29, 2012
Posts: 28
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

Joined: May 03, 2008
Posts: 4642
    
    5

Have you read the API for MouseEvent? There are two methods that return a Point. One of those is what you need.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wrong Mouse Positions