0

I need to render a large amount of images for my game and they need to be scaled to any aspect ration to fit the screen and I can't find a solution anywhere.

It already renders the image just not in the correct aspect ratio.

Rendering code:

class _canvas extends JComponent {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (renderable_object part : objs) {  
            if (part.type == 5) {
                g.drawImage(part.get_Image(), part.i, part.j, 23,23);
            }
        }
    }
}

Renderable object:

class renderable_object {
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;

    int type = 0;

    String file = "";

    Image get_Image() {
        return(Toolkit.getDefaultToolkit().getImage(file));
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sam oakes
  • 67
  • 1
  • 4
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an .. – Andrew Thompson Jul 16 '21 at 06:28
  • 1
    .. `UPPER_CASE_CONSTANT`) and use it consistently. **General Tips:** 1) The method quoted in the title uses 8 numeric arguments whereas the one in the code uses four. When you [edit] to add that MRE / SSCCE, correct the title if needed. Also, every `drawImage(..)` variant I can see in the [docs for `Graphics`](https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/java/awt/Graphics.html#method.summary) specifies an `ImageObserver` - so I don't understand how that code could compile. 2) Scaling images takes some CPU grunt, so best do it once when constructed. Even better, create a .. – Andrew Thompson Jul 16 '21 at 06:33
  • 1
    .. range of different sizes and choose the right size when the constructor is called. These images will generally be of better quality than ones 'resized on the fly'. Especially if they're produced from vector graphics. Which reminds me another way to go (depending on the images) might be to forego the images & instead *draw* them using the Java 2P API. Oh, and.. 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the .. – Andrew Thompson Jul 16 '21 at 06:38
  • 1
    .. [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jul 16 '21 at 06:38

1 Answers1

0

The code you show does not scale the image at all. There are at least two ways you can go from here.

You already found the correct method (looking at the title of the question) to use. With that method you can define the source and destination coordinates and rendering will happen accordingly. It seems it is up to you to ensure the coordinates you demand have the correct aspect ratio.

Another possibility would be to cast Graphics into a Graphics2D, then use https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html#drawImage(java.awt.Image,java.awt.geom.AffineTransform,java.awt.image.ImageObserver) to actually render the image. The scaling and exact positioning of the image can now be done via AffineTransform, which you could obtain by

getScaleInstance​(double sx, double sy).concatenate(getTranslateInstance​(double tx, double ty))

so it might be easier to setup. This way it is also very easy to apply rotation, shearing or other stuff.

Queeg
  • 7,748
  • 1
  • 16
  • 42