• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextAreaの背景に画像を表示
#navi(../)
*JTextAreaの背景に画像を表示 [#s334455a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2006-03-06~
更新日:&lastmod;

#contents

**概要 [#n3a100ea]
JTextAreaの背景に画像を表示しています。[[Java Forums - How can I use TextArea with Background Picture ?>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=567904]]のようにJava Forumでは定番のコードを引用しています。
JTextAreaの背景に画像を表示しています。[[How can I use TextArea with Background Picture ?>http://forum.java.sun.com/thread.jspa?threadID=567904]]のようにJava Forumでは定番のコードを引用しています。

#screenshot

**サンプルコード [#c5694631]
#code{{
 class CentredBackgroundBorder implements Border {
   private final BufferedImage image;
   public CentredBackgroundBorder(BufferedImage image) {
     this.image = image;
   }
   public void paintBorder(Component c, Graphics g,
       int x, int y, int width, int height) {
     x += (width-image.getWidth())/2;
     y += (height-image.getHeight())/2;
     ((Graphics2D) g).drawRenderedImage(image, 
       AffineTransform.getTranslateInstance(x,y));
   }
   public Insets getBorderInsets(Component c) {
     return new Insets(0,0,0,0);
   }
   public boolean isBorderOpaque() {
     return true;
   }
 }

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

**解説 [#z741df6b]
画像を中央に表示するようにしたBorderを作成し、これをViewportのBorderとして設定しています。上記のサンプルでは、JTextAreaの背景に適用していますが、JDesktopPaneや、その他のJComponentでも同様に使用することが出来ます。

**参考リンク [#k26904ee]
-[[Java Forums - How can I use TextArea with Background Picture ?>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=567904]]
-[[How can I use TextArea with Background Picture ?>http://forum.java.sun.com/thread.jspa?threadID=567904]]
-[[デジタル出力工房 絵写楽>http://www.bekkoame.ne.jp/~bootan/free2.html]]

**コメント [#tc6f84b8]
- centreは英式のcenterのことらしいです。 -- [[terai]] &new{2006-03-23 (木) 00:00:58};

#comment