這是我的第一個圖形化Java程序,我想要做的是重新創建一個簡單的經典程序,其中我彈出一個JFrame
窗口中彈出多個球。Java圖形「彈跳球」程序
到目前爲止,我已經成功地使用run()
方法中的代碼來反彈。這對我創建的一個球對象起作用,但現在我想要有很多球,所以我正在嘗試在我的Ball
類中創建一個方法,這將使我創建的每個球對象獨立地在我的「球世界」中反彈。
現在我所關心的是他們從牆上反彈,而不是彼此(我將在後面解釋)。
問題:在我ballMove(int, int, int, int)
方法我有四個int
參數,其中,前兩個參數是window
的width
和height
,最後兩個參數是Xspeed
和Yspeed
。當我通過我的if statements
時,它會溫和地將x
和y speed
參數設置爲負數,當球擊中右側或底部牆時,但是當run()
方法再次執行ballMove(int, int, int, int)
方法時,它們會回到正值,並且球從窗口。我曾嘗試在我的球類中使用一堆getter
和setter
方法。我在我的ballMove(int, int, int, int)
方法中嘗試了臨時變量。我沒有嘗試過的作品。
問題:通過使用我的Ball
類的方法,如何防止我的參數Xspeed
和Yspeed
從重新初始化我的實例變量的速度爲正,當球(S)與牆壁碰撞?
因爲我是圖形編程新手,任何有用的建議將不勝感激。
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);
}
}
}
你爲什麼要在'move()'方法中使用參數。它不應該採取任何參數,只是使用Ball狀態 –
1)使用Swing'Timer'而不是'Thread.sleep()'。 2)你在'this.setSpeedX(xSpeed); this.setSpeedY(ySpeed);'行中的'ballMove()'方法中的問題。 3)在'JFrame'上使用'JPanel'和方法'paintComponent()' – alex2410
@ alex2410'JFrame'和'JPanel'之間的區別是什麼,爲什麼'JPanel'是一個更好的選擇這個?怎麼樣使用Applet類?我可以用它來代替嗎? – zoltack429