# [java] Resize graphics when resize window

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

## 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 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 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 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 on other sites
You can also look at OpenGL. When you use OpenGL for 2D, you can specify your coordinate space independant of the resolution.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 14
• ### Forum Statistics

• Total Topics
632155
• Total Posts
3004477

×