Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)

Painting to a JFrame

By: jcdj1996 on Jan 10th, 2013  |  syntax: Java  |  size: 1.06 KB  |  hits: 266  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class Display{
  2.         JFrame frame;  
  3.         JPanel contentPane;
  4.         public Display()
  5.         {
  6.                 frame = new JFrame("Painting example"); //initialize JFrame
  7.                 contentPane = new JPanel();
  8.                 contentPane.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
  9.                 contentPane.setLayout(new GridLayout(1,0));
  10.                
  11.                 panel.setContentPane(contentPane);      // Add content pane to frame
  12.                 contentPane.add(new MyPanel()); //Add paint surface
  13.         }
  14.         public static void main(String[] args)
  15.         {
  16.                 Display g1 = new Display();
  17.         }
  18. }
  19. class MyPanel extends JPanel{
  20.         public MyPanel() {
  21.                 setBorder(BorderFactory.createLineBorder(Color.black));
  22.         }
  23.  
  24.         public Dimension getPreferredSize() {
  25.                 return new Dimension(250,200);
  26.         }
  27.  
  28.         public void paintComponent(Graphics g) {
  29.                 super.paintComponent(g);
  30. // You can add paint objects here or implement double/triple buffering and access it from your main class(es)
  31.                 g.setColor(Color.BLACK);
  32.                 g.fillRect(0, 0, 1000, 1000);
  33.                 g.setColor(Color.RED);
  34.                 g.fillOval(100, 100, 10, 10);
  35.         }
  36. }