【发布时间】:2014-12-22 15:07:46
【问题描述】:
由于我是 Java 新手,所以我想编写一个简单的 2D 游戏,您可以在其中走动于由 16x16 图形组成的 2D 世界。我已经找到了一种创建纹理 JPanel 的方法:
public class TexturedPanel extends JPanel {
private Image image;
private boolean tile;
TexturedPanel(Image image) {
this.image = image;
this.tile = true;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(tile) {
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0) {
for(int x = 0; x < getWidth(); x += iw) {
for(int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
}
} else {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}
现在我必须决定是继续使用这种方法还是应该使用另一种方法......
那么有没有更好(更快/更简单)的方法来构建带纹理的 JPanel?
提前致谢,马文
【问题讨论】:
标签: java swing jpanel textures paintcomponent