4

I am creating a game of space invaders and i would like for all of the images to be in ratio to the screen resolution. This means that it can be used on all screen sizes and all the images will shrink or get bigger so that they fit onto the screen. The game is fullscreen. What is the easiest technique to do this? Also is this the best way to set the size of everything?

Michael Haywood
  • 319
  • 2
  • 4
  • 10
  • How are you displaying the images? – Baz Sep 18 '12 at 10:52
  • perhaps JAI API? Umm, bad idea may be. – Nishant Sep 18 '12 at 10:52
  • You should show us what you've tried. And also, what you mean is scaling. This will reduce the quality of the image as pixels are duplicated to create new information that was not there before. Best approach is to use a fixed resolution (i.e. change screen resolution to your game's resolution). This is what all games do. – m0skit0 Sep 18 '12 at 10:52
  • @m0skit0 Last time I played a game on PC I could chose the resolution, and thats pretty much standard. A fixed resolution would also lock the game into a specific aspect ration (remember screens may be 4:3, 16:9 or 16:10 aspect to just name a few common ones...) – Durandal Sep 18 '12 at 11:28
  • Sure, but that's because the game is made at maximum supported resolution and then images scaled down, which in fact loses less quality than scaling up (in scaling down you remove information, while scaling up you have to create new data). – m0skit0 Sep 18 '12 at 11:35
  • I am using the paint(g) method i think. Sorry im quite new to this. Also how do you change the computers screen resolution? This would save a lot of time by making it so i don't have to put it all to a certain scale. Also i have managed to change the size of the image thank you – Michael Haywood Sep 18 '12 at 19:53

3 Answers3

5

Have you had a look at Image#getScaledInstance() though it has its downfalls(The Perils of Image.getScaledInstance()) in short the problem is:

Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.

due to these downfalls you might want a different way to resize your images, you could try something like this:

import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.RenderingHints;

public class ImgUtils {

public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
    BufferedImage bi = null;
    try {
        ImageIcon ii = new ImageIcon(filename);//path to image
        bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return bi;
}

 }

you'd use it like:

BufferedImage img=new ImgUtils().scaleImage(50,50,"c:/test.jpg");
//now you have a scaled image in img

References:

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    Right, JAI I personally hate. There are so many tweaking and almost always you have to compromise on one thing or other. I do not know why there is no decent image processor in Java that is comparable with imageMagick. – Nishant Sep 18 '12 at 11:21
1

Suppose g is your instance of the Graphics2D object, and theImage is the Image you want to draw, and xFactor and yFactor are the scaling factors for x and y, try:

AffineTransformation scaleTransformation = AffineTransformation.getScaleInstance(xFactor, yFactor);
g.draw(theImage, scaleTransformation, null);
scarfridge
  • 111
  • 2
1
Image getScaledImage(Image Img, int wt, int ht) {
    BufferedImage resizedImg = new BufferedImage(wt, ht, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = resizedImg.createGraphics();

    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(Img, 0, 0, wt, ht, null);
    g2.dispose();

    return resizedImg;
}