JPanel: paintComponent(Graphics g) : JPanel « javax.swing « Java by API






JPanel: paintComponent(Graphics g)

  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  Color color;

  public MainClass(Color color) {
    this.color = color;
  }

  public void paintComponent(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(color);
    g.drawOval(0, 0, width, height);
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Oval Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(2, 2));

    Color colors[] = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
    for (int i = 0; i < 4; i++) {
      MainClass panel = new MainClass(colors[i]);
      frame.add(panel);
    }

    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

           
         
    
  








Related examples in the same category

1.JPanel: addAncestorListener(AncestorListener lis)
2.JPanel: addContainerListener(ContainerListener l)
3.JPanel: addMouseListener(MouseListener mous)
4.JPanel: addMouseMotionListener(MouseMotionListener lis)
5.JPanel: addKeyListener(KeyListener l)
6.JPanel: getFocusTraversalKeys(int id)
7.JPanel: isFocusCycleRoot()
8.JPanel: repaint()
9.JPanel: setBackground(Color c)
10.JPanel: setBorder(Border border)
11.JPanel: setComponentPopupMenu(JPopupMenu popup)
12.JPanel: setFocusCycleRoot(boolean value)
13.JPanel: setFocusTraversalPolicyProvider(boolean value)
14.JPanel: setLayout(null)
15.JPanel: setOpaque(boolean isOpaque)
16.JPanel: setPreferredSize(Dimension preferredSize)
17.JPanel: setToolTipText(String htmlString)
18.extends JPanel