Swing/CentredBackgroundBorder のバックアップの現在との差分(No.8)
TITLE:JTextAreaの背景に画像を表示
Posted by aterai at 2006-03-06
JTextAreaの背景に画像を表示
JTextAreaの背景に画像を表示しています。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:
概要
JTextArea
などのコンポーネントの背景に、Border
を使って中心揃えした画像を表示します。Swing - How can I use TextArea with Background Picture ?のコードを引用しています。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
class CentredBackgroundBorder implements Border {
private final Insets insets = new Insets(0, 0, 0, 0);
private final BufferedImage image;
#spanadd
#spanend
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));
#spanadd
#spanend
@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();
}
public Insets getBorderInsets(Component c) {
return new Insets(0,0,0,0);
#spanadd
#spanend
@Override public Insets getBorderInsets(Component c) {
return insets;
}
public boolean isBorderOpaque() {
#spanadd
#spanend
@Override public boolean isBorderOpaque() {
return true;
}
}
解説
画像を中央に表示するようにしたBorderを作成し、これをViewportのBorderとして設定しています。上記のサンプルでは、JTextAreaの背景に適用していますが、JDesktopPane(参考:JInternalFrameを半透明にする)や、その他のJComponentでも同様に使用することが出来ます。解説
上記のサンプルでは、以下のようにしてJTextArea
の背景に画像を表示しています。
参考リンク
- Swing - How can I use TextArea with Background Picture ?
- JInternalFrameを半透明にする
- デジタル出力工房 絵写楽
- 画像を中央に表示するようにした
Border
を作成してこれをViewport
に設定 -
Viewport
に追加したJTextArea
の背景を透明化#spanend #spanadd textarea.setOpaque(false); #spanend #spanadd textarea.setBackground(new Color(0x0, true))); #spanend #spanadd
コメント
- centreは英式のcenterのこと?らしいです。 -- aterai
-
Border
を使って背景に画像を表示する方法はJDesktopPane
(参考: JInternalFrameを半透明にする)や、その他のJComponent
でも使用可能