how to draw line in java graphics code example
Example: how to draw a line in java
import javax.swing.*;
import java.awt.*;
class Scratch extends JComponent{
public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Example");
window.setSize(600, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
Scratch drawing = new Scratch();
window.add(drawing);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(100, 200, 300, 400);
g2.draw(new Rectangle(10, 10, 10, 10));
g2.setColor(Color.GREEN);
g2.fill(new Rectangle(100, 100, 100, 100));
}
}