编写图形界面程序,显示一个红色反弹球的程序,当该球撞击Applet边框时,它应从边框弹回并以相反方向45°运动。
import javax.swing.*;
import java.awt.*;
class BallPanel extends JPanel implements Runnable {
ball_move BM = null;
public BallPanel() {
BM = new ball_move();
Thread t = new Thread(BM);
t.start();//这一行不加会导致画面停止不动
}
public void paint(Graphics g) {//画球
super.paint(g);
setBackground(Color.white);
setForeground(Color.white);
g.fillOval(0, 0, 20, 20);//fillOval(int x,int y,int width,int height)使用度当前颜色填充外接指定矩形框的椭圆。
this.drawBall(BM.ball.getX(), BM.ball.getY(), BM.ball.getWidth(), BM.ball.getHeight(), g);
}
public void drawBall(int x, int y, int width, int height, Graphics g) {
g.setColor(Color.red);
g.fillOval(x, y, width, height);
}
public void run() {
while (true) {
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();//数据在ball_move中设置
}
}
}
//球的参数
class Ball {
int x,y,width,height;
int x_spe