Toolkit.getDefaultToolkit()
は、現在の画面サイズを取得するなど、あなたが必要なものをご提供する方法を、持っている:
private void makeFrameFullSize(JFrame aFrame)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
aFrame.setSize(screenSize.width, screenSize.height);
}
この投稿はResize image while keeping aspect ratio in Javaについて説明します。
これで画面のサイズがわかったので、現在のフレームサイズとフルスクリーンサイズの差の%で、画像の高さ/幅と現在のフレームサイズの比率を計算できます。ここ
private Dimension get getFrameToScreenRatio(Frame aFrame){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
return dimension.setSize(aFrame.getWidth()/dimension.getWidth(), aFrame.getHeight()/dimension.getHeight());
}
は(背景画像など)キャンバスに合うように画像をスケーリングすることができる例示的なユーティリティクラスであると適合するようにスケール:
パッケージ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);
}
ありがとう!私はそれを知っていましたが、フレームのサイズは変更されますが、内部のコンポーネントは変更されません(?)。大きな問題は私が画像を扱うことです。私は本当にどこからそれを取るか分からない。 –