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.geom.GeneralPath; import java.awt.image.BufferedImage; import java.util.Random; /* * FlowerTutorial - A random cartoon flower 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 FlowerTutorial 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 FlowerTutorial(); } public FlowerTutorial() { 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; /* * Construct the petal */ GeneralPath petal = new GeneralPath(); int flowerSize = 64; int upsize=flowerSize/5; int aperture = rnd.nextInt(5)+5; int cp = rnd.nextInt(5)+3; int[] fx = new int[2*cp+3]; int[] fy = new int[2*cp+3]; petal.moveTo(flowerSize/2-aperture,upsize+flowerSize); fx[2*cp+2] = flowerSize/2-aperture; fy[2*cp+2] = upsize+flowerSize; for (int i = cp; i >= 0; i--) { fx[2*i] = rnd.nextInt(flowerSize/2); fy[2*i] = upsize+flowerSize/cp*i; fx[2*i+1] = rnd.nextInt(flowerSize/2); fy[2*i+1] = upsize+fy[2*i]+rnd.nextInt(flowerSize/cp*(i+1)-flowerSize/cp*i); petal.quadTo(fx[2*i+1],fy[2*i+1],fx[2*i],fy[2*i]); } // Connecting quad petal.quadTo(flowerSize/2,upsize+rnd.nextInt(upsize)-2*upsize,flowerSize-fx[0],upsize); // Compute reflected points for (int i = 0; i <= cp; i++) { petal.quadTo(flowerSize-fx[2*i+1],fy[2*i+1],flowerSize-fx[2*i+2],fy[2*i+2]); } petal.closePath(); /* * Now construct the flower */ int kernelSize = 20; int petals = 10; Color flowerColor = new Color(0x00AAFF); Color kernelColor = new Color(0x00FFFF); BufferedImage flowerImage = new BufferedImage(2*flowerSize,2*flowerSize,BufferedImage.TYPE_INT_ARGB); Graphics2D gFlower = flowerImage.createGraphics(); double angle = 2*Math.PI/petals; gFlower.translate(flowerSize/2,0); for (int i = 0; i <= petals; i++) { gFlower.setPaint(new GradientPaint(0,0,flowerColor,flowerSize,flowerSize,new Color(0))); gFlower.fill(petal); gFlower.setColor(new Color(0)); gFlower.draw(petal); gFlower.rotate(-angle,flowerSize/2,flowerSize); } if (kernelSize != 0) { gFlower = flowerImage.createGraphics(); gFlower.setColor(new Color(0)); gFlower.fillOval(flowerSize-kernelSize,flowerSize-kernelSize,2*kernelSize,2*kernelSize); gFlower.setColor(kernelColor); gFlower.fillOval(flowerSize-kernelSize+1,flowerSize-kernelSize+1,2*kernelSize-2,2*kernelSize-2); } /* * Now draw it several times */ for (int i = 0; i < 5; i++) { g.drawImage(flowerImage,(flowerSize*2+50)*i,100,null); } } }