Jump to content

Rendering an image which fills the screen but does not get stretched


Recommended Posts

Hello. In the process of creating a GUI, I went on trying to give it a custom background. It works well except that, the image stretches itself to fit on the screen, which makes it look weird and impractical. I wonder if there was a way to make it fill the screen without being stretched. Pretty similar to how it appears here: unknown.png. I'm mainly using Gui#drawModalRectWithCustomSizedTexture() to draw the resource (I tried using the BufferBuilder which roughly brought the same exact result).


The image I would like to render has dimensions of 1280x720. I don't know if any other dimension would look better, so if you are aware of any more appropriate size then I'd love to know.


Which looks on screen like that:


(Blacked out parts are just hidden, however they are a part of the screen. The gradients are extra effects.).


Here's the code I'm using:

Gui.drawModalRectWithCustomSizedTexture(0, 0, scaledResolution.getScaledWidth(), scaledResolution.getScaledHeight(), scaledResolution.getScaledWidth(), scaledResolution.getScaledHeight(), scaledResolution.getScaledWidth(), scaledResolution.getScaledHeight());

I tried replacing all references to scaledResolution with the image's dimensions, which did not really change anything.


Any idea on how to make it display normally without being stretched?

Link to comment
Share on other sites

4 hours ago, diesieben07 said:

Thanks! It now renders much better.

Here's the code in case anyone ran into the same problem:

float imgRatio = (720F / 1280F);       
float containerRatio = ((float) sr.getScaledHeight() / (float) sr.getScaledWidth());
float finalWidth;
float finalHeight;
if (containerRatio > imgRatio) {
    finalHeight = sr.getScaledHeight();
    finalWidth = (sr.getScaledHeight() * imgRatio);
} else {
    finalHeight = (sr.getScaledWidth() * imgRatio);
    finalWidth = sr.getScaledWidth();
finalWidth += 400F; // Extend the image display by 400 pixels
Gui.drawModalRectWithCustomSizedTexture(0, 0, finalWidth, finalHeight, (int) finalWidth, (int) finalHeight, finalWidth, finalHeight);
Edited by Reflxction
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.