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.
Asked
Active
Viewed 5,083 times
3
-
If you can produce a dashed line, a `Thread` (or a Swing `Timer`) combined with `repaint()` and some tweaking of where the dashes start and end - should achieve it. – Andrew Thompson Mar 19 '12 at 14:32
-
2@AndrewThompson: the `BasicStroke` class has a dash phase property which can be used for this purpose. – lhballoti Mar 19 '12 at 14:53
-
@lhballoti Aah, the `dashPhase` - good call. – Andrew Thompson Mar 19 '12 at 15:29
1 Answers
21
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.
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,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);
}
});
}
}

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
@trashgod It's nice to know the common name/history of a very recognizable animated border. And love the name - ants are bloody amazing (herding, agriculture, building complex structures.. - I just hope they don't gain 'fire' in their toolkit any time soon). ;) – Andrew Thompson Oct 01 '13 at 19:13
-
Java 7 has a [StrokeBorder](http://docs.oracle.com/javase/7/docs/api/javax/swing/border/StrokeBorder.html). You don't need to do the painting yourself anymore then. – Timmos Oct 19 '13 at 22:49