Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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 从阵列中绘制对象_Java_Swing_Object_Arraylist_Subclass - Fatal编程技术网

Java 从阵列中绘制对象

Java 从阵列中绘制对象,java,swing,object,arraylist,subclass,Java,Swing,Object,Arraylist,Subclass,很抱歉又问了一个问题,伙计们,但我又一次被难住了,ArrayList就是不喜欢我;)。 现在我被画出物体的问题困扰着,它们不会被画出来,除了在一个随机的地方画一个。我想也许我只是创建了一个对象,然后把它放在新的阵列点上,但我不能完全确定。block是一个超类 public class MuthaLoad extends JPanel { public static void main(String[] args) { JFrame window

很抱歉又问了一个问题,伙计们,但我又一次被难住了,ArrayList就是不喜欢我;)。 现在我被画出物体的问题困扰着,它们不会被画出来,除了在一个随机的地方画一个。我想也许我只是创建了一个对象,然后把它放在新的阵列点上,但我不能完全确定。block是一个超类

    public class MuthaLoad extends JPanel {

        public static void main(String[] args) {
            JFrame window = new JFrame("MuthaLoad");
            window.setContentPane(new MuthaLoad());
            window.setSize(600,600);
            window.setLocation(100,100);
            window.setVisible(true);

    }
        private int health=100;
        private int money=100;

        private ArrayList<Block> blockWorld=new ArrayList<Block>();
        private int colum=0;
        private int row=0;

        public MuthaLoad(){
            for(int i=0;i<144;i++){
                if(i%12==0){
                    colum=0;
                    row+=50;
                }
                blockWorld.add(new Block(0,0));
                int random=(int)(Math.random()*100);
                System.out.println(colum + "" + row);
                blockWorld.add(new Block.Dirt(colum,row));
                if(i<72){

                }
                else{
                    blockWorld.add(new Block.Dirt(i,i));
                }
//              else if(random<5){
//                  
//              }
//              else if(random<50){
////                    blockWorld.add(block.new dirt(50,0));
//              }
                colum+=50;
            }
        }
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                for(int i=0;i<144;i++){
                    blockWorld.get(i).draw(g);
                }


        }

}
公共类MuthaLoad扩展了JPanel{
公共静态void main(字符串[]args){
JFrame窗口=新JFrame(“Muthalad”);
setContentPane(新的MuthaLoad());
窗口设置大小(600600);
窗口设置位置(100100);
window.setVisible(true);
}
私人健康指数=100;
私人整数货币=100;
private ArrayList blockWorld=new ArrayList();
私有整数列=0;
私有int行=0;
公共MuthaLoad(){

对于(inti=0;i用户和我离线讨论了这个问题。我们发现了问题:在“Block”类中,变量“x”和“y”被声明为静态的。
因此,对任何块的“x”和“y”执行的最后一次赋值变量设置了所有块对象的位置。

您是否将块的位置设置为不同的位置?是的,它们每个都有不同的x和y坐标,但它似乎只是创建了一个,我认为它只是反复替换了最后一个,但我不知道如何修复它。是什么让您认为它只是替换了t他一次又一次的上一个?