Wil*_*ill 2 java graphics swing jframe paintcomponent
我正在制作一个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);
}
Run Code Online (Sandbox Code Playgroud)
第一行"Graphics h = new Graphics2D();" 产生错误,因为Graphics2d是抽象的,但我不知道从哪里开始.
我需要代码来获取我拥有的所有图像并将它们绘制到JFrame中的点.我提醒你,我以前从未这样做过,所以请告诉我这是否是错误的做法.
paintComponent()改为覆盖; 它将提供Graphics上下文.你可以把它投射到Graphics2D.
Graphics2D g2d = (Graphics2D) g;
Run Code Online (Sandbox Code Playgroud)
附录:这假设你压倒paintComponent()了a JComponent,然后加入到JFrame.
| 归档时间: |
|
| 查看次数: |
13057 次 |
| 最近记录: |