【发布时间】:2011-06-07 03:57:46
【问题描述】:
我正在制作一款 2D 垂直射击游戏,其中除了图形之外的所有内容都已编码(并且可以正常工作)。我以前没有使用过 Graphics 类,所以这对我来说是全新的。以下是我用来将所有内容绘制到 JFrame 的代码:
public void paintAll()
{
Graphics h = new Graphics2D();
for(Bullet j : GameState.getEnBullets()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
for(Enemy j : GameState.getEnemies()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
for(Bullet j : GameState.getPlayBullets()){
h.drawImage(j.getImage(),j.getX(), j.getY(), null);}
this.paint(h);
}
第一行“图形 h = new Graphics2D();”产生错误,因为 Graphics2d 是抽象的,但我不知道从哪里开始。
我需要代码来获取我拥有的所有图像并将它们绘制到 JFrame 中的点。我提醒您,我以前从未这样做过,所以请告诉我这样做是否是错误的方法。
【问题讨论】:
-
与其对
JFrame本身进行自定义绘制,不如添加JComponent或JPanel(如果要包含其他组件)。就在您认为在顶级容器中绘制最好的时候,您意识到您希望在全屏JWindow,或JDialog,或JInternalFrame,或CENTER中进行渲染BorderLayout在另一个JPanel中,或者..
标签: java swing graphics jframe paintcomponent