我正在制作一个 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);
}
第一行“Graphics h = new Graphics2D();”产生错误,因为 Graphics2d 是抽象的,但我不知道从这里去哪里。
我需要代码来获取我拥有的所有图像并将它们绘制到 JFrame 中的点。我提醒您,我以前从未这样做过,所以请告诉我这是否是错误的方法。
覆盖paintComponent() http://download.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent%28java.awt.Graphics%29反而;它将提供Graphics
语境。你可以cast http://download.oracle.com/javase/tutorial/2d/overview/rendering.html它到一个Graphics2D
.
Graphics2D g2d = (Graphics2D) g;
附录:这假设您正在覆盖paintComponent() http://download.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent%28java.awt.Graphics%29 in a JComponent http://download.oracle.com/javase/6/docs/api/javax/swing/JComponent.html,然后将其添加到JFrame
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)