さて、最も迅速で簡単な解決策は、使用することですImage.getScaledInstance
g.drawImage(img.getScaledInstance(newWidth, -1, Image. SCALE_SMOOTH), x, y, this);
負の数について疑問がある場合は、Java ドキュメントに次のように記載されています。
幅または高さのいずれかが負の数の場合、元の画像の寸法の縦横比を維持するために値が代入されます。幅と高さの両方が負の場合、元の画像の寸法が使用されます。
アップデート
補足として(私のGoogleは再生していました)。
getScaledInstance
最速または最高品質のアプローチではありませんが、最も簡単です。
その他のアイデアについては、 The Perils of Image.getScaledInstanceをお読みください。
アップデート
領域に合わせて画像をスケーリングすることは、単純に縦横比をスケーリングするよりも少し複雑です。画像を領域内に「合わせる」(おそらくその周囲に空白領域を残す) か、領域を「塗りつぶす」(最小寸法が領域の最大寸法に合うようにする) かを選択する必要があります。


フィット&フィル
基本的に、私はスケールファクターで作業します
これは、特定のサイズの倍率を返します。これを使用して、必要なアルゴリズムに基づいて使用する要素を決定します
public static double getScaleFactor(int iMasterSize, int iTargetSize) {
double dScale = 1;
if (iMasterSize > iTargetSize) {
dScale = (double) iTargetSize / (double) iMasterSize;
} else {
dScale = (double) iTargetSize / (double) iMasterSize;
}
return dScale;
}
この2つの方法で使用されます。彼らは単に2Dimension
秒かかります。オリジナルとターゲット。
public static double getScaleFactorToFit(Dimension original, Dimension toFit) {
double dScale = 1d;
if (original != null && toFit != null) {
double dScaleWidth = getScaleFactor(original.width, toFit.width);
double dScaleHeight = getScaleFactor(original.height, toFit.height);
dScale = Math.min(dScaleHeight, dScaleWidth);
}
return dScale;
}
public static double getScaleFactorToFill(Dimension masterSize, Dimension targetSize) {
double dScaleWidth = getScaleFactor(masterSize.width, targetSize.width);
double dScaleHeight = getScaleFactor(masterSize.height, targetSize.height);
double dScale = Math.max(dScaleHeight, dScaleWidth);
return dScale;
}
画像を (直接またはサポート メソッドを介して) 渡すのは比較的簡単です。たとえば、paint
メソッド内からこれを呼び出すことができます
double factor getScaledFactorToFit(new Dimension(image.getWidth(), image.getHeight()), getSize());
int scaledWidth = image.getWidth() * scale;
int scaledHeight *= image.getWidth() * scale;
これにより、アスペクト比が自動的に処理されます;)
拡張された例で更新
public double getScaleFactor(int iMasterSize, int iTargetSize) {
double dScale = 1;
if (iMasterSize > iTargetSize) {
dScale = (double) iTargetSize / (double) iMasterSize;
} else {
dScale = (double) iTargetSize / (double) iMasterSize;
}
return dScale;
}
public double getScaleFactorToFit(Dimension original, Dimension toFit) {
double dScale = 1d;
if (original != null && toFit != null) {
double dScaleWidth = getScaleFactor(original.width, toFit.width);
double dScaleHeight = getScaleFactor(original.height, toFit.height);
dScale = Math.min(dScaleHeight, dScaleWidth);
}
return dScale;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
double scaleFactor = Math.min(1d, getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), getSize()));
int scaleWidth = (int) Math.round(image.getWidth() * scaleFactor);
int scaleHeight = (int) Math.round(image.getHeight() * scaleFactor);
Image scaled = image.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);
int width = getWidth() - 1;
int height = getHeight() - 1;
int x = (width - scaled.getWidth(this)) / 2;
int y = (height - scaled.getHeight(this)) / 2;
g.drawImage(scaled, x, y, this);
}