zoukankan      html  css  js  c++  java
  • 动画画图

    动画画图


    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class DoRun extends JPanel {
    private static final long serialVersionUID = 1L;

    private final int DELAY = 50; // 转动快慢设置
    // private final static Long time = (long) 5000; //窗体关闭事件
    private static Timer timer; //动画计时器
    private int x = 0;
    private int scrolleWidth = 0;
    /**
    * 调用
    */
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //本类为Panel
    frame.add(new DoRun());
    frame.setSize(600, 200);
    frame.setLocation(400, 400);
    frame.setVisible(true);
    //窗体定时关闭
    /*try {
    Thread.sleep(time);
    } catch (InterruptedException e) {
    }
    // 停止 Timer,使它停止向其侦听器发送动作事件。
    timer.stop();
    frame.setVisible(false);
    frame.dispose();*/

    }

    /**
    * 面板构造函数,初始化面板。包括Timer 的场景。
    */
    public DoRun() {
    timer = new Timer(DELAY, new ReboundListener());
    timer.start();
    }

    /**
    * 动画效果:不断的更新图像的位置,以达到动画的效果。
    */
    private class ReboundListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
    if (x < 360) {
    //控制每个DELAY周期旋转的角度,+ 为逆时针 - 为顺时针
    x = x - 5;
    } else {
    x = 0;
    }
    if (scrolleWidth < 200) {
    scrolleWidth = scrolleWidth + 10;
    }else {
    scrolleWidth = 0;
    }
    repaint();
    }
    }

    /**
    * 绘出图像在面板中的位置
    */
    public void paintComponent(Graphics page) {
    super.paintComponent(page);
    drawArc(page);
    }

    /**
    * 画图形
    */
    private void drawArc(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    //抗锯齿
    //JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/awt/RenderingHints.html
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(Color.blue);
    g2d.fillArc(10, 10, 100 , 100 , x, 240);
    g2d.setColor(Color.BLACK);
    g2d.fillArc(30, 30, 60 , 60 , 0, 360);
    g2d.dispose();


    Graphics2D g2d2 = (Graphics2D) g.create();
    g2d2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    //设置画笔颜色
    g2d2.setColor(Color.BLACK);
    g2d2.drawRoundRect(120, 10, 300 , 30 , 10, 10);
    g2d2.setColor(Color.blue);
    g2d2.fillRoundRect(120 + scrolleWidth,10, 100, 30 , 10,10);

    /* g2d2.setColor(Color.BLACK);
    g2d2.fillArc(140, 30, 60 , 60 , 0, 360);*/
    g2d2.dispose();
    }
    }

    参考 

    https://blog.csdn.net/qq_18878455/article/details/88831345

  • 相关阅读:
    C#8.0新特性
    C#7.0新特性
    C#6.0新特性
    C#6.0到C#8.0的新特性
    纪念博客开通的6月29日
    什么是开发中经常说的'POCO'
    什么时候用Model,什么时候用Entity?
    C#数组的 Length 和 Count()
    C#中foreach的实现原理
    Windows Server 2012 R2远程桌面默认端口修改
  • 原文地址:https://www.cnblogs.com/yuluoxingkong/p/15380824.html
Copyright © 2011-2022 走看看