How do I create a dashed stroke in Java 2D?

package org.kodejava.example.awt.geom;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class DrawDashedStroke extends JApplet {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        float[] dash = {10.0f, 5.0f, 3.0f};

        //
        // Creates a dashed stroke
        //
        Stroke dashed = new BasicStroke(2.0f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);

        g2.setStroke(dashed);
        g2.setPaint(Color.RED);
        g2.draw(new RoundRectangle2D.Double(50, 50, 300, 100, 10, 10));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Dashed Stroke Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JApplet applet = new DrawDashedStroke();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setSize(new Dimension(400, 250));
        frame.setVisible(true);
    }
}

This code snippet produce the following output:

Draw Dashed Stroke Demo

Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply