使用java.awt.BasicStroke动画化虚线

时间:2012-03-19 14:23:18

标签: java animation graphics awt

有没有办法使用java.awt中的BasicStroke生成动画虚线?我的愿望是以与Photoshop的矩形品牌工具的动画线相同的方式运行虚线。

1 个答案:

答案 0 :(得分:20)

使用虚线,Thread(或Swing Timer)&将它们与repaint()结合起来,并对破折号的起点和终点进行一些调整 - 然后就可以了。

实施例

Animated Dashed Line]

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);
            }
        });
    }
}