Public Group

# Finding Coordinates in a JFrame

This topic is 2589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

Im currently programing a simple circle in a (200 by 400) JFrame in java. Im having trouble bounding my circle within the JFrame meaning when I move it to the right Im not sure how I can keep if from moving off screen. I also am having trouble figuring out how to calculate different points within the JFrame meaning if I wanted to position the circle at the right side without having it go off screen how would I calculate that coordinate or if I wanted to position at the exact center of the JFrame how could I calculate that? Any help would be appreciated.

##### Share on other sites
This might help you: public void mouseClicked(MouseEvent e) { PointerInfo a = MouseInfo.getPointerInfo(); Point point = new Point(a.getLocation()); SwingUtilities.convertPointFromScreen(point, e.getComponent()); x=(int) point.getX(); y=(int) point.getY();}

##### Share on other sites
Can you show us how you are rending the circle currently?

Swing components have width and height properties. Using their Graphics to draw rendering occurs relative to their position. So if you ensure that circle.x is always >= circle.radius and circle.x <= component.getWidth() - circle.radius, the circle should always be bounded by the component that is drawing it (assuming circle.(x|y) is the centre of your circle).

The exact centre of the component should be easy to compute from this information.

##### Share on other sites

Can you show us how you are rending the circle currently?

Swing components have width and height properties. Using their Graphics to draw rendering occurs relative to their position. So if you ensure that circle.x is always >= circle.radius and circle.x <= component.getWidth() - circle.radius, the circle should always be bounded by the component that is drawing it (assuming circle.(x|y) is the centre of your circle).

The exact centre of the component should be easy to compute from this information.

Thank you phaelax, and rip off I apologize for not being clear Im actually using an image of a circle as I want to learn how to work with 2D images. Before posting I did what you stated by taking the size of the image and subtracting it from the width of the frame but, that didnt seem to bound it the image still went off the screen slightly. Does finding points to bound an image work differently? I appreciate the help.

##### Share on other sites
In Swing, Images are rendered from top,left to width, height. So yes, you'd need to alter the code such that the image's x and y co-ordinates never become negative, and that imageX <= image.getWidth() + component.getWidth() and imageY <= image.getHeight() + component.getHeight().

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633689
• Total Posts
3013337
×