# Java: JPanel - Example

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
    // File   : gui/low-level/drawing2/DemoDrawing2.java
    // Purpose: Demo creating new graphical component.
    //          Uses anonymous inner class listener, BorderLayout
    // Author : Fred Swartz - 21 Sept 2006 - Placed in public domain.

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

    ///////////////////////////////////////////////////// DemoDrawing2
    public class DemoDrawing2 extends JFrame {

        //===================================================== fields
        private DrawingArea left = new DrawingArea();
        private DrawingArea rite = new DrawingArea();

        //================================================ Constructor
        public DemoDrawing2() {
            left.setBackground(Color.white);
            rite.setBackground(Color.black);

            JButton changeColorBtn = new JButton("Randomize Colors");
            changeColorBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        left.setMyColor(randomColor());
                        rite.setMyColor(randomColor());
                    }
                });

            //... Add components to layout
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout(5, 5));
            content.add(changeColorBtn, BorderLayout.NORTH);
            content.add(left, BorderLayout.WEST);
            content.add(rite, BorderLayout.EAST);

            //... Set window characteristics
            setContentPane(content);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("Demo Drawing");
            setLocationRelativeTo(null);  // Center window.
            pack();
        }

        //=================================================== randomColor
        private static Color randomColor() {
            return new Color((int) (Math.random() * 256),  // Red
                             (int) (Math.random() * 256),  // Green
                             (int) (Math.random() * 256)); // Blue
        }

        //========================================================== main
        public static void main(String[] args) {
            JFrame window = new DemoDrawing2();
            window.setVisible(true);
        }
    }

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
    // File   : gui/low-level/drawing2/DrawingArea.java
    // Purpose: Demo subclassing JPanel, overriding paintComponent,
    //          private state, use of repaint().
    // Author : Fred Swartz - 21 Sept 2006 - Placed in public domain.

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

    ///////////////////////////////////////////////////////// DrawingArea
    class DrawingArea extends JPanel {

        //======================================================== fields
        private Color _ovalColor;      // Color of oval.

        //=================================================== constructor
        public DrawingArea() {
            _ovalColor = Color.GREEN;  // Initial color.
            setPreferredSize(new Dimension(100,100));
        }

        //================================================ paintComponent
        @Override public void paintComponent(Graphics g) {
            super.paintComponent(g);  // Ask parent to paint background.

            g.setColor(_ovalColor);
            int w = getWidth();       // Size might have changed if
            int h = getHeight();      // user resized window.
            g.fillOval(0, 0, w, h);
        }

        //==================================================== setMyColor
        public void setMyColor(Color col) {
            _ovalColor = col;  // Remember color.
            repaint();         // Color changed, must repaint
        }
    }