JAVA 简单动画

package Rong;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class BounceBall extends JFrame {
  JPanel panel = new JPanel(); // 主面板
  Circle c;
  // 构造函数
  public BounceBall() {
  add(panel); // 添加面板
  panel.setLayout(null); // 设置面板的布局为空布局
  c = new Circle(panel);
  panel.add(c);
  c.setBounds(40, 140, 30, 30);
  c = new Circle(panel, Color.red);
  panel.add(c);
  c.setBounds(50, 10, 30, 30);
  // 设置窗体的属性
  this.setTitle("弹弹球");
  this.setBounds(400, 140, 400, 300);
  this.setVisible(true);
  this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  }
  //主函数
  public static void main(String[] args) {
  new BounceBall();
  }
}
class Circle extends Canvas implements ActionListener {
  private int TOP = 0; // 小球能够达到的上边界
  private int BOTTOM; // 小球能够达到的下边界
  private int LEFT = 0; // 小球能够达到的左边界
  private int RIGHT; // 小球能够达到的右边界
  private Color c; // 小球的颜色
  private int R = 30; // 小球的半径
  private int xSpeed = 8; // 小球x轴上的速度
  private int ySpeed = 7; // 小球y轴上的速度
  Timer t = new Timer(1, this); // 设置定时器
  JPanel panel;

  public void actionPerformed(ActionEvent e) { // 定义事件的行为
  RIGHT = panel.getWidth();
  BOTTOM = panel.getHeight();
  Point currentPoint = this.getLocation(); // 获得当前的坐标
  double x = currentPoint.getX();
  double y = currentPoint.getY();
  x = x + xSpeed;
  y = y + ySpeed;
  if ((x + R) > RIGHT || x < 0) {
    xSpeed = -xSpeed;
  }
  if (y < 0 || (y + R) > BOTTOM) {
    ySpeed = -ySpeed;
  }
  currentPoint.setLocation(x, y);
  this.setLocation(currentPoint);
  }
  public void paint(Graphics g) {
  g.setColor(c);
  g.fillOval(0, 0, R, R);
  }
  // 构造函数一:参数为要添加的面板
  public Circle(JPanel panel) {
  this.panel = panel;
  t.start();
  }
  // 构造函数二:参数为要添加的面板和颜色
  public Circle(JPanel panel, Color c) {
  this.panel = panel;
  this.c = c;
  t.start();
  }
} JAVA <wbr>简单动画

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值