public class Display{
JFrame frame;
JPanel contentPane;
public Display()
{
frame = new JFrame("Painting example"); //initialize JFrame
contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
contentPane.setLayout(new GridLayout(1,0));
panel.setContentPane(contentPane); // Add content pane to frame
contentPane.add(new MyPanel()); //Add paint surface
}
public static void main(String[] args)
{
Display g1 = new Display();
}
}
class MyPanel extends JPanel{
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// You can add paint objects here or implement double/triple buffering and access it from your main class(es)
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1000, 1000);
g.setColor(Color.RED);
g.fillOval(100, 100, 10, 10);
}
}