TITLE:TexturePaintを使って背景に画像を表示

Posted by at 2004-09-20

TexturePaintを使って背景に画像を表示

`TexturePaint`を使用して背景にタイル状に画像を貼り付けます。

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

サンプルコード

BufferedImage bi = null;
try {
  bi = ImageIO.read(getClass().getResource("16x16.png"));
}catch(IOException ioe) {
  ioe.printStackTrace();
}
texture = new TexturePaint(img, new Rectangle(bi.getWidth(), bi.getHeight()));
panel = new JPanel() {
  @Override public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setPaint(texture);
    g2.fillRect(0, 0, getWidth(), getHeight());
    super.paintComponent(g);
  }
}
View in GitHub: Java, Kotlin

解説

このサンプルでは、`BufferedImageからTexturePaintを生成し、これをGraphics2D#setPaint`メソッドで設定してパネル全体を塗りつぶしています。

参考リンク

コメント