A Bean information class. : SimpleBeanInfo « Development Class « Java






A Bean information class.

 
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;

public class ColorsBeanInfo extends SimpleBeanInfo {
  public PropertyDescriptor[] getPropertyDescriptors() {
    try {
      PropertyDescriptor rectangular = new PropertyDescriptor("rectangular", Colors.class);
      PropertyDescriptor pd[] = { rectangular };
      return pd;
    } catch (Exception e) {
    }
    return null;
  }
}


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;

import javax.swing.JLabel;

public class Colors extends JLabel implements Serializable {
  transient private Color color; // not persistent

  private boolean rectangular; // is persistent

  public Colors() {
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        change();
      }
    });
    rectangular = false;
    setSize(200, 100);
    change();
  }

  public boolean getRectangular() {
    return rectangular;
  }

  public void setRectangular(boolean flag) {
    this.rectangular = flag;
    repaint();
  }

  public void change() {
    color = randomColor();
    repaint();
  }

  private Color randomColor() {
    int r = (int) (255 * Math.random());
    int g = (int) (255 * Math.random());
    int b = (int) (255 * Math.random());
    return new Color(r, g, b);
  }

  public void paint(Graphics g) {
    Dimension d = getSize();
    int h = d.height;
    int w = d.width;
    g.setColor(color);
    if (rectangular) {
      g.fillRect(0, 0, w - 1, h - 1);
    } else {
      g.fillOval(0, 0, w - 1, h - 1);
    }
  }
}

 








Related examples in the same category

1.Show properties and events.