draw Line Dashed - Java 2D Graphics

Java examples for 2D Graphics:Line

Description

draw Line Dashed

Demo Code


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

import java.awt.Graphics2D;

import java.awt.geom.Line2D;

public class Main {
    public static void drawLineDashed(Graphics2D g2d, int x1, int y1,
            int x2, int y2) {

        /*//from w  w w.  j a  v  a  2 s .  com
        BasicStroke stroke = new BasicStroke(
        10f,                       // grosor: 10 p?xels
        BasicStroke.CAP_BUTT,      // terminaci?n: recta
        BasicStroke.JOIN_ROUND,    // uni?n: redondeada
        1f,                        // ?ngulo: 1 grado
        new float[] {10, 5, 5, 5}, // l?nea de 10, 5 blancos, l?nea de 5, 5 blancos
        2                          // fase
        );*/

        Line2D line2D = new Line2D.Double(x1, y1, x2, y2);
        g2d.setColor(Color.orange);
        g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND, 1f, new float[] { 10, 5, 5, 5 }, 0f));
        g2d.draw(line2D);
    }

    public static void drawLineDashed(Graphics2D g2d, int x1, int y1,
            int x2, int y2, Color c) {

        /*
        BasicStroke stroke = new BasicStroke(
        10f,                       // grosor: 10 p?xels
        BasicStroke.CAP_BUTT,      // terminaci?n: recta
        BasicStroke.JOIN_ROUND,    // uni?n: redondeada
        1f,                        // ?ngulo: 1 grado
        new float[] {10, 5, 5, 5}, // l?nea de 10, 5 blancos, l?nea de 5, 5 blancos
        2                          // fase
        );*/

        Line2D line2D = new Line2D.Double(x1, y1, x2, y2);
        g2d.setColor(c);
        g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND, 1f, new float[] { 10, 5, 5, 5 }, 0f));
        g2d.draw(line2D);
    }
}

Related Tutorials