---
category: swing
folder: TexturePaint
title: TexturePaintを使って背景に画像を表示
tags: [TexturePaint, BufferedImage, Graphics2D]
author: aterai
pubdate: 2004-09-20T16:06:29+09:00
description: TexturePaintを使用して背景にタイル状に画像を貼り付けます。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTVUeXC5lI/AAAAAAAAAnc/CWUYfOODy1E/s800/TexturePaint.png
---
* 概要 [#summary]
`TexturePaint`を使用して背景にタイル状に画像を貼り付けます。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTVUeXC5lI/AAAAAAAAAnc/CWUYfOODy1E/s800/TexturePaint.png)

* サンプルコード [#sourcecode]
#code(link){{
BufferedImage bi = null;
try {
  bi = ImageIO.read(getClass().getResource("16x16.png"));
} catch (IOException ioe) {
  ioe.printStackTrace();
}
texture = new TexturePaint(bi, new Rectangle(bi.getWidth(), bi.getHeight()));
panel = new JPanel() {
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setPaint(texture);
    g2.fillRect(0, 0, getWidth(), getHeight());
    g2.dispose();
    super.paintComponent(g);
  }
}
}}

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

* 参考リンク [#reference]
- [[JPanelの背景に画像を並べる>Swing/BackgroundImage]]

* コメント [#comment]
#comment
#comment