How to draw a line in Java?

To draw a line in Java, call the Graphics.drawLine method inside JFrame.paint.

Here is the smallest amount of code to draw a line in Java:

import javax.swing.*;
import java.awt.*;

public class DrawLine extends JFrame {

  public void paint(Graphics graphics) {
    graphics.drawLine(100, 100, 400, 400);

  public static void main(String[] args) {
    DrawLine frame = new DrawLine();
    frame.setSize(500, 500);

This program creates a 500x500 window (JFrame), that paints a diagonal inside of itself.