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(2, 2));
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, 0, 0, this);
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.0f, 9.0f, -1.0f, -1.0f, -1.0f,
-1.0f };
Kernel kernel = new Kernel(3, 3, value);
ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,null);
convolve.filter(bufferedImage1, bufferedImage2);
bufferedImage = bufferedImage2;
}
public void blur() {
float value[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f,
0.0625f, 0.125f, 0.0625f };
Kernel kernel = new Kernel(3, 3, value);
ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,null);
convolve.filter(bufferedImage1, bufferedImage2);
bufferedImage = bufferedImage2;
}
public void showEdge() {
float value[] = { 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f,
-1.0f };
Kernel kernel = new Kernel(3, 3, 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(0, 0, bufferedImage.getWidth(this), bufferedImage.
getHeight(this));
g2d.drawImage(image, 0, 0, this);
bufferedImage = bufferedImage1;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(bufferedImage, 0, 0, this);
}
}
|
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

|