台球碰撞

import java.awt.*;


public class BallGame extends Frame {

Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg");
double x=100;
double y=100;
double degree = 3.14/3;
public void paint(Graphics g){
g.drawImage(sun, (int)x,(int)y, null);
x = x+10*Math.cos(degree);
y = y+10*Math.sin(degree);

if(y>300-30||y<30){
degree = - degree;
}
if(x>500-30||x<0){
degree = 3.14-degree;
}
}

void launchFrame(){
setSize(500, 300);
setLocation(50, 50);
setTitle("台球");
setBackground(Color.black);
setVisible(true);
new PaintThread().start();
}

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


class PaintThread extends Thread {
public void run(){
while(true){
repaint();  
try{
Thread.sleep(40);   
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

你可能感兴趣的:(模拟)