Painting in Swing

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Paint extends JPanel
{
Insets ins;
Random rand;
Paint()
{
setBorder(BorderFactory.createLineBorder(Color.RED, 5));
rand = new Random();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
int x, y, x2, y2;
int height = getHeight();
int width = getWidth();
ins = getInsets();
for(int i=0; i < 10; i++)
{
x = rand.nextInt(width-ins.left);
y = rand.nextInt(height-ins.bottom);
x2 = rand.nextInt(width-ins.left);
y2 = rand.nextInt(height-ins.bottom);
g.drawLine(x, y, x2, y2);
}
}
}
class Painting
{
JLabel jlab;
Paint p1;
Painting()
{
JFrame jfrm = new JFrame("Paint Demo");
jfrm.setSize(200, 150);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new Paint();
jfrm.add(p1);
jfrm.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new Painting();
}
});
}
}

Output:
Painting in Swing