• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:TexturePaintを使って背景に画像を表示
#navi(../)
RIGHT:Posted by [[terai]] at 2004-09-20
*TexturePaintを使って背景に画像を表示 [#vf616c16]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-09-20~
更新日:&lastmod;

#contents

**概要 [#ff4c7c60]
TexturePaintを使用して背景にタイル状に画像を貼り付けます。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#u3a94734]
#code{{
URL url = getClass().getResource("16x16.png");
BufferedImage img = null;
try {
  img = ImageIO.read(url);
}catch(IOException ioe) {
  ioe.printStackTrace();
}
Rectangle2D r2d = new Rectangle2D.Double(0,0,img.getWidth(),img.getHeight());
texture = new TexturePaint(img, r2d);
 public void paintComponent(Graphics g) {
}}

#code{{
public void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D)g;
  g2.setPaint(texture);
  g2.fillRect(0, 0, getWidth(), getHeight());
  super.paintComponent(g);
}
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#j85ba7cb]
このサンプルでは、BufferedImageからTexturePaintを生成し、これをGraphics2D#setPaintメソッドで設定してパネル全体を塗りつぶしています。

**参考リンク [#nc4c81eb]
-[[JPanelの背景に画像を並べる>Swing/BackgroundImage]]

**コメント [#e3931dfe]
#comment