package tools; import java.awt.Color; import java.awt.Frame; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; import java.io.File; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; /* * FilterDemo - A simple grass-effect * Copyright (c) 2007 Alexander Hristov. See http://www.ahristov.com/tutorial for more tutorials * * This application is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * version 2.1 of the License. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */ public class FilterDemo extends Frame { public static final double MIN_X = -1024/2.0; public static final double MAX_X = 1024/2.0; public static final int WIDTH = 1024; public static final int HEIGHT = 768; BufferedImage sourceImage; public static void main(String[] x) throws IOException { new FilterDemo(); } public FilterDemo() throws IOException { sourceImage = ImageIO.read(new File("--REPLACE-WITH-YOUR-FILE.jpg")); setBounds(0,0,WIDTH,HEIGHT); setVisible(true); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public void paint(Graphics gg) { Graphics2D g = (Graphics2D)gg; BufferedImage filteredImage = new BufferedImage( sourceImage.getWidth(), sourceImage.getHeight(),BufferedImage.TYPE_INT_ARGB); float[] elements = { 0,-1,0, -1,8,-1, 0,-1,0 }; // Normalize the filter float w=0; for (int i=0; i < elements.length; i++) w+=elements[i]; if (w!=0) for (int i=0; i < elements.length; i++) elements[i]/=w; Kernel kernel = new Kernel(3, 3, elements); ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); cop.filter(sourceImage,filteredImage); g.drawImage(sourceImage,100,100,null); g.drawImage(filteredImage,100+filteredImage.getWidth()+10,100,null); } }