TITLE:JPanelの背景に画像を並べる

Usage: #tags(tags)
Posted by at 2004-09-13

JPanelの背景に画像を並べる

JPanelの背景に画像をタイル状に並べて表示します。

  • &jnlp;
  • &jar;
  • &zip;
BackgroundImage.png

サンプルコード

@Override public void paintComponent(Graphics g) {
  Dimension d = getSize();
  int w = bgimage.getIconWidth();
  int h = bgimage.getIconHeight();
  for(int i=0;i*w<d.width;i++) {
    for(int j=0;j*h<d.height;j++) {
      g.drawImage(bgimage.getImage(), i*w, j*h, w, h, this);
    }
  }
//for(int x=0;x<d.width;x+=w) {
//  for(int y=0;y<d.height;y+=h) {
//    g.drawImage(bgimage.getImage(), x, y, w, h, this);
//  }
//}
  super.paintComponent(g);
}
View in GitHub: Java, Kotlin

解説

このサンプルでは、単純にImage(画像)を順番に並べて描画しています。コンポーネントの背景色を透明にしてから、super.paintComponent(g)する必要があります。

参考リンク

コメント