无法在java中向列表绘制/添加图像

00jrzges  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(106)

所有链接到ArrayList/List i的代码都有:

public void draw(Graphics2D gImages) {
    List missiles = aiShooter.getMissiles();

    if(keyH.shooting) {
        for(int i = 0; i < missiles.size(); i++) {
            Missile mis = (Missile) missiles.get(i);
            System.out.println("Shot");
            aiShooter.fire();
            mis.move();
            gImages.fillRect(mis.X, mis.Y, mis.Width, mis.Height);
        }
    }
    gImages.fillRect(X2, Y2, Width, Height);
    gImages.fillRect(X, Y, Width, Height);
}
package testGames2;

import java.util.ArrayList;
import java.util.List;

public class AIShooter {

    public static int X = 200;
    public static int Y = 50;
    public static int Width = 10;
    public static int Height = 3;
    List<Missile> missiles new ArrayList<>();

    public List<Missile> getMissiles() {
        return missiles;
    }

    public void fire() {
        missiles.add(new Missile(Missile.X, Missile.Y, Missile.Width, Missile.Height));
    }
}
package testGames2;

public class Missile {

    public static int X = AIShooter.X;
    public static int Y = AIShooter.Y;
    public static int Width = 10;
    public static int Height = 3;

    public Missile(int X, int Y, int Width, int Height) {
        this.X = X;
        this.Y = Y;
        this.Width = Width;
        this.Height = Height;
    }

    public void move() {
        X += 10;
    }

}

我要么不能添加导弹到列表中,要么矩形(导弹)不能绘制。我尝试了几种方法,但都没有工作。如果需要更多的代码,请告诉我。任何帮助都是真正的赞赏!

j5fpnvbx

j5fpnvbx1#

你的开火命令应该把新创建的导弹添加到导弹数组列表中。你认为为什么它没有被添加?
此外,为了绘制导弹,需要在调用fillRect之前使用setColor(someColor)之类的设置Graphics2D上下文,也许该上下文是在调用draw()之前设置的?

相关问题