弹跳球项目java
我正在创建一个bouncing ball java项目,该项目在用户每次点击add按钮时生成球,我还使用了3个其他按钮:弹跳球项目java,java,swing,Java,Swing,我正在创建一个bouncing ball java项目,该项目在用户每次点击add按钮时生成球,我还使用了3个其他按钮: 添加按钮-添加球 移除按钮-移除球 停止按钮-停止动画 开始按钮-开始动画 我还在JFrame底部(BorderLayout.SOUTH)的JPanel中添加了一个Jlabel,它显示了存在的球的数量,我需要面板底部的这个文本从左到右连续移动,如何做到这一点 这是代码 import java.awt.*; import java.awt.event.ActionEvent;
JFrame
底部(BorderLayout.SOUTH
)的JPanel
中添加了一个Jlabel
,它显示了存在的球的数量,我需要面板底部的这个文本从左到右连续移动,如何做到这一点
这是代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class bouncingBalls {
public static void main(String[] args) {
new bouncingBalls();
}
public bouncingBalls() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Program program = new Program();
program.run();
}
});
}
class Program {
protected JFrame mainFrame;
protected DrawPanel drawPanel;
protected JPanel panel1,panel2;
protected JLabel label;
protected String str = "Number of Balls ";
int number = 0;
void run() {
mainFrame = new JFrame();
drawPanel = new DrawPanel();
panel1 = new JPanel();
panel2 = new JPanel();
label = new JLabel();
panel1.add(drawPanel.addButton);
panel1.add(drawPanel.stopButton);
panel1.add(drawPanel.startButton);
panel1.add(drawPanel.removeButton);
panel1.setBackground(Color.BLACK);
label.setText(str + number);
label.setForeground(Color.WHITE);
panel2.add(label);
panel2.setBackground(Color.BLACK);
mainFrame.add(panel1, BorderLayout.NORTH);
mainFrame.add(panel2,BorderLayout.SOUTH);
mainFrame.getContentPane().add(drawPanel);
mainFrame.setTitle("Bouncing Balls");
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class DrawPanel extends JPanel {
private java.util.List<Ball> balls;
private Timer timer;
protected JButton addButton,startButton,stopButton,removeButton;
public DrawPanel() {
balls = new ArrayList<>(25);
addButton = new JButton("Add Ball");
stopButton = new JButton("Stop");
startButton = new JButton("Start");
removeButton = new JButton("Remove Ball");
addButton.addActionListener(new ButtonHandler());
stopButton.addActionListener(new ButtonHandler());
startButton.addActionListener(new ButtonHandler());
removeButton.addActionListener(new ButtonHandler());
}
public class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == drawPanel.addButton) {
Ball ball = new Ball(
/* Random positions from 0 to windowWidth or windowHeight */
(int) Math.floor(Math.random() * 640),
(int) Math.floor(Math.random() * 480),
/* Random size from 10 to 30 */
(int) Math.floor(Math.random() * 20) + 10,
/* Random RGB colors*/
Color.RED,
/* Random velocities from -5 to 5 */
(int) Math.floor((Math.random() * 10) - 5),
(int) Math.floor((Math.random() * 10) - 5)
);
balls.add(ball);
number++;
label.setText(str + number);
}
else if(e.getSource() == stopButton){
timer.stop();
}
else if(e.getSource() == startButton){
timer.start();
}
else if(e.getSource() == removeButton){
removeBall();
number--;
label.setText(str + number);
}
}
}
@Override
public void addNotify() {
super.addNotify();
if (timer != null) {
timer.stop();
}
timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
for (Ball b : balls) {
b.update(getSize());
}
repaint();
}
});
timer.start();
}
public void removeBall() {
if(balls.size()>=0)
balls.remove(0);
else
System.out.println("No more balls to remove");
}
@Override
public void removeNotify() {
super.removeNotify();
if (timer == null) {
return;
}
timer.stop();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
@Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
for (Ball b : balls) {
b.draw(graphics);
}
}
}
class Ball {
private int posX, posY, size;
private Color color;
private int vx;
private int vy;
public Ball(int posX, int posY, int size, Color color, int vx, int vy) {
this.posX = posX;
this.posY = posY;
this.size = size;
this.color = color;
this.vx = vx;
this.vy = vy;
}
void update(Dimension bounds) {
if (posX + size > bounds.width || posX < 0) {
vx *= -1;
}
if (posY + size > bounds.height || posY < 0) {
vy *= -1;
}
if (posX + size > bounds.width) {
posX = bounds.width - size;
}
if (posX < 0) {
posX = 0;
}
if (posY + size > bounds.height) {
posY = bounds.height - size;
}
if (posY < 0) {
posY = 0;
}
this.posX += vx;
this.posY += vy;
}
void draw(Graphics g) {
g.setColor(color);
g.fillOval(posX, posY, size, size);
}
}
}
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.*;
公开课弹跳球{
公共静态void main(字符串[]args){
新弹跳球();
}
公众弹跳球{
invokeLater(新的Runnable(){
@凌驾
公开募捐{
程序=新程序();
program.run();
}
});
}
班级计划{
受保护的JFrame主机;
受保护的抽板抽板;
受保护JPanel面板1、面板2;
受保护的标签;
受保护字符串str=“球数”;
整数=0;
无效运行(){
大型机=新的JFrame();
drawPanel=新的drawPanel();
panel1=新的JPanel();
panel2=新的JPanel();
label=新的JLabel();
panel1.add(drawPanel.addButton);
panel1.add(drawPanel.stopButton);
面板1.添加(drawPanel.startButton);
panel1.add(drawPanel.removeButton);
镶板1.立根背景(颜色:黑色);
label.setText(str+number);
标签。设置前景(颜色。白色);
面板2.添加(标签);
镶板2.立根背景(颜色:黑色);
mainFrame.add(panel1,BorderLayout.NORTH);
mainFrame.add(panel2,BorderLayout.SOUTH);
mainFrame.getContentPane().add(drawPanel);
大型机.setTitle(“弹跳球”);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
类DrawPanel扩展了JPanel{
私有java.util.List;
私人定时器;
受保护的JButton addButton、startButton、stopButton、removeButton;
公共事务委员会(){
balls=新阵列列表(25);
addButton=新的JButton(“添加球”);
stopButton=新按钮(“停止”);
startButton=新的JButton(“开始”);
removeButton=新按钮(“移除球”);
addButton.addActionListener(新的ButtonHandler());
addActionListener(newbuttonHandler());
addActionListener(新的ButtonHandler());
removeButton.addActionListener(新的ButtonHandler());
}
公共类ButtonHandler实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
如果(e.getSource()==drawPanel.addButton){
球=新球(
/*从0到windowWidth或windowHeight的随机位置*/
(int)Math.floor(Math.random()*640),
(int)Math.floor(Math.random()*480),
/*随机大小从10到30*/
(int)Math.floor(Math.random()*20)+10,
/*随机RGB颜色*/
颜色,红色,
/*从-5到5的随机速度*/
(int)数学层((Math.random()*10)-5),
(int)Math.floor((Math.random()*10)-5)
);
添加(ball);
数字++;
label.setText(str+number);
}
else if(例如getSource()==stopButton){
timer.stop();
}
else if(例如getSource()==startButton){
timer.start();
}
else if(例如getSource()==removeButton){
移除球();
数字--;
label.setText(str+number);
}
}
}
@凌驾
public void addNotify(){
super.addNotify();
如果(计时器!=null){
timer.stop();
}
计时器=新计时器(10,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
用于(球b:球){
b、 更新(getSize());
}
重新油漆();
}
});
timer.start();
}
公共无效删除球(){
如果(balls.size()>=0)
球。移除(0);
其他的
System.out.println(“不再需要移除球”);
}
@凌驾
公共无效删除通知(){
super.removeNotify();
如果(计时器==null){
返回;
}
timer.stop();
}
@凌驾
公共维度getPreferredSize(){
返回新维度(640480);
}
@凌驾
公共虚空绘制组件(图形){
super.paintComponent(图形);
用于(球b:球){
b、 绘制(图形);
}
}
}
班级舞会{
私有int posX,posY,size;
私人色彩;
私有intvx;
私营企业;
公共球(int-posX,int-posY,int-size,Color-Color,int-vx,int-vy){
this.posX=posX;
this.posY=posY;
这个。大小=大小;
这个颜色=颜色;