Java 在我的JPanel中,如何解决这个问题?

Java 在我的JPanel中,如何解决这个问题?,java,swing,jpanel,intersect,Java,Swing,Jpanel,Intersect,我的问题是,我用一条线让星星移动,它们垂直移动,效果很好,但我对星星做了一个随机的X,有时它会像这样对其他星星进行交互: 这是我为JPanel编写的代码: 类背景移动 public class Backgroundmoving extends JPanel { ArrayList<starmoving> star; public Backgroundmoving() { this.setSize(650, 501); star =

我的问题是,我用一条线让星星移动,它们垂直移动,效果很好,但我对星星做了一个随机的
X
,有时它会像这样对其他星星进行交互:

这是我为
JPanel
编写的代码:

背景移动

public class Backgroundmoving extends JPanel {
    ArrayList<starmoving> star;

    public Backgroundmoving() {
        this.setSize(650, 501);
        star = new ArrayList<>();
        for (int i = 0; i < 20; i++)
            this.addStar();
    }

    public void addStar() {
        int x, y;

        x = (int) (Math.random() * 625);

        y = (int) (Math.random() * 476);

        starmoving e = new starmoving(x, y);
        star.add(e);
        Thread t = new Thread(e);
        t.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }

    public void draw(Graphics g) {
        g.drawImage(new ImageIcon("background.png").getImage(), 0, 0, 650, 501, null);
        for (int i = 0; i < star.size(); i++) {
            star.get(i).draw(g);
        }
        repaint();
    }

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame gui = new JFrame();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(650, 510);
        gui.setResizable(false);
        gui.add(new Backgroundmoving());

        gui.setVisible(true);
    }

}
public class starmoving implements Runnable {

    int x;
    int y;
    int yVel;

    public starmoving(int x, int y) {
        this.x = x;
        this.y = y;
        yVel = 1;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private void move() {
        y += yVel;
        if (y > 476) {
            y = 0;
            x = (int) (Math.random() * 625);
        }
    }

    private boolean isOffScreen() {

        if (y <= 476)
            return false;
        return true;
    }

    public void draw(Graphics g) {
        g.drawImage(new ImageIcon("star.png").getImage(), x, y, 12, 12, null);
    }

    @Override
    public void run() {
        while (true) {
            move();

            try {
                Thread.sleep(7);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
            }

        }
    }

}
公共类背景移动扩展了JPanel{
ArrayList星;
公众背景{
这个。设置大小(650501);
star=新阵列列表();
对于(int i=0;i<20;i++)
这是addStar();
}
公共无效addStar(){
int x,y;
x=(int)(Math.random()*625);
y=(int)(Math.random()*476);
恒星移动e=新恒星移动(x,y);
加上(e);
螺纹t=新螺纹(e);
t、 start();
}
公共组件(图形g){
超级组件(g);
抽签(g);
}
公共空间绘制(图g){
g、 drawImage(新的ImageIcon(“background.png”).getImage(),0,0650501,null);
对于(int i=0;i
移星

public class Backgroundmoving extends JPanel {
    ArrayList<starmoving> star;

    public Backgroundmoving() {
        this.setSize(650, 501);
        star = new ArrayList<>();
        for (int i = 0; i < 20; i++)
            this.addStar();
    }

    public void addStar() {
        int x, y;

        x = (int) (Math.random() * 625);

        y = (int) (Math.random() * 476);

        starmoving e = new starmoving(x, y);
        star.add(e);
        Thread t = new Thread(e);
        t.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }

    public void draw(Graphics g) {
        g.drawImage(new ImageIcon("background.png").getImage(), 0, 0, 650, 501, null);
        for (int i = 0; i < star.size(); i++) {
            star.get(i).draw(g);
        }
        repaint();
    }

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame gui = new JFrame();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(650, 510);
        gui.setResizable(false);
        gui.add(new Backgroundmoving());

        gui.setVisible(true);
    }

}
public class starmoving implements Runnable {

    int x;
    int y;
    int yVel;

    public starmoving(int x, int y) {
        this.x = x;
        this.y = y;
        yVel = 1;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private void move() {
        y += yVel;
        if (y > 476) {
            y = 0;
            x = (int) (Math.random() * 625);
        }
    }

    private boolean isOffScreen() {

        if (y <= 476)
            return false;
        return true;
    }

    public void draw(Graphics g) {
        g.drawImage(new ImageIcon("star.png").getImage(), x, y, 12, 12, null);
    }

    @Override
    public void run() {
        while (true) {
            move();

            try {
                Thread.sleep(7);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
            }

        }
    }

}
public类starmoving实现可运行{
int x;
int-y;
内特伊维尔;
公共移星(整数x,整数y){
这个.x=x;
这个。y=y;
yVel=1;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
公共int getY(){
返回y;
}
公共空间设置(整数y){
这个。y=y;
}
私人空位移动(){
y+=yVel;
如果(y>476){
y=0;
x=(int)(Math.random()*625);
}
}
私有布尔值IsofScreen(){
如果(y)
我不希望恒星相交,在随机X之前,我在考虑if,但我怎么知道其他恒星是否在那个X

您有一个ArrayList,其中包含所有“StarMoving”对象。因此,您需要遍历该列表以确保所有对象都不相交

除此之外,你还有其他问题

  • 使用正确的Java类名。Java类应该以大写字符开头。(例如,“starmoving”是错误的)

  • 不要在动画中使用多个线程。当前代码从20个线程开始。应该有一个线程,然后遍历ArrayList以移动所有星星

  • 不要在draw()方法中读取图像。目前,您的代码每7毫秒读取一次图像。这不是很有效。图像应该读取一次,然后作为类的属性存储


  • 请给我们看
    starmoving
    类。问题是我不确定如何不与其他恒星相交,添加了starmoving类为什么恒星在3D空间的2D投影上看起来不相交?另请参见。我不希望恒星相交,在随机X之前考虑的是if,但我如何知道其他恒星是否在该X中,也许我可以在我的面板中实现Runnable并在方法run中进行控制,但还有其他方法吗?我知道“starmoving”错了吗?这只是一个例子,对于第3点,好的,我会读一次,但我不明白你的意思是什么,我怎么能用一个线程来处理所有事情?\uo哦,我明白你的意思了,使用t=new…在构造函数上声明t
    我知道“starmoving”是错误的这只是一个例子
    -如果你想让我们阅读你的代码,那么就按照标准来做。为什么你要浪费时间使用一个错误的类名,然后再更改它。第一次就做对!!!在你将所有的星星添加到a之后,线程应该在
    BackgroundMoving
    类中实现和启动你的意思是从addStar中删除t=new Thread(e);t.start();并在构造函数中的for之后删除t=new Thread(star);和t.start();?它不应该工作,因为star是一个ArrayList,我必须做其他事情,比如做这个t=new Thread(star.get(i));t.start();