编写图形界面程序,显示一个红色反弹球的程序,当该球撞击Applet边框时,它应从边框弹回并以相反方向45°运动。

编写图形界面程序,显示一个红色反弹球的程序,当该球撞击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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值