“Java图形”;“弹跳球”;程序
这是我的第一个图形化Java程序,我正在尝试重新创建一个简单的经典程序,其中有多个球在“Java图形”;“弹跳球”;程序,java,swing,graphics,Java,Swing,Graphics,这是我的第一个图形化Java程序,我正在尝试重新创建一个简单的经典程序,其中有多个球在JFrame窗口中反弹 到目前为止,我已经成功地使用run()方法中的代码让一个球弹起。这适用于我创建的一个ball对象,但现在我想要有多个ball,所以我尝试在我的ball类中创建一个方法,使我创建的每个ball对象在我的“ball world”中独立反弹 现在我所关心的是它们从墙上弹下来,而不是彼此(我稍后会弄清楚) 问题:在我的ballMove(int,int,int,int,int)方法中,我有四个in
JFrame
窗口中反弹
到目前为止,我已经成功地使用run()
方法中的代码让一个球弹起。这适用于我创建的一个ball对象,但现在我想要有多个ball,所以我尝试在我的ball
类中创建一个方法,使我创建的每个ball对象在我的“ball world”中独立反弹
现在我所关心的是它们从墙上弹下来,而不是彼此(我稍后会弄清楚)
问题:在我的ballMove(int,int,int,int,int)
方法中,我有四个int
参数,其中前两个参数是窗口的宽度和高度,最后两个参数是Xspeed
和Yspeed
。当我通过我的if语句时
它会在球碰到右壁或底壁时将x
和y速度
参数适度设置为负值,但当run()
方法再次执行ballMove(int,int,int,int)
方法时,他们回到积极的状态,球从窗口消失了。在我的ball类中,我尝试过使用一系列的getter
和setter
方法。我在我的ballMove(int,int,int,int)
方法中尝试了临时变量。我试过的都不管用
问题:通过使用我的Ball
类方法,如何防止我的参数Xspeed
和Yspeed
在球与墙碰撞时将实例速度变量重新初始化为正值
因为我是图形编程新手,任何有用的建议都将不胜感激
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();
}
}
}
这是我的球
课
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);
}
}
}
导入java.awt.Color;
导入javax.swing.JFrame;
公共类球框架{
/**
*
*/
私有静态最终长serialVersionUID=1L;
私人整数宽度、高度、球×球Y;
私有int-Xspeed;
私人互联网速度;
私人色彩;
公共球(颜色、整数宽度、整数高度、整数球X、整数球Y){
这个。宽度=宽度;
高度=高度;
这个颜色=颜色;
this.ball_X=ball_X;
this.ball\u Y=ball\u Y;
}
公共颜色getColor(){
返回此.color;
}
公共int getWidth(){
返回这个.width;
}
公共整数getHeight(){
返回此参数。高度;
}
public int getBallX(){
把这个还给我;
}
公共int getBallY(){
把这个还给我;
}
公共无效设置速度x(整数x){
这个.Xspeed=x;
}
公共无效设置速度(整数x){
这个.Yspeed=x;
}
public int getSpeedX(){
返回这个.Xspeed;
}
公共int getSpeedY(){
把这个还给我;
}
公共空间集合x(整数x){
这个.ball_X=X;
}
公共空间(int y){
这个球Y=Y;
}
公共空球移动(整数X,整数Y,整数X速度,整数Y速度){
//使用函数的参数初始化Xspeed和Yspeed
这个.setSpeedX(xSpeed);
这个.setSpeedY(ySpeed);
//每次执行线程时,通过将设定速度添加到球的位置来移动球
this.setBallX(this.getBallX()+this.getSpeedX());
this.setBallY(this.getBallY()+this.getSpeedY());
//当球撞到墙上时,它们会反弹,直到撞到另一面墙为止。
if(this.getBallX()+50>=X){
此.setSpeedX(-xSpeed);
}
if(this.getBallY()+50>=Y){
此.setSpeedY(-ySpeed);
}
如果(this.getBallX()+25您的问题就在那里:
ball.ballMove(宽度、高度、20、5);
因为这是一个循环,每次调用它时,你都让它朝同一个方向移动。如果球撞到墙,你在球移动结束时反转速度,但这并不重要,因为下次调用它时,球仍然朝+20,+5移动
我的建议是在创建球的实例时添加速度参数,并让ballmove更新自己的速度
ball.ballMove(宽度、高度);
其他建议:在实际移动球之前进行碰撞检查。这样你可以确保在实际移动球之前没有朝错误的方向移动
现在,另一个问题是,在main中,您正在调用runnable.run的run()
方法,该方法将在当前线程上执行。您需要执行以下操作:
threadt=新线程(主线程);
t、 开始();
独立于JFrame进行绘图和计算。为什么在move()
方法中使用参数。它不应该使用任何参数,只使用Ball状态1)使用SwingTimer
而不是Thread.sleep()
。2)您在this.setSpeedX(xSpeed)中的ballMove()
方法中遇到的问题;此.setSpeedY(ySpeed);
行。3)改为在JFrame
上绘制,使用JPanel
和方法paintComponent()@alex2410JFrame
和JPanel
之间有什么区别,为什么JPanel
会是这样一个程序的更好选择?使用Applet类怎么样?我可以改用它吗?谢谢!我尝试了你的第一个建议,效果非常好!