Java AWT Graphics2D draw line with Line2D
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel { Random rnd = new Random(); @Override// w ww . ja v a 2 s .com public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.BLACK); g2d.clearRect(0, 0, getParent().getWidth(), getParent().getHeight()); // White line g2d.setPaint(Color.WHITE); g2d.setStroke(new BasicStroke(10.0f)); g2d.draw(new Line2D.Float(150, 10, 10, 160)); } public static void main(String[] args) { // create frame for Main JFrame frame = new JFrame("java2s.com"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Main Main = new Main(); frame.add(Main); frame.setSize(300, 210); frame.setVisible(true); } }