package jconvolveopdemo1;
//Download by http://www.codesc.net
import java.awt.*;
import javax.swing.JPanel;
import java.awt.image.*;
import java.awt.color.*;
public class DrawPane extends JPanel {
private BorderLayout borderLayout1 = new BorderLayout();
Image image;
BufferedImage bimage;
BufferedImage bimageSrc;
BufferedImage bimageDest;
Graphics2D g2D;
public DrawPane() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.setLayout(borderLayout1);
loadImage();
createBufferedImage();
bimage = bimageSrc;
this.setSize(image.getWidth(this),image.getHeight(this));
}
public void loadImage(){
//image = Toolkit.getDefaultToolkit().createImage("images/image.jpg");
// System.out.println(ClassLoader.getSystemResource("images/image.jpg"));
image = this.getToolkit().createImage(ClassLoader.getSystemResource("images/image.jpg"));
MediaTracker mt = new MediaTracker(this);
mt.addImage(image,0);
try{
mt.waitForAll();
}catch(Exception err){
System.err.println("Could not load the image.");
}
if (image.getWidth(this) == -1){
System.err.println("Could not get the image.");
System.exit(1);
}
}
public void createBufferedImage(){
bimageSrc = new BufferedImage(image.getWidth(this),image.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
g2D = bimageSrc.createGraphics();
g2D.drawImage(image,0,0,this);
bimageDest = new BufferedImage(image.getWidth(this),image.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
}
public void sharpImage(){
float[] data = {
-1.0f,-1.0f,-1.0f,
-1.0f,10.0f,-1.0f,
-1.0f,-1.0f,-1.0f
};
Kernel kernel = new Kernel(3,3,data);
ConvolveOp co = new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
co.filter(bimageSrc,bimageDest);
bimage = bimageDest;
}
public void blurImage(){
float[] data = {
0.0625f,0.125f,0.0625f,
0.125f,0.25f,0.125f,
0.0625f,0.125f,0.0625f
};
Kernel kernel = new Kernel(3,3,data);
ConvolveOp co = new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
co.filter(bimageSrc,bimageDest);
bimage = bimageDest;
}
public void reset(){
g2D.setColor(Color.black);
g2D.clearRect(0,0,image.getWidth(this),image.getHeight(this));
g2D.drawImage(image,0,0,this);
bimage = bimageSrc;
}
public void update(Graphics g){
g.clearRect(0,0,this.getWidth(),this.getHeight());
paintComponent(g);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(bimage,0,0,this);
}
}