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.jpg");
double x = 100;
double y = 100;
//boolean right = true;//right 向右
double degree = 3.14/3;//弧度,此处就是60度;degree度数
//图片加载
public void paint(Graphics g){
System.out.println("刷新");
g.drawImage(desk, 0, 0, null);
g.drawImage(ball,(int) x, (int)y, null);//球的位置//出现报错,强制类型转换
//直线运动
/*if(right){
x = x +10;
}else{
x = x - 10;
}
if(x>650){
right = false;
//x = x - 10;
}
if(x<40){
right = true;
}*/
//变换角度
x = x + 5*Math.cos(degree);
y = y + 5*Math.sin(degree);
//上下边界
if(y>421-35-30||y<35+20){//421窗口高度,30是桌子边框,30球的直径;30+20是桌子边框 加上上面标题栏的宽度
degree = -degree;
}
//左右边界
if(x
版权声明:本文为weixin_45339692原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。