Java AWT BasicStroke create dashed line
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel { @Override/*from ww w . j ava 2 s . c om*/ public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // cast g to Graphics2D // draw 2D line using stroke float[] dashes = { 10 }; // specify dash pattern g2d.setPaint(Color.YELLOW); g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, 0)); g2d.draw(new Line2D.Double(320, 30, 395, 150)); } public static void main(String[] args) { JFrame frame = new JFrame("Drawing 2D shapes"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main Main = new Main(); frame.add(Main); frame.setSize(425, 200); frame.setVisible(true); } }