Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在图形上绘制气球2d_Java_Graphics2d - Fatal编程技术网

Java 在图形上绘制气球2d

Java 在图形上绘制气球2d,java,graphics2d,Java,Graphics2d,我想做一个泡泡射击游戏,但我在开始的时候有问题。在尝试编译程序时,出现错误:线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常。 public class MyPanel extends JPanel { Init init; public MyPanel(){ super(); init = new Init(); } public void paint(

我想做一个泡泡射击游戏,但我在开始的时候有问题。在尝试编译程序时,出现错误:
线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常。

public class MyPanel extends JPanel {
    Init init;

    public MyPanel(){
        super();
        init = new Init();      
    }

    public void paint(Graphics g){
        Dimension size = getSize();
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.GRAY);
        g2d.fillRect(0, 0, size.width, size.height - 70);
        for(int j = 0; j < 10; j++)
            for(int i = 0; i < 20; i++){
                init.fields[i][j].b.paint(g);       //here compiler shows error
            }
    }
}


public class Field {
    private int x;
    private int y;
    private int r = 30;
    public Baloon b;


    public Field(int x, int y){
        this.x = x*r;
        this.y = y*r;
    }

    public void addBaloon(int n){
        b = new Baloon(this.x, this.y, r, n);
        }
}

public class Init {

    Parser pr = new Parser();
    private int r = pr.getRadius();
    private int x = pr.getXDimension();
    private int y = pr.getYDimension();
    private int ni = pr.getColorRange();

    Field[][] fields = new Field[x][y];

    private int startX = 20;
    private int startY = 10;

    public Init(){
        for(int yi = 1; yi<y; yi++){
            for (int xi = 1; xi<x; xi++){
                fields[xi][yi] = new Field(xi*r, yi*r);         
            }
        }

        for(int yi = 1; yi < startY; yi ++){
            for(int xi = 1 ; xi < startX; xi++){
                Random rand = new Random();
                int n = rand.nextInt(ni);
                fields[xi][yi].addBaloon(n);    
            }
        }       
    }
}
公共类MyPanel扩展了JPanel{
初始化初始化;
公共事务委员会(){
超级();
init=新的init();
}
公共空间涂料(图g){
维度大小=getSize();
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.GRAY);
g2d.fillRect(0,0,size.width,size.height-70);
对于(int j=0;j<10;j++)
对于(int i=0;i<20;i++){
init.fields[i][j].b.paint(g);//这里编译器显示错误
}
}
}
公共类字段{
私人INTX;
私营企业;
私有整数r=30;
公共阳台b;
公共字段(整数x,整数y){
这个.x=x*r;
这个。y=y*r;
}
公共空位地址(int n){
b=新的阳台(这个.x,这个.y,r,n);
}
}
公共类初始化{
Parser pr=new Parser();
private int r=pr.getRadius();
private int x=pr.getXDimension();
private int y=pr.getYDimension();
private int ni=pr.getColorRange();
字段[][]字段=新字段[x][y];
专用int startX=20;
私人int startY=10;
公共Init(){

对于(int yi=1;yi您正在从索引1初始化数组:

for(int yi = 1; yi<y; yi++){
    for (int xi = 1; xi<x; xi++){
        fields[xi][yi] = new Field(xi*r, yi*r);         
    }
}

对于(int yi=1;yiPaste whole stacktrace,让我们知道它抛出的是哪一行NPE。数组索引从0开始,而不是从1I开始。我认为在超级构造函数中调用了paint(),这意味着Init为null。
for(int j = 0; j < 10; j++)
    for(int i = 0; i < 20; i++){
        init.fields[i][j].b.paint(g);       //here compiler shows error
    }
for(int yi = 0; yi<y; yi++){
    for (int xi = 0; xi<x; xi++){
        fields[xi][yi] = new Field(xi*r, yi*r);         
    }
}