Jump to content
  • Advertisement
Sign in to follow this  
Greenbird

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!