コンテンツのバッキング イメージを保持する独自のカスタム コントロールがあります。このバッファのタイプはBufferedImage
です。
注意!バッキング イメージの使用は要件によるものです。内に描くことを教えないでpaintComponent()
現在、次の方法で画像のサイズを変更しています。
@Override
public void setBounds(int x, int y, int width, int height) {
if( bufferedImage == null ) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
else {
if( bufferedImage.getWidth() < width || bufferedImage.getHeight() < height ) {
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
newImage.createGraphics().drawImage(bufferedImage, 0, 0, null);
bufferedImage = newImage;
}
}
super.setBounds(x, y, width, height);
}
残念ながら、これには新しいBufferedImage
オブジェクトを作成する必要があるため、以前に取得したGraphics
オブジェクトが無効になります。
だから私は私自身の方法を持たなければならない
public Graphics2D createImageGraphics() {
if( bufferedImage != null ) {
return bufferedImage.createGraphics();
}
else {
return null;
}
}
オーバーライドしたいのですがgetGraphics()
。
Graphics オブジェクトを節約するために画像のサイズを変更することは可能ですか?