Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 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

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.