Reputation:
I am just about to have this app ready to distribute. However i still haven't found out a way to make a app suitable for whatever screen. In fact I am using pngs file for background and buttons (mainly) into JLabels, and this is a huge problem i think. What i would like to achieve is to have the JFrame and its components resizable but maintaining the proportions, just like for a responsive website! Other ways i was planning of creating pngs of the originals but smaller and then create new frames and while booting the app ask the system what frame should suite the screen best and use it (but i know i would not be great and then of course os sometimes use screen resolution in different ways making is appear smaller or larger, if you know what i mean). (I am using Netbeans).
Thank you a lot, i am looking foreword to discuss with you this issue that i am sure will concern many others.
Upvotes: 0
Views: 2402
Reputation: 7867
Toolkit.getDefaultToolkit()
has methods that provide you with what you need, including getting the current screen size:
private void makeFrameFullSize(JFrame aFrame)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
aFrame.setSize(screenSize.width, screenSize.height);
}
This post discusses Resize image while keeping aspect ratio in Java.
Now that you have the screen size, you can calculate the ratio of image height/width to current frame size and scale on the % difference of current frame size to full screen size.
private Dimension get getFrameToScreenRatio(Frame aFrame){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
return dimension.setSize(aFrame.getWidth()/dimension.getWidth(), aFrame.getHeight()/dimension.getHeight());
}
Here is an example utility class that can scale an image to fit a canvas (like a background image) and scales it to fit:
package net.codejava.graphics;
import java.awt.Component; import java.awt.Graphics; import java.awt.Image;
/**
* This utility class draws and scales an image to fit canvas of a component.
* if the image is smaller than the canvas, it is kept as it is.
*
* @author www.codejava.net
*
*/
public class ImageDrawer {
public static void drawScaledImage(Image image, Component canvas, Graphics g) {
int imgWidth = image.getWidth(null);
int imgHeight = image.getHeight(null);
double imgAspect = (double) imgHeight / imgWidth;
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
double canvasAspect = (double) canvasHeight / canvasWidth;
int x1 = 0; // top left X position
int y1 = 0; // top left Y position
int x2 = 0; // bottom right X position
int y2 = 0; // bottom right Y position
if (imgWidth < canvasWidth && imgHeight < canvasHeight) {
// the image is smaller than the canvas
x1 = (canvasWidth - imgWidth) / 2;
y1 = (canvasHeight - imgHeight) / 2;
x2 = imgWidth + x1;
y2 = imgHeight + y1;
} else {
if (canvasAspect > imgAspect) {
y1 = canvasHeight;
// keep image aspect ratio
canvasHeight = (int) (canvasWidth * imgAspect);
y1 = (y1 - canvasHeight) / 2;
} else {
x1 = canvasWidth;
// keep image aspect ratio
canvasWidth = (int) (canvasHeight / imgAspect);
x1 = (x1 - canvasWidth) / 2;
}
x2 = canvasWidth + x1;
y2 = canvasHeight + y1;
}
g.drawImage(image, x1, y1, x2, y2, 0, 0, imgWidth, imgHeight, null);
}
Upvotes: 1