Advertisement
jcdj1996

Painting to a JFrame

Jan 10th, 2013
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement