为什么只抽一个球?应该还有很多 [英] Why is only one ball being drawn? There should be many more

查看:27
本文介绍了为什么只抽一个球?应该还有很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个 Ball 对象的 ArrayList,我想将它们绘制到屏幕上,但只绘制了其中一个,我不知道为什么.

I'm trying to have an ArrayList of Ball objects, and I want to draw them to the screen, but only one of them gets drawn and I don't know why.

球类:

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 类(它存储球对象的数组列表)

BallManager class (where it stores the arraylist of ball objects)

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 中的球彼此一致移动,但我不知道为什么.

I have no clue what the problem is. It seems to me that the balls in the arraylist move in unison with each other but I don't know why.

推荐答案

有些事情有点倒退:

  • Ball 类不应从 JPanel 或任何其他 Swing 组件扩展.相反,它应该是一个逻辑类,一个知道球的位置、颜色以及如何绘制它的类,在一个方法中,比如public void draw(Graphics g).
  • 应该只有一个 JPanel 将逻辑球保存在 ArrayList 中,并通过 for 循环在其 paintComponent 方法中绘制所有这些球.
  • 应该将这个单一的 JPanel 添加到 JFrame、BorderLayout.CENTER.
  • The Ball class should not extend from JPanel nor any other Swing component. Instead it should be a logical class, one that knows the location, color of a ball and how to draw it, in a method, say public void draw(Graphics g).
  • There should be only one JPanel that holds the logical Balls in an ArrayList<Ball>, and draws them all within its paintComponent method via a for-loop.
  • This single JPanel should be added to the 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);
        }
    }
}

这篇关于为什么只抽一个球?应该还有很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆