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.util.Random;

/*
 * Grass - 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 Grass 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;
		
	public static void main(String[] x) {
		new Grass();
	}
	
	
	public Grass() {
		setBounds(0,0,WIDTH,HEIGHT);
		setVisible(true);
		addWindowListener(
				new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						System.exit(0);
					}
				}
				);
	}
	
	
	public void paint(Graphics gg) {
		Random rnd = new Random();
		Graphics2D g = (Graphics2D)gg;
		
		
		BufferedImage scenery = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB); 
		BufferedImage target = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
		g = scenery.createGraphics();
		
		g.setColor(new Color(0x003300));
		g.fillRect(0,0,WIDTH,HEIGHT);
		
		Color grassBottomColor = new Color(0x039201);
		Color grassTopColor = new Color(0x003B00);
		
    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);
		for (int i = 0; i < 800; i++) {
			for (int j = 0; j < 3; j++) {
				int x0 = 5+i;
				int y0 = 200-2*j;
				int x1 = x0+rnd.nextInt(10); 
				int y1 = y0-rnd.nextInt(30);
				g.setPaint(new GradientPaint(x0,y0,grassBottomColor,x1,y1,grassTopColor));
				g.drawLine(x0,y0,x1,y1);
			}
		}
    cop.filter(scenery,target);
    g = (Graphics2D)gg;
    g.drawImage(target,0,0,null);
    g.drawImage(scenery,0,200,null);
	}
}

