JTextAreaの背景に画像を表示

編集者:Terai Atsuhiro
作成日:2006-03-06
更新日:2022-01-09 (日) 02:00:12

概要

JTextAreaの背景に画像を表示しています。Java Forums - How can I use TextArea with Background Picture ?のようにJava Forumでは定番のコードを引用しています。

#screenshot

サンプルコード

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;

解説

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

参考リンク

コメント