Share on Google+Share on Google+

Show Alpha Value and Alpha Rules

In this section, you will study about the alpha-values and alpha-rules.

Show Alpha Value and Alpha Rules


In this section, you will study about the alpha-values and alpha-rules.

To give blending and transparency effects with graphics and images and the alpha values, the class AlphaComposite is used. An alpha value of 0.0f makes the source disappear and 1.0f make it completely opaque. Various degrees of translucency exist between the 0.0f and 1.0f. We are providing you an example.

We have defined two ComboBox for alpha-values and alpha-rules respectively. We have given five alpha-values i.e. 0.0,  0.25, 0.50, 0.75, 1.0.  The existing image is called destination and the image rendered on it is the source. 

ItemEvent.SELECTED is the state-change value which indicates that an item was selected. The getSelectedItem() returns the selected item. To show  different alpha-values and alpha-rules, the class Ellipse2D defines two ovals which act as a source and destination. In the given example, cyan colored oval act as a destination and red colored oval act as a source.

Here is the code of

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

public class CompositeExample extends JApplet implements ItemListener {
  ComponentPanel component;
  JLabel label1 = new JLabel("Alpha values");
  JComboBox combo1= new JComboBox();
  JLabel label2 = new JLabel("Rules");
  JComboBox combo2 = new JComboBox();
  String alpha = "1.0";
  int rule = 0;

  public void init() {
  getContentPane().setLayout(new FlowLayout());
  component = new ComponentPanel();
  public void itemStateChanged(ItemEvent event) {
  if (event.getStateChange() != ItemEvent.SELECTED) {
  Object object = event.getSource();
  if (object == combo1) {
  alpha = (String) combo1.getSelectedItem();
  else {
  rule = combo2.getSelectedIndex();
  component.change(alpha, rule);
  public static void main(String s[]) {
  JFrame frame = new JFrame("Composite Example");
  frame.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent event) {
  JApplet applet = new CompositeExample();
  frame.getContentPane().add("Center", applet);
  frame.setSize(new Dimension(350330));;
class ComponentPanel extends JPanel {
  AlphaComposite alphaCo = AlphaComposite.getInstance(AlphaComposite.SRC);
  float alpha = 1.0f;
  public ComponentPanel() {
  setPreferredSize(new Dimension(350330));
  public void change(String a, int rule) {
  alpha = Float.valueOf(a).floatValue();
  alphaCo = AlphaComposite.getInstance(getRule(rule), alpha);
  public int getRule(int rule) {
  int alphaComp = 0;
  switch (rule) {
  case 0:
  alphaComp = AlphaComposite.SRC;
  case 1:
  alphaComp = AlphaComposite.SRC_IN;
  case 2:
  alphaComp = AlphaComposite.SRC_OVER;
  case 3:
  alphaComp = AlphaComposite.SRC_OUT;
  case 4:
  alphaComp = AlphaComposite.DST;
  case 5:
  alphaComp = AlphaComposite.DST_IN;
  case 6:
  alphaComp = AlphaComposite.DST_OVER;
  case 7:
  alphaComp = AlphaComposite.DST_OUT;
  case 8:
  alphaComp = AlphaComposite.CLEAR;
  return alphaComp;
  public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  Dimension dim = getSize();
  int w = dim.width;
  int h = dim.height;
  BufferedImage bufferedImage = new BufferedImage(w, h,
  Graphics2D graph = bufferedImage.createGraphics();
  g2d.fillRect(00, dim.width, dim.height);
  graph.fill(new Ellipse2D.Double(150100150100));
  graph.fill(new Ellipse2D.Double(170,150150100));
  g2d.drawImage(bufferedImage, null, 00);

Output will be displayed as:

Download Source Code



Posted on: October 1, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Show Alpha Value and Alpha Rules  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image