Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7669669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:38:53+00:00 2026-05-31T15:38:53+00:00

Is there a way to produce animated dashed line using BasicStroke from java.awt? My

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:38:54+00:00Added an answer on May 31, 2026 at 3:38 pm

    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

    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);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to produce MSDN-style documentation from the Visual Studio XML
Is there a way to produce javadoc so that all methods accessible from the
In R is there any way to produce plots which have no title and
In Rails 3, is there a way to produce an ActiveRecord object from xml
Is there a way to produce a PDF of a table from R in
Is there a way to produce a diagram showing existing tables and their relationships
Is there a way to produce colorful output (like diagnostic messages and status messages)
Is there a easy way how to produce following matrix: a = 4 5
Is there any way to convert differential backup to SQL statements which will produce
I'm wondering if is there a way to force MSSQL Management Studio to produce

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.