Studyon Minte9.com
Java SE




Line




package testexample; import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 400, 300); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } public Test() { JPanel p = new JPanel(){ @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // Vertical line // --- Look Here --- // g2.setColor(Color.black); // color black default g2.draw(new Line2D.Double(50, 10, 50, 100)); // x1, y1, x2, y2 // Oblique line g2.setColor(Color.blue); g2.draw(new Line2D.Double(100, 10, 120, 100)); // Graph for (int i=0; i<100; i += 10) { g2.setColor(Color.red); g2.setStroke(new BasicStroke(5)); int x1 = 150 + i; int x2 = x1; int y1 = 10 + i; int y2 = 150; g2.draw(new Line2D.Double(x1, y1, x2, y2)); } } }; add(p); } }