package tools; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.GeneralPath; import java.awt.image.BufferedImage; import java.util.Random; /* * FlowerTutorial - A random cartoon rock/treetop generator * 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 RockTutorial extends Frame { public static final double MIN_X = -1024/2.0; public static final double MAX_X = 1024/2.0; public static void main(String[] x) { new RockTutorial(); } public RockTutorial() { setBounds(0,0,1024,768); setVisible(true); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public void paint(Graphics gg) { Graphics2D g = (Graphics2D)gg; GeneralPath rockPath = new GeneralPath(); // Starting color of the rock. Subsequent layers // become darker and darker float rockHue = 0; float rockSaturation = 14/255f; float rockBrightness = 100/255f; // Number of vertices int numPoints = 20; // Width of the rock in pixels int rockSize = 200; // Intensity of the deformation. The closer to 1 - the more jagged is the // resulting rock double intensity = 0.3; BufferedImage rockImage = new BufferedImage(rockSize,rockSize,BufferedImage.TYPE_INT_ARGB); // Create tha basic polygon Graphics2D gRock = rockImage.createGraphics(); for (int point = 0; point < numPoints; point++) { // Calcuate a random perturbation along the axis of the nth point double pertx = Math.random()*(rockSize/2.5*intensity)-rockSize/2.5*(intensity/2); double perty = Math.random()*(rockSize/2.5*intensity)-rockSize/2.5*(intensity/2); double angle = Math.PI/10*point; int x = rockSize/2+(int)(rockSize/2.5*Math.cos(angle) + pertx*Math.cos(angle)); int y = rockSize/2-(int)(rockSize/2.5*Math.sin(angle) + perty*Math.sin(angle)); if (point == 0) rockPath.moveTo((int)x,y); else rockPath.lineTo((int)x,y); } rockPath.closePath(); gRock.setColor(Color.getHSBColor(rockHue,rockSaturation,rockBrightness)); gRock.fill(rockPath); gRock.setClip(rockPath); // Number of layers int numLayers = 3; Random rnd = new Random(); for (int layer = 1; layer < numLayers; layer++) { int tx = (int)(rockSize/2.5/numLayers*layer); int ty = (int)(rockSize/2.5/numLayers*layer); double sx = 0.05/(rnd.nextDouble()+0.1); double sy = 0.05/(rnd.nextDouble()+0.1); gRock.translate(tx,ty); gRock.shear(sx,sy); gRock.setColor(Color.getHSBColor(rockHue,rockSaturation,rockBrightness*(1-layer/(float)(numLayers+10)))); gRock.fill(rockPath); gRock.setColor(Color.black); gRock.draw(rockPath); } gRock = rockImage.createGraphics(); gRock.setStroke(new BasicStroke(2)); gRock.setColor(Color.black); gRock.draw(rockPath); // Draw the rock for (int i = 0; i < 5; i++) { g.drawImage(rockImage,(rockSize+50)*i,100,null); } } }