You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PflanzeImpl.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package garten.model;
  7. /**
  8. *
  9. * @author Jan
  10. */
  11. import java.awt.BasicStroke;
  12. import java.text.CharacterIterator;
  13. import java.util.ArrayList;
  14. import javax.swing.JComponent;
  15. import lsystem.Grammar;
  16. import lsystem.collection.TurtleStack;
  17. import lsystem.turtle.Turtle;
  18. public class PflanzeImpl extends JComponent implements Pflanze
  19. {
  20. ArrayList<float[]> pts;
  21. protected float DELTA; // 18 degrees
  22. protected Grammar grammar;
  23. final protected String axiom = "F";
  24. protected int generations;
  25. //protected String rule;
  26. protected float startLength;
  27. protected float drawLength;
  28. protected float theta;
  29. protected float xpos;
  30. protected float ypos;
  31. protected int positionx;
  32. protected int positiony;
  33. protected int flaeche;
  34. //Position();
  35. TurtleStack ts;
  36. //public Line2D.Float Gerade;
  37. private BasicStroke pinsel;
  38. public PflanzeImpl(int Xstart, int Ystart, int Generation)
  39. {
  40. positionx = Xstart;
  41. positiony = Ystart;
  42. generations = Generation;
  43. pts = new ArrayList<float[]>();
  44. ts = new TurtleStack(); //PApplet
  45. pinsel = new BasicStroke(2f);
  46. //Gerade = new Line2D.Float();
  47. }
  48. public void translateRules()
  49. {
  50. float x_temp, y_temp;
  51. Turtle turtle = new Turtle(positionx, positiony, (float) ((Math.PI)/2));
  52. CharacterIterator it = grammar.getIterator();
  53. for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next())
  54. {
  55. switch (ch)
  56. {
  57. case 'F':
  58. x_temp = turtle.getX();
  59. y_temp = turtle.getY();
  60. turtle.setX((float) (x_temp + drawLength * Math.cos((turtle.getTheta()))));
  61. turtle.setY((float) (y_temp - drawLength * Math.sin((turtle.getTheta()))));
  62. float[] temp =
  63. {
  64. x_temp, y_temp, turtle.getX(), turtle.getY()
  65. };
  66. pts.add(temp);
  67. break;
  68. case '+':
  69. turtle.setTheta(turtle.getTheta() + DELTA);
  70. break;
  71. case '-':
  72. turtle.setTheta(turtle.getTheta() - DELTA);
  73. break;
  74. case '[':
  75. ts.push(new Turtle(turtle)); // Uncomment this (and comment previous line to avoid using clone)
  76. break;
  77. case ']':
  78. turtle = ts.pop();
  79. break;
  80. default:
  81. System.err.println("character " + ch + " not in grammar");
  82. }
  83. }
  84. }
  85. /*
  86. public void draw()
  87. {
  88. Graphics2D g3 = (Graphics2D) this.getGraphics();
  89. for (float[] pt : pts)
  90. {
  91. g3.draw(new Line2D.Float(pt[0], pt[1], pt[2], pt[3]));
  92. }
  93. g3.dispose();
  94. }
  95. */
  96. public ArrayList<float[]> getPoints()
  97. {
  98. return pts;
  99. }
  100. public int getPositionx()
  101. {
  102. return positionx;
  103. }
  104. public int getPositiony()
  105. {
  106. return positiony;
  107. }
  108. public void setPositionx(int x)
  109. {
  110. positionx = x;
  111. }
  112. public void setPositiony(int y)
  113. {
  114. positiony = y;
  115. }
  116. public int getflaeche()
  117. {
  118. return flaeche;
  119. }
  120. public void setPosition()
  121. {
  122. }
  123. }