DashedStrokeDemo.java Source code

Java tutorial

Introduction

Here is the source code for DashedStrokeDemo.java

Source

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DashedStrokeDemo extends JPanel {
    public void init() {
        setBackground(Color.white);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        float dash[] = { 10.0f };
        g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));

        g2.setPaint(Color.blue);

        Rectangle r = new Rectangle(5, 5, 200, 200);

        g2.draw(r);
    }

    public static void main(String s[]) {
        JFrame f = new JFrame();
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        DashedStrokeDemo p = new DashedStrokeDemo();
        f.getContentPane().add("Center", p);
        p.init();
        f.pack();
        f.setSize(new Dimension(250, 250));
        f.show();
    }

}