2
\$\begingroup\$

enter image description here

I am currently making a 2D game using Java (Java 2D). I want to make my game resizable. If you play any game on an emulator (e.g. legend of Zelda in fceux emulator) in full screen mode, you'll see that the 8-bit or 16-bit game re-sizes to fit the screen size. How can I implement the same thing in my game programatically?

\$\endgroup\$
3
\$\begingroup\$

I'm assuming you're using the standard Java 2D API, not OpenGL.

In that case, you render your graphics to a buffer image first. Then, to render that image to the screen, you can either use a Graphics.drawImage call that allows resizing your buffered image:

public abstract boolean drawImage(Image img,        // the buffer image
                int dx1, int dy1, int dx2, int dy2, // 0, 0, screen.w, screen.h
                int sx1, int sy1, int sx2, int sy2, // 0, 0, image.w, image.h
                ImageObserver observer)             // null is fine

or, if you want to have more control over the scaling algorithm, you can first convert the image to a second buffered image using the following method of the Image class:

public Image getScaledInstance(int width, int height, int hints)

Here, the parameter hints is one of

Image.SCALE_DEFAULT
Image.SCALE_FAST
Image.SCALE_SMOOTH
Image.SCALE_REPLICATE
Image.SCALE_AREA_AVERAGING

See the API doc for details. For bigger games, you might want to keep an eye on performance doing these rescales, but for typical 8-bit resolutions you should probably be fine.

\$\endgroup\$
  • \$\begingroup\$ yay...nice answer. "getscaledinstance" is working awesome. \$\endgroup\$ – arandomguy Sep 17 '14 at 17:57
  • \$\begingroup\$ i REALLY want to urge you to not use getScaledInstance! read this article about the perils of ScaledInstance today.java.net/pub/a/today/2007/04/03/… \$\endgroup\$ – Martin Frank Sep 18 '14 at 5:15
  • \$\begingroup\$ ScaledInstance has been discussed on StackOverflow for many times.. so condsider using drawImage instead! \$\endgroup\$ – Martin Frank Sep 18 '14 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.