这是我的第一个图形Java程序,我尝试做的是重新创建一个简单的经典程序,我在JFrame
窗口中弹出多个球。
到目前为止,我已成功地使用run()
方法内的代码获得一个球反弹。这适用于我创建的一个球对象,但现在我想要有很多球,所以我试图在我的Ball
类中创建一个方法,使我创建的每个球对象在我的& #34;球世界"独立地
现在我所关心的只是他们从墙上弹跳而不是彼此(我稍后会想到的)。
问题:在我的ballMove(int, int, int, int)
方法中,我有四个int
参数,其中前两个参数是width
和height
window
,最后两个参数是Xspeed
和Yspeed
。当我浏览if statements
时,当球击中右侧或底壁时,它将温和地将x
和y speed
参数设置为负值,但是当run()
方法执行时再次ballMove(int, int, int, int)
方法,他们回到积极的状态,球从窗口消失。我尝试在我的球类中使用一堆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();
}
}
}
这是我的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);
}
}
}
答案 0 :(得分:0)
你的问题就在那里:
ball.ballMove(width, height, 20, 5);
因为这是一个循环,所以每次调用它都会让它向同一个方向移动。如果碰到墙壁,你会在转弯结束时反转速度,但这并不重要,因为下次你打电话时,球仍会朝+20,+ 5移动。
我的建议是在创建球的实例时添加速度参数,并让球更换自己的速度。
ball.ballMove(width, height);
其他建议:在实际移动球之前进行碰撞检查。这样你就可以确保在实际移动它之前没有走错方向。
现在,另一个问题是,在你的主要部分,你正在调用runnable的run()
方法。 Run在当前线程上执行。你需要做这样的事情:
Thread t = new Thread(main);
t.start();
独立于JFrame进行绘图和计算。