Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.util.Stack;
  4. import javax.swing.*;
  5.  
  6. // キモい再帰的なツリーを描くプログラム
  7. public class drawtree extends JPanel
  8. {
  9. private static final int DEGREE = 12;
  10. private static final int BRUNCH_LENGTH = 40;
  11. private static final int DRAW_LEVEL = 8;
  12.  
  13. public static void main(String[] args)
  14. {
  15. drawtree dt = new drawtree();
  16.  
  17. JFrame jf = new JFrame();
  18. jf.setSize(200, 200);
  19. jf.setVisible(true);
  20. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. jf.setLayout(new BorderLayout());
  22. jf.add(dt, BorderLayout.CENTER);
  23. }
  24.  
  25. public drawtree()
  26. {
  27. this.currentDegree = 90;
  28. this.positionStack = new Stack<Point2D>();
  29. positionStack.push(new Point2D.Double(100, 150));
  30. }
  31.  
  32. private int currentDegree;
  33. private Stack<Point2D> positionStack;
  34.  
  35. public void drawtree(Graphics g, int n)
  36. {
  37. if(n == 0)
  38. {
  39. // 何もしない
  40. }
  41. else
  42. {
  43. left();
  44. forward(g, n);
  45. drawtree(g, n-1);
  46. back(n);
  47. right();
  48.  
  49. right();
  50. forward(g, n);
  51. drawtree(g, n-1);
  52. back(n);
  53. left();
  54. }
  55. }
  56.  
  57. public void left()
  58. {
  59. this.currentDegree -= DEGREE;
  60. }
  61.  
  62. public void right()
  63. {
  64. this.currentDegree += DEGREE;
  65. }
  66.  
  67. public void forward(Graphics g, int n)
  68. {
  69. int brunchLength = BRUNCH_LENGTH - ((DRAW_LEVEL - n) * 2);
  70.  
  71. Graphics2D g2d = (Graphics2D)g;
  72. Point2D currentPosition = (Point2D)positionStack.peek();
  73.  
  74. // debug
  75. //System.out.println("CURRENT:" + currentPosition.getX() + "," + currentPosition.getY());
  76.  
  77. Point2D moveTo = null;
  78. if(currentDegree <= 90)
  79. {
  80. double moveX = Math.sin((90 - (currentDegree - DEGREE)) / 180.0) * brunchLength;
  81. double moveY = Math.sin((currentDegree - DEGREE) / 180.0) * brunchLength;
  82. moveTo = new Point2D.Double(currentPosition.getX() - moveX, currentPosition.getY() - moveY);
  83. }
  84. else
  85. {
  86. int targetDegree = 180 - currentDegree;
  87. double moveX = Math.sin((90 - (targetDegree - DEGREE)) / 180.0) * brunchLength;
  88. double moveY = Math.sin((targetDegree - DEGREE) / 180.0) * brunchLength;
  89. moveTo = new Point2D.Double(currentPosition.getX() + moveX, currentPosition.getY() - moveY);
  90. }
  91.  
  92. // debug
  93. //System.out.println("MOVE:" + moveTo.getX() + "," + moveTo.getY());
  94.  
  95. g.setColor(Color.black);
  96. Line2D.Double line2d = new Line2D.Double(currentPosition.getX(), currentPosition.getY(), moveTo.getX(), moveTo.getY());
  97. g2d.draw(line2d);
  98.  
  99. positionStack.push(moveTo);
  100. }
  101.  
  102. public void back(int n)
  103. {
  104. positionStack.pop();
  105. }
  106.  
  107. public void paint(Graphics g)
  108. {
  109. drawtree(g, DRAW_LEVEL);
  110. }
  111. }
Add Comment
Please, Sign In to add comment