大家好,我有一个按钮,可以向我设置的 ArrayList 添加一个新球。而不是添加一个新球,它只是加速我已经开始的球。此 CreateCircle 创建球:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class CreateCircle extends JPanel {
/* Ports JFrame width, height from BouncingDrawFrame */
static double c, d;
/* Ports desired size of Circle */
static int r = 20; // Initial Value 20
/* Ports timer delay from BouncingDrawFrame */
static int z = 10; // Initial Value 10
/* Timer to control speed */
static Timer t = new Timer(z, null);
/* X,Y points to start, speed of movement */
static double x, y, velX = 1, velY = 1;
/* Ports color choice from BouncingDrawFrame */
static Color myColor;
public CreateCircle(int a, int b) {
/* Height of Frame */
c = a;
/* Width of Frame */
d = b;
t.start();
t.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/* "Bounces" the Ball off the sides */
if (x < 0 || x > (d - (r + 2))) {
velX = -velX;
}
/* "Bounces" the Ball off the top and bottom */
if (y < 0 || y > (c - (r + 30))) {
velY = -velY;
}
/* Moves ball 2 pixels each timer action */
x += velX;
y += velY;
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, r, r);
g2.setColor(myColor);
g2.fill(circle);
}
}
虽然这是处理按钮的类,如果单击则会创建一个新球:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class BouncingDrawFrame extends JFrame {
public BouncingDrawFrame() {
/*
* Create ArrayList to hold balls, remember ArrayList is not a component
* but the elements of ArrayList are
*/
final ArrayList<CreateCircle> ballList = (ArrayList<CreateCircle>) new ArrayList<CreateCircle>();
/* Create Main Ball Frame */
final JFrame main = new JFrame();
main.setTitle("Bouncing Balls!!");
main.setSize(350, 500);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setLocationRelativeTo(null);
main.setVisible(true);
/* Create Control Panel */
JFrame control = new JFrame();
control.setSize(300, 300);
control.setTitle("Change Settings!");
control.setLocationRelativeTo(null);
control.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
control.setVisible(true);
final JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(10, 2));
control.add(p1);
final JButton addBall = new JButton("Add A Ball");
p1.add(addBall);
/* Y Point */
final int a = main.getHeight();
/* X Point */
final int b = main.getWidth();
ballList.add(new CreateCircle(a, b));
main.add(ballList.get(0));
addBall.addActionListener(new ActionListener() {
private int click;
public void actionPerformed(ActionEvent arg0) {
click++;
ballList.add((click), new CreateCircle(a, b));
System.out.println(click);
System.out.println(ballList.size());
main.add(ballList.get(click));
repaint();
}
});
}
}
而不是创建一个新球,单击按钮只会加快第一个球的移动。我正在尝试将一个新球添加到 ArrayList 的索引中,该索引等于单击的数量。我有 ArrayList 的大小和点击的数字输出到系统,所以我知道 ArrayList 的大小随着点击次数的增加而增加。我只是不知道为什么它不添加新的 CreateCircle。
PS:这里是主线。
public class BouncingRun {
public static void main(String[] args) {
new BouncingDrawFrame();
}
}