JOptionPaneの背景画像を設定する方法は?JOptionPaneの背景に別の画像を表示したい。
5008 次
1 に答える
1
JOptionPaneクラスを拡張して、paintメソッドをオーバーライドすることができます。
編集:画像の解像度と品質によっては、WindowResizeイベント中にAffineTransformを使用して、歪みをあまり発生させずに画像を引き伸ばすことができる場合があります。これにより、以下で説明するJOptionPaneと画像サイズの不一致を処理できます。
class ImageBackgroundPane extends JOptionPane
{
private BufferedImage img;
public ImageBackgroundPane (BufferedImage image)
{
this.img = image;
}
@Override
public void paint(Graphics g)
{
//Pick one of the two painting methods below.
//Option 1:
//Define the bounding region to paint based on image size.
//Be careful, if the image is smaller than the JOptionPane size you
//will see a solid white background where the image does not reach.
g.drawImage(img, 0, 0, img.getWidth(), img.getHeight());
//Option 2:
//If the image can be guaranteed to be larger than the JOptionPane's size
Dimension curSize = this.getSize();
g.drawImage(img, 0, 0, curSize.width, curSize.height, null);
//Make sure to paint all the other properties of Swing components.
super.paint(g);
}
}
于 2010-04-13T13:22:11.823 に答える