码迷,mamicode.com
首页 > 其他好文 > 详细

物体在框体内来回弹的UI

时间:2016-05-24 00:19:36      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

 

首先这是一个让一个物体在框体内来回弹的UI,

碰到边界反弹是符合物理学动量守恒的,就是这样,理想的。

准备接下来做一下碰撞机制,试着加入一些好玩的算法。

反正写的很烂,都不准笑。

一、方法类

 

package test;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame{
    
    Image img=GameUtil.getImage("images/sun2.jpg");
    
    public static final int GAME_WIDTH=500;
    public static final int GAME_HEIGHT=500;
    public void launchFrame(){
        setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
        setLocation(100,100);
        setVisible(true);
        
        new PaintThread().start();
        addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            
            }
        });    
    }
    
    private double x=100,y=100;
    private double degree=3.14/3;
    private boolean left=false;
    private boolean up=false;
    private double speed=10;
    
    @Override
    public void paint(Graphics g) {
        g.drawImage(img, (int)x,(int)y,null);
        if(speed>0){
            speed-=0.05;
        }else{
            speed=0;
        }
        x+=speed*Math.cos(degree);
        y+=speed*Math.sin(degree);

        if((x>500-50)||(x<20)){
            degree=Math.PI-degree;
            }
        if((y>300-50)||(y<20)){
            degree=-degree;;
            }        

    }
    
    class PaintThread extends Thread{
        
        public void run(){
            while(true){
                repaint();
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        }
    }



    public static void main(String[] args){
        MyFrame gf=new MyFrame();
        gf.launchFrame();
    }
}

 

 

二、窗口类

 

package test;

import java.awt.Graphics;
import java.awt.Image;

public class GameFrame extends MyFrame{
    Image img=GameUtil.getImage("images/sun2.jpg");
    
    private double x=100,y=100;
    private double degree=Math.PI/3;
    private boolean left=false;
    private boolean up=false;
    private double speed=10;
    
    @Override
    public void paint(Graphics g) {
        g.drawImage(img, (int)x,(int)y,null);
        if(speed>0){
            speed-=0.1;
        }else{
            speed=0;
        }
        x+=speed*Math.cos(degree);
        y+=speed*Math.sin(degree);

        if((x>500-50)||(x<20)){
            degree=Math.PI-degree;
            }
        if((y>300-50)||(y<20)){
            degree=-degree;;
            }        

    }
    
class PaintThread extends Thread{
        
        public void run(){
            while(true){
                repaint();
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        }
    }



    public static void main(String[] args){
        GameFrame gf=new GameFrame();
        gf.launchFrame();
    }
}

就是这么蠢一个界面

他的球是可以动的,动的,别笑。

技术分享

物体在框体内来回弹的UI

标签:

原文地址:http://www.cnblogs.com/MnsterLu/p/5521999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!