draw Dashed Line - Java java.awt

Java examples for java.awt:Graphics2D

Description

draw Dashed Line

Demo Code


//package com.java2s;
import java.awt.BasicStroke;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;

public class Main {
    public static void drawDashedLine(Graphics g, double x1, double y1,
            double x2, double y2) {

        //creates a copy of the Graphics instance
        Graphics2D g2d = (Graphics2D) g.create();

        Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL, 0, new float[] { 9 }, 0);
        g2d.setStroke(dashed);/*from  ww  w  . j a v a2s.co m*/
        g2d.drawLine((int) x1, (int) y1, (int) x2, (int) y2);

        //gets rid of the copy
        g2d.dispose();
    }
}

Related Tutorials