
| VM | JDK と JRE |
| Language | オブジェクトとインスタンス this と super static フィールドアクセス |
| IO | Serializable の実装 |
| Swing | MetalLookAndFeel イメージパネル |
| JavaBeans | プロパティ名について XMLEncoder で保存 |
| その他 | 正規表現テストアプリ 秀丸の強調表示 Ant のインストール 文字セット変換 Ant タスク XAMPP + Tomcat |
|
イメージパネル |
ありふれてるかもしれませんが ImagePanel
というイメージを背景とする JPanel を作りました。
JLabel との違いは設定されたイメージはパネルのサイズに合わせて拡大縮小、あるいは
テクスチャパターンとして並べられ、常にパネル全体を覆う点です。
下のテストコードやイメージファイルと共に ImagePanel のソースコードをまとめた
ZIP ファイルを
こちらからダウンロード
できます。
下は ImagePanel を使用した例でイメージを背景にした JTextArea です。
ImagePanel に透明な JTextArea を貼り付けています。
最大化した場合には背景のイメージも拡大されます。
こちらはテクスチャパターンを使っています。
オーロラのイメージを背景にした方のテキストエリアのソースコードです。
テクスチャパターンを使う方はコメントアウトしている方を有効にします。
スクロールペインを使う場合にはビューポートも透明する必要があります。
/************************** TestFrame.java **************************/
import java.awt.*;
import javax.swing.*;
public class TestFrame1 extends JFrame {
public TestFrame1() {
super("ImagePanel - Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea ta = new JTextArea();
ta.setOpaque(false);
ta.setForeground(Color.CYAN);
ta.setCaretColor(Color.WHITE);
// ta.setForeground(Color.BLACK);
// ta.setCaretColor(Color.BLACK);
ta.setFont(new Font("Monospaced", Font.PLAIN, 15));
ta.setLineWrap(true);
JScrollPane sp = new JScrollPane(ta);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);
JPanel imagePanel = new ImagePanel("aurora.png",
ImagePanel.DrawType.RESIZE);
/*
JPanel imagePanel = new ImagePanel("bg1.jpg",
ImagePanel.DrawType.PATTERN);
*/
imagePanel.setLayout(new BorderLayout());
imagePanel.add(sp);
getContentPane().add(imagePanel, BorderLayout.CENTER);
}
//------------------------- main -------------------------
public static void main(String[] args) {
JFrame frame = new TestFrame1();
frame.setSize(600, 400);
frame.setVisible(true);
}
}
上のイメージはテクスチャパターンをイメージファイルからではなく
グラフィックコンテキストを使って作成しています。
以下がソースコードです。
/************************** TestFrame.java **************************/
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class TestFrame3 extends JFrame {
public TestFrame3() {
super("ImagePanel - Test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea ta = new JTextArea();
ta.setOpaque(false);
ta.setForeground(Color.WHITE);
ta.setCaretColor(Color.WHITE);
ta.setFont(new Font("Monospaced", Font.PLAIN, 15));
ta.setLineWrap(true);
JScrollPane sp = new JScrollPane(ta);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);
//TexturePaintオブジェクトの作成
BufferedImage textureImg = new BufferedImage(20, 20,
BufferedImage.TYPE_INT_RGB);
Graphics g = textureImg.createGraphics();
g.setColor(new Color(128, 0, 0));
g.fillRect(0, 0, 20, 20);
g.setColor(new Color(32, 32, 32));
g.fillRect(9, 0, 2, 8);
g.fillRect(0, 8, 20, 2);
g.fillRect(18, 10, 2, 8);
g.fillRect(0, 18, 20, 2);
g.dispose();
Rectangle rect = new Rectangle(0, 0, 20, 20);
TexturePaint tp = new TexturePaint(textureImg, rect);
//テクスチャパターンで描画
ImagePanel imagePanel = new ImagePanel(tp);
imagePanel.setLayout(new BorderLayout());
imagePanel.add(sp);
getContentPane().add(imagePanel, BorderLayout.CENTER);
}
//------------------------- main -------------------------
public static void main(String[] args) {
JFrame frame = new TestFrame3();
frame.setSize(600, 400);
frame.setVisible(true);
}
}