• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextAreaの背景に画像を表示
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2006-03-06
*JTextAreaの背景に画像を表示 [#s334455a]
JTextAreaの背景に画像を表示しています。[http://forums.sun.com/thread.jspa?threadID=567904 Swing - How can I use TextArea with Background Picture ?]のようにJava Forumでは定番のコードを引用しています。
---
category: swing
folder: CentredBackgroundBorder
title: JTextAreaの背景に画像を表示
tags: [JTextArea, BufferedImage, Border, JViewport]
author: aterai
pubdate: 2006-03-06T19:15:08+09:00
description: JTextAreaなどのコンポーネントの背景に、Borderを使って中心揃えした画像を表示します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIyAIY_mI/AAAAAAAAATU/GovGMBqjzRo/s800/CentredBackgroundBorder.png
---
* 概要 [#summary]
`JTextArea`などのコンポーネントの背景に、`Border`を使って中心揃えした画像を表示します。[https://community.oracle.com/thread/1395763 Swing - How can I use TextArea with Background Picture ?]のコードを引用しています。

-&jnlp;
-&jar;
-&zip;
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIyAIY_mI/AAAAAAAAATU/GovGMBqjzRo/s800/CentredBackgroundBorder.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTIyAIY_mI/AAAAAAAAATU/GovGMBqjzRo/s800/CentredBackgroundBorder.png)

**サンプルコード [#c5694631]
* サンプルコード [#sourcecode]
#code(link){{
class CentredBackgroundBorder implements Border {
  private final Insets insets = new Insets(0, 0, 0, 0);
  private final BufferedImage image;

  public CentredBackgroundBorder(BufferedImage image) {
    this.image = image;
  }
  @Override 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));

  @Override public void paintBorder(
      Component c, Graphics g, int x, int y, int width, int height) {
    int cx = x + (width - image.getWidth()) / 2;
    int cy = y + (height - image.getHeight()) / 2;
    Graphics2D g2 = (Graphics2D) g.create();
    g2.drawRenderedImage(image, AffineTransform.getTranslateInstance(cx, cy));
    g2.dispose();
  }

  @Override public Insets getBorderInsets(Component c) {
    return new Insets(0,0,0,0);
    return insets;
  }

  @Override public boolean isBorderOpaque() {
    return true;
  }
}
}}

**解説 [#z741df6b]
上記のサンプルでは、以下のようにしてJTextAreaの背景に画像を表示しています。
* 解説 [#explanation]
上記のサンプルでは、以下のようにして`JTextArea`の背景に画像を表示しています。

- 画像を中央に表示するようにしたBorderを作成し、これをViewportのBorderとして設定
- Viewportに追加したJTextAreaの背景を透明化
-- textarea.setOpaque(false);
-- textarea.setBackground(new Color(0,0,0,0)));
- 画像を中央に表示するようにした`Border`を作成してこれを`Viewport`に設定
- `Viewport`に追加した`JTextArea`の背景を透明化
#code{{
textarea.setOpaque(false);
textarea.setBackground(new Color(0x0, true)));
}}

----
Borderを使って背景に画像を表示する方法は、JDesktopPane(参考:[[JInternalFrameを半透明にする>Swing/TransparentFrame]])や、その他のJComponentでも使用することが出来ます。
- `Border`を使って背景に画像を表示する方法は`JDesktopPane`(参考: [[JInternalFrameを半透明にする>Swing/TransparentFrame]])や、その他の`JComponent`でも使用可能

**参考リンク [#k26904ee]
-[http://forums.sun.com/thread.jspa?threadID=567904 Swing - How can I use TextArea with Background Picture ?]
-[[JInternalFrameを半透明にする>Swing/TransparentFrame]]
-[http://www.bekkoame.ne.jp/~bootan/free2.html デジタル出力工房 絵写楽]
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1395763 Swing - How can I use TextArea with Background Picture ?]
- [[JInternalFrameを半透明にする>Swing/TransparentFrame]]
- [http://www.bekkoame.ne.jp/~bootan/free2.html デジタル出力工房 絵写楽]

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

#comment