Java实现台球游戏的动画实现相关代码

 

 

package com.bjsxt;//scr中新建的包package
import java.awt.*;
import javax.swing.*;

public class BallGame extends JFrame {
Image ball=Toolkit.getDefaultToolkit().getImage("images/ball.png");
Image desk=Toolkit.getDefaultToolkit().getImage("images/desk.png");
double x=200;
double y=200;
double degree=3.14/3;
//绘制窗口
public void paint(Graphics g){
System.out.println("窗口被绘制了一次。");
g.drawImage(desk,0,0,null);
g.drawImage(ball,(int)x,(int)y,null);
x=x+10*Math.cos(degree);
y=y+10*Math.sin(degree);
//碰到上下边界
if(y>501-40-30||y<0+40+30){
degree=-degree;
}
//碰到左右边界
if(x>856-40-30||x<0+40+30) {
degree = 3.14 - degree;
}
}
//创建窗口
void launchFrame(){
setSize(856,501);
setLocation(100,100);
setVisible(true);
//实现动画,每秒绘制窗口25次
while(true){
repaint();
try {
Thread.sleep(40);//1s=1000ms,大约1s绘制25次
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
System.out.println("我的小游戏开始了!");
BallGame game=new BallGame();
game.launchFrame();
}

}
posted @ 2023-02-24 18:31  师大无语  阅读(53)  评论(0编辑  收藏  举报