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.

Show Alpha Value 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 CompositeExample.java

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());
  getContentPane().add(label1);
  combo1.addItem("1.0");
  combo1.addItem("0.75");
  combo1.addItem("0.50");
  combo1.addItem("0.25");
  combo1.addItem("0.0");
  combo1.addItemListener(this);
  getContentPane().add(combo1);
  getContentPane().add(label2);
  combo2.addItem("SRC");
  combo2.addItem("SRC_IN");
  combo2.addItem("SRC_OVER");
  combo2.addItem("SRC_OUT");
  combo2.addItem("DST");
  combo2.addItem("DST_IN");
  combo2.addItem("DST_OVER");
  combo2.addItem("DST_OUT");
  combo2.addItem("CLEAR");
  combo2.addItemListener(this);
  getContentPane().add(combo2);
  component = new ComponentPanel();
  getContentPane().add(component);
  validate();
  }
  public void itemStateChanged(ItemEvent event) {
  if (event.getStateChange() != ItemEvent.SELECTED) {
  return;
  }
  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) {
  System.exit(0);
  }
  });
  JApplet applet = new CompositeExample();
  frame.getContentPane().add("Center", applet);
  applet.init();
  frame.pack();
  frame.setSize(new Dimension(350330));
  frame.show();
  }
}
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);
  repaint();
  }
  public int getRule(int rule) {
  int alphaComp = 0;
  switch (rule) {
  case 0:
  alphaComp = AlphaComposite.SRC;
  break;
  case 1:
  alphaComp = AlphaComposite.SRC_IN;
  break;
  case 2:
  alphaComp = AlphaComposite.SRC_OVER;
  break;
  case 3:
  alphaComp = AlphaComposite.SRC_OUT;
  break;
  case 4:
  alphaComp = AlphaComposite.DST;
  break;
  case 5:
  alphaComp = AlphaComposite.DST_IN;
  break;
  case 6:
  alphaComp = AlphaComposite.DST_OVER;
  break;
  case 7:
  alphaComp = AlphaComposite.DST_OUT;
  break;
  case 8:
  alphaComp = AlphaComposite.CLEAR;
  break;
  }
  return alphaComp;
  }
  public void paint(Graphics g) {
  super.paint(g);
  Graphics2D g2d = (Graphics2D) g;
  Dimension dim = getSize();
  int w = dim.width;
  int h = dim.height;
  BufferedImage bufferedImage = new BufferedImage(w, h,
  BufferedImage.TYPE_INT_ARGB);
  Graphics2D graph = bufferedImage.createGraphics();
  g2d.setColor(Color.white);
  g2d.fillRect(00, dim.width, dim.height);
  graph.setColor(Color.cyan);
  graph.fill(new Ellipse2D.Double(150100150100));
  graph.setColor(Color.red);
  graph.setComposite(alphaCo);
  graph.fill(new Ellipse2D.Double(170,150150100));
  g2d.drawImage(bufferedImage, null, 00);
  }
}

Output will be displayed as:

Download Source Code