public class FractalTreeTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFractalFrame frame = new JFractalFrame(); frame.setVisible(true); } }); } }
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(); } }
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); } }
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; } }
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()); } }
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(); } } } }
Source: https://habr.com/ru/post/268405/
All Articles