Botre Posted February 19, 2016 Share Posted February 19, 2016 (edited) Class: package temp; import java.awt.BasicStroke; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Observable; import javax.swing.Timer; public class MarchingAnts extends Observable { private BasicStroke stroke; private float dashPhase = 0f; private float dash[] = {5.0f,5.0f}; private ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { dashPhase += 9.0f; stroke = new BasicStroke(1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 1.5f, dash, dashPhase); setChanged(); notifyObservers(); } }; public MarchingAnts() { stroke = new BasicStroke(1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 1.5f, dash, dashPhase); } private Timer timer = new Timer(40, listener); public BasicStroke getStroke() { return stroke; } public void startAnimation() { timer.start(); } public void stopAnimation() { timer.stop(); } } Example: //Declare private MarchingAnts ants = new MarchingAnts(); ... //Start animation ants.startAnimation(); ... //Add observers ants.addObserver((obj, obs) -> repaint()); //repaint() method from a painting component ... //Use stroke g2d.setStroke(ants.getStroke()); Edited February 19, 2016 by Botre 5 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted February 19, 2016 Share Posted February 19, 2016 Nice snippet, but I like ducks more then ants Quote Link to comment Share on other sites More sharing options...
Botre Posted February 19, 2016 Author Share Posted February 19, 2016 Nice snippet, but I like ducks more then ants 4 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted February 19, 2016 Share Posted February 19, 2016 Omfg I love it Quote Link to comment Share on other sites More sharing options...