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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Show Other Image Effects 
 

To show image effects like sharpen, blur, show edge, reset ,etc, we have create radio buttons on which the action will be performed by calling ActionListener class.

 

Show Other Image Effects

                         

This section illustrates you some other image effects

To show image effects like sharpen, blur, show edge, reset ,etc, we have create radio buttons on which the action will be performed by calling ActionListener class. The method getDefaultToolkit() of class Toolkit provides the default toolkit. The method getImage() returns the specified image.

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

A matrix is defined in the variable value of float type. This matrix is defined by the class Kernel to describe how a pixel affects its position in the output image of a filtering operation. 

The class ConvolveOp provides the correlation from the source to the destination. The filter() method of this class performs a cross relation on BufferedImages. The ConvolveOp.EDGE_NO_OP,null copies the pixels of source image to the corresponding pixels in the destination image without any modification.

Here is the code of SomeImageEffects.java

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

public class SomeImageEffects extends JFrame {
  ShowLabel label;

  JRadioButton button1, button2, button3, button4, button5;
    public SomeImageEffects() {
    super("Show Image Effects");
    Container container = getContentPane();
    label = new ShowLabel();
    container.add(label);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(22));
    panel.setBorder(new TitledBorder("Select"));
    ButtonGroup group=new ButtonGroup();
    button1 = new JRadioButton("Sharpen");
    panel.add(button1);
    group.add(button1);
    button1.addActionListener(new ButtonListener());
    button2 = new JRadioButton("Blur");
  panel.add(button2);
  group.add(button2);
    button2.addActionListener(new ButtonListener());
    button3 = new JRadioButton("Show Edge");
  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);
    setSize(350,300);
    setVisible(true); 
  }
    public static void main(String arg[]) {
    new SomeImageEffects();
  }
  class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JRadioButton button = (JRadioButton) e.getSource();

      if (button.equals(button1)) {
        label.sharpen();
        label.repaint();
      else if (button.equals(button2)) {
        label.blur();
        label.repaint();
      else if (button.equals(button3)) {
        label.showEdge();
        label.repaint();
      else if (button.equals(button4)) {
        label.reset();
        label.repaint();
      }
    }
  }
}
class ShowLabel extends JLabel {
  Image image;
  BufferedImage bufferedImage1,bufferedImage2;
  BufferedImage bufferedImage; 
  Graphics2D g2d;

  ShowLabel() {
    image = Toolkit.getDefaultToolkit().getImage("image4.jpg");
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 1);
    try {
      mediaTracker.waitForAll();
    catch (Exception e) {}
    setSize(image.getWidth(this), image.getWidth(this)); 
    createImages();
    bufferedImage = bufferedImage1;
  }
    public void createImages() {
    bufferedImage1 = new BufferedImage(image.getWidth(this), image
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
    g2d = bufferedImage1.createGraphics();
    g2d.drawImage(image, 00this);
    bufferedImage2 = new BufferedImage(image.getWidth(this), image
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
  }
    public void sharpen() {
    float value[] = { -1.0f, -1.0f, -1.0f, -1.0f9.0f, -1.0f, -1.0f, -1.0f,
        -1.0f };
    Kernel kernel = new Kernel(33, value);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,null);
    convolve.filter(bufferedImage1, bufferedImage2);
    bufferedImage = bufferedImage2;
  }
    public void blur() {
    float value[] = { 0.0625f0.125f0.0625f0.125f0.25f0.125f,
        0.0625f0.125f0.0625f };
    Kernel kernel = new Kernel(33, value);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,null);
    convolve.filter(bufferedImage1, bufferedImage2);
    bufferedImage = bufferedImage2;
  }
    public void showEdge() {
    float value[] = { 1.0f0.0f, -1.0f1.0f0.0f, -1.0f1.0f0.0f,
        -1.0f };
  Kernel kernel = new Kernel(33, value);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,null);
    convolve.filter(bufferedImage1, bufferedImage2);
    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:

If you select sharpen button, the image will get sharp. If you select blur button, the image will get blurred. On clicking Show Edge button, only the edge of the image will be shown and reset button displays the original image.

Download Source Code

 

                         

» View all related tutorials
Related Tags: c ide class methods method get icon height vi width change using int id ai create js define show paint

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.