Title: Dashes
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Dashes

We are going to make use of another BasicStroke constructor to create dashed lines:

BasicStroke(float width, int cap, int join, float miterlimit,
float[] dash, float dash_phase)

Dashes_Basic_Strokes.java



import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;

public class Dashes_Basic_Strokes extends JFrame{
    Dashes_Basic_Strokes()
    {
        setSize(600, 420);
        setTitle("Dashes_Basic_Strokes");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();

      float[] dash1 = {1f};
      //float[] dash1 = {1f,1f};
      //float[] dash1 = {1f,1f,1f};

      float[] dash2 = {8f};
      //float[] dash2 = {8f,8f};

      float[] dash3 = {8f,  3f};
      float[] dash4 = {8f, 8f, 2f};
      float[] dash5 = {16f, 8f, 4f, 2f, 1f};

      // vertical line on the left
      g2d.setColor(Color.red);
      g2d.drawLine(20, 20, 20, 300);

      // top line using the default Basic Stroke
      g2d.setColor(Color.black);
      g2d.drawLine(20, 40, 250, 40);
      g2d.drawString("first line", 260, 40);

      int stroke_size=1;
      float dash_phase = 4f; // See what changes when this is 0

      // Used for the second line
      BasicStroke bs2 = new BasicStroke(stroke_size, BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_ROUND, 1.0f, dash1, dash_phase);

      // Used for the third line
      BasicStroke bs3 = new BasicStroke(stroke_size, BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_ROUND, 1.0f, dash2, dash_phase);

      // Used for the fourth line
      BasicStroke bs4 = new BasicStroke(stroke_size, BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_ROUND, 1.0f, dash3, dash_phase);

      // Used for the fifth line
      BasicStroke bs5 = new BasicStroke(stroke_size, BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_ROUND, 1.0f, dash4, dash_phase);

      // Used for the sixth line
      BasicStroke bs6 = new BasicStroke(stroke_size, BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_ROUND, 1.0f, dash5, dash_phase);

      g2d.setStroke(bs2);
      g2d.drawLine(20, 80, 250, 80);
      g2d.drawString("second line", 260, 80);

      g2d.setStroke(bs3);
      g2d.drawLine(20, 120, 250, 120);
      g2d.drawString("third line", 260, 120);

      g2d.setStroke(bs4);
      g2d.drawLine(20, 160, 250, 160);
      g2d.drawString("fourth line", 260, 160);

      g2d.setStroke(bs5);
      g2d.drawLine(20, 200, 250, 200);
      g2d.drawString("fifth line", 260, 200);

      g2d.setStroke(bs6);
      g2d.drawLine(20, 240, 250, 240);
      g2d.drawString("sixth line", 260, 240);

        g2d.dispose();
    }

    public static void main(String[] args)
    {
        Dashes_Basic_Strokes bs1 = new Dashes_Basic_Strokes();
    }

}





Dashes_Animated.java



import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Dashes_Animated extends JFrame implements ActionListener{
    float dash_phase =1f;
    Timer timer;

    Dashes_Animated()
    {
        setSize(380, 250);
        setTitle("Dashes_Animated");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        timer = new Timer(100, this);
        timer.setInitialDelay(50);
        timer.start();
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        Dimension d = getSize();
        g.fillRect(0, 0, d.width, d.height);


        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.black);

        float[] dash1 = {8f, 3f};
        float[] dash2 = {16f, 4f};

        BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1.0f, dash1, dash_phase);

        BasicStroke bs2 = new BasicStroke(1, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1.0f, dash2, dash_phase);

        g2d.setStroke(bs1);
        g2d.drawRect(50, 50, 300, 150);
        g2d.setStroke(bs2);
        g2d.drawRect(100, 100, 200, 50);

        g2d.dispose();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        dash_phase += 4f;
        repaint();
    }

    public static void main(String[] args)
    {
        Dashes_Animated bs1 = new Dashes_Animated();
    }

}





Last Updated: June 17, 2015 2:28 PM