动画与java.awt.BasicStroke中的虚线 [英] Animating dashed-line with java.awt.BasicStroke

查看:2138
本文介绍了动画与java.awt.BasicStroke中的虚线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有生产使用的BasicStroke从java.awt中的动画虚线的方式?我的愿望是有同样的方式,Photoshop的长方形劳斯莱斯工具都有其行动画正在运行的虚线。

Is there a way to produce animated dashed line using BasicStroke from java.awt? My desire is to have a running dashed-line in the same way that photoshop's rectangle marque tool has its line animated.

推荐答案

使用虚线,在(或一个Swing 定时)及与重绘()和那里的破折号开始一些调整和终止结合他们 - 和你有它。

Use a dashed line, a Thread (or a Swing Timer) & combine them with repaint() and some tweaking of where the dashes start and end - and there you have it.

package test;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class AnimatedStroke {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                BasicStroke dashedStroke;
                final int width = 100;
                final int height = 30;
                final BufferedImage image = new BufferedImage(
                        width,height,BufferedImage.TYPE_INT_ARGB);
                final JLabel label = new JLabel(new ImageIcon(image));
                int pad = 5;
                final Shape rectangle = new Rectangle2D.Double(
                        (double)pad,(double)pad,
                        (double)(width-2*pad),
                        (double)(height-2*pad));

                ActionListener listener = new ActionListener() {

                    float dashPhase = 0f;
                    float dash[] = {5.0f,5.0f};
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        dashPhase += 9.0f;
                        BasicStroke dashedStroke = new BasicStroke(
                                1.5f,
                                BasicStroke.CAP_ROUND,
                                BasicStroke.JOIN_MITER,
                                1.5f, //miter limit
                                dash,
                                dashPhase
                                );
                        Graphics2D g = image.createGraphics();

                        g.setColor(Color.WHITE);
                        g.fillRect(0,0,width,height);

                        g.setColor(Color.BLACK);
                        g.setStroke(dashedStroke);
                        g.draw(rectangle);

                        g.dispose();
                        label.repaint();
                        /*
                        if (dashPhase<100f) {
                            try {
                                ImageIO.write(
                                        image, 
                                        "PNG", 
                                        new File("img" + dashPhase + ".png"));
                            } catch(IOException ioe) {
                                // we tried
                            }
                        }*/
                    }
                };
                Timer timer = new Timer(40, listener);
                timer.start();
                JOptionPane.showMessageDialog(null, label);
            }
        });
    }
}

这篇关于动画与java.awt.BasicStroke中的虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆