Canvas and Images Resizing Proportionately?

Ok, my next problem is this.

If a user click on the maximise button at the top of the window, or drags the frame to make it bigger, how can I have it so that my images will resize proportionately with the change in width or height the user has made.

Or should I just leave it a fixed window with the maximise button disabled.

I don’t even know where to start on this problem so any advice would be great.

So you want to be able to resize the window while maintaining the aspect ratio of the canvas?

I guess he first wants to know how to zoom out of his 2D game world. He wants to resize the images and change the position of the sprites to make that happen.

I guess you should take a look at your [icode]glOrtho(…)[/icode]. (Are you using OpenGL? :X )
I’m pretty sure it’s possible with glOrtho.

I’m using Java2D, which probably makes it a little bit more awkward.

And I’d like to be able to do both, to be able to zoom out my 2D world for such things like a map, or minimap and to be able to keep aspect ratio if the user wants to resize the window.

Like I said, I don’t even know where to start.

Take a look at AffineTransforms.

They are resposible for translating, scaling, and rotating BufferedImages.
You could also just use the [icode]g.drawImage(buffImg, x, y, width, height);[/icode] methods, which take width and height parameters. They automatically “stretch” the image, so they have the desired width and height.

Or you could just switch to OpenGL :slight_smile:

Yeah, as matheus says, you can use Graphics.drawImage to scale your canvas. :slight_smile: Here’s an article that explains how you would maintain the aspect ratio and also calculate the margins.

You have to think about what you want to do with the new available space you get from expanding your window.

you could show more of the map or whatever you rendering (no scaling neccssary)
you could scale your your images up to fill the space (you will have to scale BuffereImages e.g. using AffineTransforms)
This could get complicated especially if your images have the resolutions matching your smaller (regular) screen. In this case you have to use interpolation (AffineTransforms have that option if I remember correctly)

You have to be more specific what you actually want to do with that space

greenOwl

Ok, I’m not really using a graphic engine as such, so I’m guessing showing more of the map would be better.

I’ve decided that, I could have two variables like

int tilewidth, tileheight;

And then when I render my tiles I can do:

g.drawImage(img, x*tilewidth, y*tileheight, tilewidth, tileheight);

Then if the user scrolls the mouse wheel, then I can have tilewidth and tileheight to change or I could simply have a little icon for zooming in or out. I would need to restrict how far to zoom in, otherwise I could end up with a single tile taking up 640x400 or whatever resolution I finally end up with.

If the user resizes the window, will it automatically render more of my map?

Use Java2D full screen or fake full screen and scale your image a fixed amount. This way you control the size of the screen yourself.

Yes, I suppose using Fullscreen would eliminate the fear of the user resizing the window.

Thanks for the advice and suggestions everyone!