- public class BallGame2 extends JFrame {
- Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
- Image table = Toolkit.getDefaultToolkit().getImage("images/table.jpg");
- double x=100;// 小球的横坐标
- double y=100;// 小球的纵坐标
- double degree = 3.14/3;// 弧度. 此处就是 60 度
- // 画窗口的方法
- public void paint(Graphics g){
- System.out.println("窗口被画一次");
- g.drawImage(table, 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>438-40-25||y<30){
- //438 是窗口高度, 40 是桌子边框
- degree = -degree;
- }
- // 鹏杜鳌
- if(x<40||x>856-40-30){
- degree = 3.14 -degree;
- }
- }
- // 窗口加载
- void launchFrame(){
- setSize(847,440);// 宽高
- setLocation(50,50);// 坐标
- setVisible(true);
- // 重画窗口每秒画 25 次
- while(true){
- repaint();
- try{
- Thread.sleep(40);//40ms 1 秒等于 1000 毫秒 大约 1 秒画 20 次窗口
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- //main 方法是程序执行的入口
- public static void main(String[] args){
- System.out.println("我是刘总司令在进行百战程序员娱乐小程序");
- BallGame2 game = new BallGame2();
- game.launchFrame();
- }
- }
来源: http://www.bubuko.com/infodetail-2929574.html