Using Java awt. Basicstroke animated dashed line

Is there any way to use Java Basic stroke in AWT generates animated dashed lines? My wish is to run the dotted line in the same way as the animation line of Photoshop's rectangular brand tool

Solution

Use dashed lines, threads (or swing timers) & combine them with repeat() and make some adjustments to the start and end of dashes – then it's OK

example

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,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);
            }
        });
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>