gpt4 book ai didi

Java图形 "Bouncing Ball"程序

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:13 26 4
gpt4 key购买 nike

这是我的第一个图形 Java 程序,我想做的是重新创建一个简单的经典程序,其中我有多个球在 JFrame 中弹跳。窗口。

到目前为止,我已经成功地使用 run() 中的代码让一个球弹来弹去。方法。这适用于我创建的一个球对象,但现在我想要有很多球,所以我尝试在我的 Ball 中创建一种方法类将使我创建的每个球对象在我的“球世界”中独立弹跳。

现在我关心的是它们从墙上反弹,而不是彼此(我稍后会弄清楚)。

问题:在我的 ballMove(int, int, int, int) 中方法我有四种int参数,其中前两个参数是 widthheight window的,最后两个参数是Xspeed ,和Yspeed 。当我浏览我的if statements时它将适度设置 xy speed当球击中右侧或底部墙壁时,参数为负值,但当 run()方法执行 ballMove(int, int, int, int)再次用这种方法,他们又恢复到积极状态,球从 window 上消失了。我尝试过使用一堆 gettersetter我的球类中的方法。我已经在我的 ballMove(int, int, int, int) 中尝试过临时变量方法。我尝试过的任何方法都不起作用。

问题:通过使用我的 Ball类方法,如何防止我的参数 XspeedYspeed当球与墙壁碰撞时,将我的实例速度变量重新初始化为正值?

因为我是图形编程的新手,所以任何有用的建议将不胜感激。

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;

public class Main extends JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int width = 800;
private int height= 600;
private int ballRadius = 50;

private Random rand = new Random();

//Create and initialize a ball object
public Ball ball = new Ball(Color.BLUE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball2 = new Ball(Color.RED, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball3 = new Ball(Color.GREEN, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball4 = new Ball(Color.ORANGE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball5 = new Ball(Color.YELLOW, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//constructor
public Main(){

setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}
//Paint the ball(s)
public void paint(Graphics g){
super.paint(g);
g.setColor(ball.getColor());
g.fillOval(ball.getBallX(), ball.getBallY(), ball.getWidth(), ball.getHeight());
//g.setColor(ball2.getColor());
//g.fillOval(ball2.getBallX(), ball2.getBallY(), ball2.getWidth(), ball2.getHeight());
//g.setColor(ball3.getColor());
//g.fillOval(ball3.getBallX(), ball3.getBallY(), ball3.getWidth(), ball3.getHeight());
//g.setColor(ball4.getColor());
//g.fillOval(ball4.getBallX(), ball4.getBallY(), ball4.getWidth(), ball4.getHeight());
//g.setColor(ball5.getColor());
//g.fillOval(ball5.getBallX(), ball5.getBallY(), ball5.getWidth(), ball5.getHeight());
}
//Run the program
public static void main(String [] args){
Main main = new Main();
main.setVisible(true);
main.run();

}

@Override
public void run() {
// TODO Auto-generated method stub


while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

ball.ballMove(width, height, 20, 5);
repaint();
//ball2.ballMove(width, height, 15, 3);
//repaint();
//ball3.ballMove(width, height, 3, 20);
//repaint();
//ball4.ballMove(width, height, 10, 10);
//repaint();
//ball5.ballMove(width, height, 10, 20);
//repaint();


}
}


}

这是我的Ball

import java.awt.Color;

import javax.swing.JFrame;


public class Ball extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private int width, height, ball_X, ball_Y;
private int Xspeed;
private int Yspeed;
private Color color;
public Ball(Color color, int width, int height, int ball_X, int ball_Y){
this.width = width;
this.height = height;
this.color = color;
this.ball_X = ball_X;
this.ball_Y = ball_Y;
}
public Color getColor(){
return this.color;
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public int getBallX(){
return this.ball_X;
}
public int getBallY(){
return this.ball_Y;
}
public void setSpeedX(int x){
this.Xspeed = x;
}
public void setSpeedY(int x){
this.Yspeed = x;
}
public int getSpeedX(){
return this.Xspeed;
}
public int getSpeedY(){
return this.Yspeed;
}
public void setBallX(int x){
this.ball_X = x;
}
public void setBallY(int y){
this.ball_Y = y;
}
public void ballMove(int X, int Y, int xSpeed, int ySpeed){

//initialize Xspeed and Yspeed with the parameters of the function
this.setSpeedX(xSpeed);
this.setSpeedY(ySpeed);
//Moves the balls by adding the set speed to the position of the balls each time thread is executed
this.setBallX(this.getBallX() + this.getSpeedX());
this.setBallY(this.getBallY() + this.getSpeedY());
//When the balls hit the walls they are suppose to bounce back until they hit another wall.
if(this.getBallX() + 50 >= X){
this.setSpeedX(-xSpeed);
}
if(this.getBallY() + 50 >= Y){
this.setSpeedY(-ySpeed);
}
if(this.getBallX() + 25 <= 0){
this.setBallX(xSpeed);
}
if(this.getBallY() + 25 <= 0){
this.setSpeedY(ySpeed);
}
}
}

最佳答案

你的问题就在那里:

ball.ballMove(宽度, 高度, 20, 5);

由于这是一个循环,因此每次调用它时,您都会使其朝同一方向移动。如果球撞到墙壁,您将在球移动结束时反转速度,但这并不重要,因为下次您调用它时,球仍然会向 +20、+5 移动。

我的建议是在创建球实例时添加速度参数,并让 ballmove 更新自己的速度。

ball.ballMove(宽度,高度);

其他建议:在实际移动球之前进行碰撞检查。这样你就可以在实际移动它之前确保你没有走错方向。

现在,另一个问题是在您的 main 中,您正在调用可运行对象的 run() 方法。 Run 在当前线程上执行。你需要做这样的事情:

线程 t = new Thread(main);
t.start();

独立于 JFrame 进行绘图和计算。

关于Java图形 "Bouncing Ball"程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23294604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com