Oval Panel : Customized Component « Swing JFC « Java






Oval Panel

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

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

public class OvalPanel extends JPanel {

  Color color;

  public OvalPanel() {
    this(Color.black);
  }

  public OvalPanel(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);
    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(2, 2));

    Color colors[] = { Color.red, Color.blue, Color.green, Color.yellow };
    for (int i = 0; i < 4; i++) {
      OvalPanel panel = new OvalPanel(colors[i]);
      content.add(panel);
    }

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


           
       








Related examples in the same category

1.FontChooser dialogFontChooser dialog
2.Customized componentCustomized component
3.Ploygon ButtonPloygon Button
4.Demonstrating the Box ComponentDemonstrating the Box Component
5.The MyBean JavaBean Component
6.The TrafficLight Component
7.Alias BeanAlias Bean