Sign in to follow this  
Zaxx

[java] Resize graphics when resize window

Recommended Posts

Hello. I've been thinking about how to separate the coordinate space of my game area and the coordinate space of my window. What I mean is this: when you make a JPanel you can specify its size in pixels. When you draw things to this panel, the coordinates you use correspond to pixels, even when you use floats or doubles. What I was wondering was if it's possible to create an independant coordinate space my game objects exist in, which is stretched to fit the JPanel. So say I create a space with an arbitrary 100 x 100 rectangular bound and create a 50 x 50 ellipse placed at the coordinates (25, 25) of my bound. I want to draw this space to a 700 x 700 JPanel, and for the space to be stretched to fill the JPanel but have all the lines and points have the same orientation as they did in the virtual space (so in this case, everything just zooms in). This is attractive to me since I wouldn't have to worry about the moniter's resolution and that I could resize my window as much as I want but not affect the orientations of the game objects. I could even make the window rectangular and preserve the virtual coordinates for the game objects, though everything would look squished on screen. I'm making a vector game, so I'm just using points and lines. I'm not worried about image sprites or things like that. Of course, I'm not really sure how to do this exactly. I know Java2D makes a distinction between user space and device space. I also know how Graphics2D objects have methods like shear() and scale(), though I haven't tried them yet. Anybody else have some ideas?

Share this post


Link to post
Share on other sites
why yes, it's quite simple

given:
double world_width, world_height;
int panel_width, panel_height;
Graphics2D g;

before any rendering, call:
g.scale((double)panel_width/world_width, (double)panel_height/world_height);


the typecast to double probably isn't necessary.

Share this post


Link to post
Share on other sites
Thanks capn_midnight.

I noticed that if you're rendering each frame (animation), it's better to use an AffineTransform:


AffineTransform at = new AffineTransform();
at.scale((double)pWidth / boundsWidth(), (double)pHeight / boundsHeight());

g.setTransform(at);



Otherwise I think it just keeps zooming in each frame.

Share this post


Link to post
Share on other sites
It depends on how you do your rendering. If you are controlling the calls to the rendering method, and use only one graphics object the entire time, then yes, it will continue to zoom in all the time. However, if you are using a thread timer and calling the frames repaint method, then you are using a new Graphics2D object for every rendered frame, and the transform is reset each time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this