Toolkit.getDefaultToolkit()
hat Methoden, die Sie mit dem, was Sie benötigen, einschließlich der aktuelle Bildschirmgröße erhalten:
private void makeFrameFullSize(JFrame aFrame)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
aFrame.setSize(screenSize.width, screenSize.height);
}
Dieser Beitrag Resize image while keeping aspect ratio in Java diskutiert.
Jetzt, da Sie die Bildschirmgröße haben, können Sie das Verhältnis von Bildhöhe/-breite zur aktuellen Bildgröße berechnen und auf den% Unterschied der aktuellen Bildgröße zur Vollbildgröße skalieren.
private Dimension get getFrameToScreenRatio(Frame aFrame){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
return dimension.setSize(aFrame.getWidth()/dimension.getWidth(), aFrame.getHeight()/dimension.getHeight());
}
Hier ist ein Beispiel Utility-Klasse, die ein Bild skalieren kann eine Leinwand (wie ein Hintergrundbild) zu passen, und skaliert sie passen:
Paket net.codejava.graphics;
importieren java.awt.Component; Import java.awt.Graphics; importieren Sie 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);
}
Vielen Dank! Ich wusste das, aber es passt nur den Rahmen an, aber nicht seine Komponenten innerhalb (?). Das Hauptproblem besteht darin, dass ich mich mit den Bildern beschäftige. Ich weiß nicht, wo ich es hinkriege. –