package tools; /* * GrassAnimation - A simple animated 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 */ import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; import java.util.Random; import javax.swing.JFrame; public class GrassAnimation extends JFrame { public static final int WIDTH=1024; public static final int HEIGHT=768; public GrassAnimation() { setSize(WIDTH,HEIGHT); Graphics2D g; show(); Random rnd = new Random(); createBufferStrategy(2); BufferStrategy strategy = getBufferStrategy(); int[] x = new int[WIDTH]; int[] y = new int[WIDTH]; for (int i = 0; i < WIDTH; i++) { x[i] = 5+i+rnd.nextInt(20)-10; y[i] = 90-rnd.nextInt(30); } int steps = 20; float[] elements = { .0f, .1f, .0f, .3f, .1f, .3f, .0f, .2f, .0f}; Kernel kernel = new Kernel(3, 3, elements); ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); BufferedImage scenery = new BufferedImage(WIDTH,100,BufferedImage.TYPE_INT_ARGB); BufferedImage[] target = new BufferedImage[steps]; for (int a = 0; a < steps; a++) { target[a] = new BufferedImage(WIDTH,100,BufferedImage.TYPE_INT_ARGB); g = scenery.createGraphics(); g.setColor(new Color(0x003300)); g.fillRect(0,0,WIDTH,100); Color c1 = new Color(0x039201); Color c2 = new Color(0x003B00); g.setColor(c1); g.fillRect(0,88,WIDTH,10); int displacement = (int)Math.round(5*Math.sin(a*Math.PI*2/steps)); for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < 3; j++) { int x0 = 5+i; int y0 = 90-2*j; g.setPaint(new GradientPaint(x0,y0,c1,x[i]+displacement,y[i],c2)); g.drawLine(x0,y0,x[i]+displacement,y[i]); } } cop.filter(scenery,target[a]); } int a = 0; while(true) { long lastFrame=System.currentTimeMillis(); while (System.currentTimeMillis()-lastFrame < 100) Thread.yield(); g = (Graphics2D)strategy.getDrawGraphics(); g.setColor(new Color(0x003300)); g.fillRect(0,0,WIDTH,HEIGHT); g.drawImage(target[a],0,200,null); a++; if (a >= steps) a =0; strategy.show(); if (!isVisible()) { System.exit(0); } } } public static void main(String[] args) throws Exception { new GrassAnimation(); } }