📜 ⬆️ ⬇️

Fractal bush from beginner for beginners

As I mentioned, I'm a beginner (and the most modest in the universe) Java programmer. Sometimes the soul asks for something like that, so it’s straight that it first turns around and then coagulates again. I want beauty. And the beauty of drawing, we are not yet trained. But trained to draw sticks and circles.

And with the battle cry “Beauty is in simplicity!”, We draw from sticks. And what can we draw beautiful and simple, so that colleagues gasp in delight? And here comes to the aid of a beautiful word - Fractal.

First, the definition: "A fractal is a structure of parts ... bla-bla-bla ... self-similarity ... bla-bla-bla ... beautiful ... bla-bla-bla ...".
')


And armed with this comprehensive knowledge, let's draw a fractal tree, or rather a bush.

To begin with, we will create a shell from which we will run everything:

FractalTreeTest class
public class FractalTreeTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFractalFrame frame = new JFractalFrame(); frame.setVisible(true); } }); } } 


Now we will create a class responsible for displaying the window and launching the animation:

JFractalFrame class
 class JFractalFrame extends JFrame{ boolean startAnimation=false; JPanel paintPanel; public JFractalFrame() { setTitle("FractalTree"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); paintPanel = new PaintPanel(); paintPanel.setBackground(Color.DARK_GRAY); add(paintPanel); requestFocus();//    .   ,    //       ,    addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e){ if(e.getKeyCode()>0 && !startAnimation){ pack(); startAnimation=true; startAnimation (); } } }); paintPanel.repaint(); pack(); } public void startAnimation(){ Runnable galaxyRun = new FractalRunnable((PaintPanel) paintPanel); Thread t = new Thread(galaxyRun); t.start(); } } 


A class with a panel on which the whole action is drawn. It contains the recursive function fractal, which renders the new branches:

PaintPanel class
 class PaintPanel extends JPanel{ List<Branch> listBranch = new CopyOnWriteArrayList<>(); double angle=0; public void setAngle(double angle) { this.angle = angle; } // ,         ,     //    ,   ,  ,    public void fractal(int startLength, Point2D startPoint, double alpha, int step){ if(alpha<0) alpha=360; double radian =(alpha/(180/Math.PI)); Point2D endPoint1 = new Point2D(); Point2D endPoint2 = new Point2D(); endPoint1.setX((float) (startPoint.getX()-startLength*Math.cos(radian))); endPoint1.setY((float) (startPoint.getY()-startLength*Math.sin(radian))); addBranch(new Branch(startPoint, endPoint1, startLength)); endPoint2.setX((float) (startPoint.getX()-startLength*Math.cos(radian))); endPoint2.setY((float) (startPoint.getY()-startLength*Math.sin(radian))); addBranch(new Branch(startPoint, endPoint2, startLength)); if(step>0){ step--; startLength-=4; //   //        .  //  . fractal(startLength, endPoint1, alpha-(20+angle),step); //angle    fractal(startLength, endPoint2, alpha+(20-angle), step); } } public void addBranch(Branch b){ listBranch.add(b); } public void paintComponent(Graphics g){ super.paintComponent(g); //   ,     60,  90,   10  fractal(60, new Point2D(320, 480), 90, 10); Random randomX = new Random(); Graphics2D g2d = (Graphics2D)g; for(Branch b: listBranch){ //   ,   «»   g2d.setColor(new Color(randomX.nextInt(255),randomX.nextInt(255),randomX.nextInt(255))); //   ,    , // ,        /* if(b.length>30) g2d.setColor(Color.ORANGE.darker()); else g2d.setColor(Color.GREEN); */ g2d.draw(b.getShape()); } //   ,      listBranch.clear(); } //    public Dimension getPreferredSize() { // TODO Auto-generated method stub return new Dimension(640,480); } } 


2D point class:

Class Point2D
 class Point2D{ private float x, y; //     public Point2D(float x, float y) { this.x=x; this.y = y; } public Point2D(){ } public void setX(float x) { this.x = x; } public void setY(float y) { this.y = y; } public float getX() { return x; } public float getY() { return y; } } 


The class responsible for drawing branches:

Branch class
 class Branch{ Point2D begin; Point2D end; int length; //     public Branch(Point2D begin, Point2D end, int length) { this.begin=begin; this.end=end; this.length=length; } //      public Line2D getShape(){ return new Line2D.Double(begin.getX(), begin.getY(), end.getX(), end.getY()); } } 


And finally, the Runnable class, in which our animation will spin:

FractalRunnable class
 class FractalRunnable implements Runnable{ PaintPanel paintPanel; public FractalRunnable(PaintPanel paintPanel) { // TODO Auto-generated constructor stub this.paintPanel=paintPanel; } public void run() { double count=0; boolean leftDir = true; while(true){ //»  ,   …» . //    count if(count>8 && a<count){ leftDir=false; } if(count<-8 && count>-9){ leftDir=true; } if(leftDir) count+=0.01; else count-=0.01; paintPanel.setAngle(a); paintPanel.repaint(); try { Thread.sleep(5); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 


We launch, click on any button on the keyboard, and rejoice.

So let me leave.

Source: https://habr.com/ru/post/268405/


All Articles