为什么只抽一个球?应该还有更多

时间:2019-03-30 21:13:21

标签: java swing

我正在尝试创建一个Ball对象的ArrayList,我想将它们绘制到屏幕上,但是只绘制了其中一个,我不知道为什么。

球类:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;

public class Ball extends JPanel{
    int sX,sY;
    Color color;
    int speed;
    int height;
    int width;
    int velX=0;
    int velY=0;
    Random randInt;
    JFrame window;
    public Ball(int sX,int sY,int height,int width){
        this.sX=sX;
        this.sY=sY;
        this.color=color;
        this.speed=speed;
        this.height=height;
        this.width=width;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d=(Graphics2D)g;
        g2d.setColor(color.RED);
        Ellipse2D ellipse = new Ellipse2D.Double(sX,sY,width,height);
        g2d.fill(ellipse);

    }


    public String getCoords(){
        return "X: "+String.valueOf(sX)+" Y: "+String.valueOf(sY);
    }
}

BallManager类(用于存储球对象的数组列表)

import javax.swing.*;
import java.util.ArrayList;

public class BallManager {
    ArrayList<Ball> listOfBalls;
    int width,height;
    JFrame window;
    Ball newBall;
    public BallManager(JFrame window) {
        this.listOfBalls=new ArrayList<Ball>();
        this.window=window;
        this.addBalls(100);
        //this.drawBalls();
    }
    public void addBalls(int n){

        for (int y=0;y<n;y+=20){
            for(int x=0;x<n;x+=20){
                this.listOfBalls.add(new Ball(x,y,10,10));
                drawBalls();
            }
        }
        System.out.println(listOfBalls.size());

    }
    public void drawBalls(){

        for(Ball b:listOfBalls){
            window.add(b);
            System.out.println(b.getCoords());
        }
    }
}

主类:

public class Main {
    public static void main(String[] args){

        JFrameWindow j= new JFrameWindow(300,500);
        BallManager bm=new BallManager(j);
    }
}

窗口类:

import javax.swing.*;
import java.awt.*;

public class JFrameWindow extends JFrame {
    int width;
    int height;
    public JFrameWindow(int width,int height){
        super("JFrame ballssssss");
        this.width=width;
        this.height=height;
        this.setLocationRelativeTo(null);
        this.setSize(this.width,this.height);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
        this.getContentPane().setBackground(Color.orange);


    }


}

我不知道问题是什么。在我看来,arraylist中的球彼此一致地移动,但我不知道为什么。

1 个答案:

答案 0 :(得分:3)

您有些倒退:

  • Ball类不应从JPanel或任何其他Swing组件扩展。相反,它应该是一个逻辑类,该类通过一种方法public void draw(Graphics g)知道球的位置,球的颜色以及如何绘制球。
  • 应该只有一个JPanel将逻辑球保存在ArrayList<Ball>中,并通过for循环将其全部绘制在其paintComponent方法内。
  • 该单个JPanel应该添加到JFrame BorderLayout.CENTER。

例如

public class Ball {
    private static final int RADIUS = 5;
    private int x;
    private int y;
    private Color color;

    // constructors

    // getters / setters

    // methods to move the ball

    // or might use Graphics2D and rendering hints to smooth drawing
    public void draw(Graphics g) {
        g.setColor(color);
        g.fillOval(x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
    }
}   

class BallPanel extends JPanel {
    private List<Ball> balls = new ArrayList<>();

    // constructor -- fill the balls list

    // other methods....

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Ball ball : balls) {
            ball.draw(g);
        }
    }
}