多个移动球:添加球工作但移除不工作

时间:2014-06-03 20:55:49

标签: java swing

我自己学习Java并且遇到图形问题。我从StackOverflow中学到了很多东西!如果不是早期的线程,我会早点发布很多!

这是基本情景。我添加了JFrame Balls JPanel(这是另一个扩展ArrayList的类)。球随机移动,一个球在矩形上移动。面板上有三个按钮:暂停,添加随机球和删除随机球。我有去除球的问题。它提供了对import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.JPanel; public class Balls extends JPanel { public static ArrayList<Balls> BallList = new ArrayList<Balls>(); public Balls(String n, Color col, String nm) { mode = n; ballcolor = col; name = nm; if (name != "dummy") { BallList.add(this); } } @Override protected void paintComponent(Graphics g) { // TODO Auto-generated method stub super.paintComponent(g); for (Balls ball : BallList) { g.setColor(ball.ballcolor); g.fillOval(ball.x, ball.y, 20, 20); } } private int x, y; private String name; private static Boolean run = true; public String mode; public Color ballcolor; private String DirectionR = "R"; private void getposition() { if (mode.equals("Circle")) { } if (mode.equals("Rectangle")) { if (x == 0) { x = 150; } if (y == 0) { y = 200; } if (x == 150 && y == 200) { DirectionR = "R"; } if (x == 350 && y == 200) { DirectionR = "D"; } if (x == 350 && y == 300) { DirectionR = "L"; } if (x == 150 && y == 300) { DirectionR = "U"; } if (DirectionR.equals("R")) { x++; } if (DirectionR.equals("L")) { x--; } if (DirectionR.equals("U")) { y--; } if (DirectionR.equals("D")) { y++; } } if (mode.equals("Random")) { x = (int) (Math.random() * 400); y = (int) (Math.random() * 400); } } public void run() { while (run == true) { for (Balls ball : BallList) { ball.getposition(); } try { Thread.sleep(20); } catch (InterruptedException e2) { // TODO: handle exception e2.printStackTrace(); } repaint(); } } static class pauseframe implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (run.equals(true)) { run = false; } else { run = true; } } } static class addball implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Balls Randn = new Balls("Random", Color.GREEN, "NEW ADDED"); for (Balls ball : BallList) { System.out.println(ball.name); } } } static class Delball implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub for (Balls ball : BallList) { System.out.println(ball.mode); if (ball.mode.equals("Random")) { BallList.remove(ball); } } } } } 的并发访问的例外,我不知道如何解决这个问题。

而且我想将暂停按钮变为暂停/播放但不确定如何重新启动run方法作为非静态方法。

由于我是Java的新手,我可能会犯一些基本的错误,或者我的方法可能不正确。任何建议将不胜感激。谢谢!

以下是代码:

球类

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class Balls_test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        Balls dummy = new Balls("Dummy", Color.BLACK, "dummy");
        frame.setSize(500, 500);
        frame.getContentPane().add(dummy);
        frame.setBackground(Color.WHITE);
        dummy.setBackground(Color.WHITE);

        Balls Rect = new Balls("Rectangle", Color.RED, "Rect");

        Balls Rand = new Balls("Random", Color.BLUE, "Rand");

        Button pause = new Button("Pause");
        ActionListener pauseall = new Balls.pauseframe();
        pause.addActionListener(pauseall);
        frame.getContentPane().add(BorderLayout.NORTH, pause);

        Button addRandom = new Button("Add Random Ball");
        ActionListener NewBall = new Balls.addball();
        addRandom.addActionListener(NewBall);
        frame.getContentPane().add(BorderLayout.SOUTH, addRandom);

        Button DelRandom = new Button("Remove Random Ball");
        ActionListener DelBall = new Balls.Delball();
        DelRandom.addActionListener(DelBall);
        frame.getContentPane().add(BorderLayout.WEST, DelRandom);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        dummy.run();

    }

}

测试人员类

{{1}}

2 个答案:

答案 0 :(得分:1)

在迭代代码中使用的List时,无法删除该项:

public static ArrayList<Balls> BallList = new ArrayList<Balls>();

for (Balls ball : BallList) {
    System.out.println(ball.mode);
    if (ball.mode.equals("Random")) {
         BallList.remove(ball); 
    }
}

在迭代List时,您应该使用Iterator删除项目。

示例代码:

Iterator<Balls> it = BallList.iterator();

while(it.hasNext()) {
    Balls ball = it.next();
    System.out.println(ball.mode);
    if (ball.mode.equals("Random")) {
        it.remove();
    }
}

答案 1 :(得分:1)

  1. 您的添加动作永远不会将球添加到球列表中,因此没有&#34;随机&#34;列表中的球
  2. if (name != "dummy") {不是String比较在Java中的工作方式
  3. 在迭代过程中,您无法从List中移除元素。您可以使用synchronized块来确保一次只有一个线程正在访问List,但使用Iterator迭代List可能会更容易而是