Importjava.awt.*;Importjava.awt.event.*;ImportJava.util.Random;Importjavax.swing.*;/** * * @author http://javaflex.iteye.com/ * */ Public classGraphicstestextendsJFrameImplementsActionListener { Public Static Final DoublePI = math.pi/180; JPanel Panel; JPanel Pnlctl; JButton button; JButton button2; Graphics2D G2; Publicgraphicstest (String string) {Super(string); } Public voidinit () {Panel=NewJPanel (); Pnlctl=NewJPanel (); Button=NewJButton ("Fractal Tree"); Button2=NewJButton ("Clear"); This. Add (Panel, borderlayout.center); Button.addactionlistener ( This); Button2.addactionlistener ( This); Pnlctl.add (button); Pnlctl.add (button2); This. Add (Pnlctl, Borderlayout.north); SetSize (800, 600); This. Setdefaultcloseoperation (Jframe.exit_on_close); This. setvisible (true); Dimension winsize=Toolkit.getdefaulttoolkit (). Getscreensize (); This. setlocation (Winsize.width- This. GetWidth ())/2, (Winsize.height- This. GetHeight ())/2); G2=(graphics2d) panel.getgraphics (); } Public Static voidMain (string[] args)throwsclassnotfoundexception, Instantiationexception, Illegalaccessexception, Unsupportedlookandfeele xception {Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ()); Graphicstest Testpanel=NewGraphicstest ("Fractal Tree: QQ: 328,247,676"); Testpanel.init (); } @Override Public voidactionperformed (ActionEvent e) {if("Fractal Tree". Equals (E.getactioncommand ())) {drawleaf (G2,+ random.nextint (100)); } Else if("Clear". Equals (E.getactioncommand ())) {panel.getgraphics (). Clearrect (0, 0, 800, 800); }} random Random=NewRandom (); Public voidDrawleaf (Graphics G,DoubleXDoubleYDoubleLDoublea) {//random=new random (); //you can draw the speed of the picture to understand its algorithm//try {//Thread.Sleep (1000); //} catch (Interruptedexception e) {// //TODO auto-generated Catch block//E.printstacktrace (); // } intRed = random.nextint (127); intGreen = Random.nextint (127); intBlue = Random.nextint (127); //Random ColorG.setcolor (NewColor (red, green, blue)); Doublex1, x2, x1l, x2l, X2R, x1r, y1, y2, y1l, y2l, Y2R, y1r; floatdeflection = 50-random.nextint (20);//angle of the trunk of the side stem floatintersection = Random.nextint (40)-20;//main deflection Angle floatDepth = 2 + random.nextint (2);//Limit Recursion Depth floatRatio = 3f;//Trunk Side dry length ratio (adjustable to make it more dense or sparse) floatRatio2 = 1.2f;//Superior backbone vs. backbone length ratio (adjustable to make it high or low) if(L >depth) {X2= x + L * MATH.COS (A *PI); Y2= y + L * Math.sin (A *PI); X2R= x2 + l/ratio * Math.Cos ((A + deflection) *PI); Y2R= y2 + l/ratio * Math.sin ((A + deflection) *PI); X2L= x2 + l/ratio * Math.Cos ((a-deflection) *PI); Y2L= y2 + l/ratio * Math.sin ((a-deflection) *PI); X1= x + l/ratio * MATH.COS (A *PI); Y1= y + l/ratio * Math.sin (A *PI); x1l= x1 + l/ratio * Math.Cos ((a-deflection) *PI); y1l= y1 + l/ratio * Math.sin ((a-deflection) *PI); X1R= x1 + l/ratio * Math.Cos ((A + deflection) *PI); Y1R= y1 + l/ratio * Math.sin ((A + deflection) *PI); G.drawline ((int) x, (int) Y, (int) x2, (int) y2); G.drawline ((int) x2, (int) Y2, (int) X2R, (int) y2r); G.drawline ((int) x2, (int) Y2, (int) x2l, (int) y2l); G.drawline ((int) x1, (int) Y1, (int) x1l, (int) y1l); G.drawline ((int) x1, (int) Y1, (int) X1R, (int) y1r); Drawleaf (g, X2, y2, L/Ratio2, A +intersection); Drawleaf (g, X2R, y2r, L/ratio, A +deflection); Drawleaf (g, x2l, y2l, L/ratio, A-deflection); Drawleaf (g, x1l, y1l, L/ratio, A-deflection); Drawleaf (g, x1r, y1r, L/ratio, A +deflection); } }}
Java Fractal tree