概要
TexturePaint
を使用して背景にタイル状に画像を貼り付けます。
サンプルコード
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);
}
}
view all解説
このサンプルでは、BufferedImage
からTexturePaint
を生成し、これをGraphics2D#setPaint
メソッドで設定してパネル全体を塗りつぶしています。