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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Show Image Effect 
 

To show Image effects, we have defined three matrix: RED_MATRIX, GREEN_MATRIX, BLUE_MATRIX which shows colors:

 

Show Image Effect

                         

This section shows you image effects.

To show Image effects, we have defined three matrix: RED_MATRIX, GREEN_MATRIX, BLUE_MATRIX  which shows colors:

 

 

 

 

static final float RED_MATRIX[][] = { { 2.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f } };
static final float GREEN_MATRIX[][] = { { 0.0f, 0.0f, 0.0f },
{ 0.0f, 2.0f, 0.0f }, { 0.0f, 0.0f, 0.0f } };
static final float BLUE_MATRIX[][] = { { 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 2.0f } };

We have used four radio buttons to perform different functions by calling ActionListener class. The getDefaultToolkit() method of Toolkit class provides the default toolkit. The method getImage() of class ToolKit returns an image.

The MediaTracker class provides a media objects including images. Create an instance of MediaTracker and call addImage() method which adds the specified image. The method waitForAll() of this class loads the image which is added.

The Raster class provides an array of pixels which store the values and locate the value in Databuffer. The BandCombineOp class provides a linear combination of the bands in a Raster with specified matrix.

Here is the code of ImageEffectExample.java

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class ImageEffectExample extends JFrame {
  ShowPanel spanel;
  JRadioButton button1, button2, button3, button4;

  public ImageEffectExample() {
    super("Show Image Effects");
    Container container = getContentPane();
    spanel = new ShowPanel();
    container.add(spanel);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(22));
    panel.setBorder(new TitledBorder("select"));
  ButtonGroup group=new ButtonGroup();

    button1 = new JRadioButton("Show Image Red");
  panel.add(button1);
  group.add(button1);
    button1.addActionListener(new ButtonListener());

    button2 = new JRadioButton("Show Image Green");
  panel.add(button2);
  group.add(button2);
    button2.addActionListener(new ButtonListener());

    button3 = new JRadioButton("Show Image Blue");
  panel.add(button3);
  group.add(button3);
    button3.addActionListener(new ButtonListener());

    button4 = new JRadioButton("Reset");
  panel.add(button4);
  group.add(button4);
    button4.addActionListener(new ButtonListener());
    container.add(BorderLayout.NORTH, panel);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  setSize(350,200);
    setVisible(true);
  }
    public static void main(String args[]) {
    new ImageEffectExample();
  }
    class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JRadioButton button = (JRadioButton) e.getSource();
      if (button.equals(button1)) {
        spanel.combine(ShowPanel.RED_MATRIX);
        spanel.repaint();
      else if (button.equals(button2)) {
        spanel.combine(ShowPanel.GREEN_MATRIX);
        spanel.repaint();
      else if (button.equals(button3)) {
        spanel.combine(ShowPanel.BLUE_MATRIX);
        spanel.repaint();
      else if (button.equals(button4)) {
        spanel.reset();
        spanel.repaint();
      }
    }
  }
}
class ShowPanel extends JLabel {
  static final float RED_MATRIX[][] = { { 2.0f0.0f0.0f },
      0.0f0.0f0.0f }, { 0.0f0.0f0.0f } };
  static final float GREEN_MATRIX[][] = { { 0.0f0.0f0.0f },
      0.0f2.0f0.0f }, { 0.0f0.0f0.0f } };
  static final float BLUE_MATRIX[][] = { { 0.0f0.0f0.0f },
      0.0f0.0f0.0f }, { 0.0f0.0f2.0f } };
Image image;
BufferedImage bufferedImage1;
BufferedImage bufferedImage2;
Raster raster;
WritableRaster writableRaster;
BufferedImage bufferedImage;
Graphics2D g2d;

ShowPanel() {
    loadImage();
    setSize(image.getWidth(this), image.getWidth(this));
    createImages();
    bufferedImage = bufferedImage1;
  }
  public void loadImage() {
    image = Toolkit.getDefaultToolkit().getImage("alert.gif");
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 1);
    try {
      mediaTracker.waitForAll();
    catch (Exception e) {}
    }

  public void createImages() {
    bufferedImage1 = new BufferedImage(image.getWidth(this), image
    .getHeight(this), BufferedImage.TYPE_INT_RGB);
    g2d = bufferedImage1.createGraphics();
    g2d.drawImage(image, 00this);
    raster = bufferedImage1.getRaster();
    bufferedImage2 = new BufferedImage(image.getWidth(this), image
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
    writableRaster = (WritableRaster) bufferedImage2.getRaster();
  }
    public void combine(float[][] combineMatrix) {
    BandCombineOp band = new BandCombineOp(combineMatrix, null);
    band.filter(raster, writableRaster);
    bufferedImage = bufferedImage2;
  }
 public void reset() {
    g2d.setColor(Color.black);
    g2d.clearRect(00, bufferedImage.getWidth(this), bufferedImage.
       getHeight(
this));
    g2d.drawImage(image, 00this);
    bufferedImage = bufferedImage1;
  }
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;
    g2D.drawImage(bufferedImage, 00this);
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c com class orm ant form model io components sed color format component value byte int rgb show pixel for

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.