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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.text.CharacterIterator;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  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. protected float[] boundingBoxes;
  35. ArrayList<Float> tempx;
  36. ArrayList<Float> tempy;
  37. //Position();
  38. TurtleStack ts;
  39. public PflanzeImpl(int xstart, int ystart, int generation, int startLength)
  40. {
  41. this.positionx = xstart;
  42. this.positiony = ystart;
  43. this.generations = generation;
  44. this.startLength = startLength;
  45. pts = new ArrayList<float[]>();
  46. ts = new TurtleStack();
  47. boundingBoxes=new float[4]; //minx, miny, maxx, maxy
  48. tempx = new ArrayList<>();
  49. tempy = new ArrayList<>();
  50. }
  51. public void translateRules()
  52. {
  53. float x_temp, y_temp;
  54. Turtle turtle = new Turtle(positionx, positiony, (float) ((Math.PI) / 2));
  55. CharacterIterator it = grammar.getIterator();
  56. for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next())
  57. {
  58. switch (ch)
  59. {
  60. case 'F':
  61. x_temp = turtle.getX();
  62. y_temp = turtle.getY();
  63. turtle.setX((float) (x_temp + drawLength * Math.cos((turtle.getTheta()))));
  64. turtle.setY((float) (y_temp - drawLength * Math.sin((turtle.getTheta()))));
  65. float[] temp =
  66. {
  67. x_temp, y_temp, turtle.getX(), turtle.getY()
  68. };
  69. tempx.add(x_temp);
  70. tempx.add(turtle.getX());
  71. tempy.add(y_temp);
  72. tempy.add(turtle.getY());
  73. pts.add(temp);
  74. break;
  75. case '+':
  76. turtle.setTheta(turtle.getTheta() + DELTA);
  77. break;
  78. case '-':
  79. turtle.setTheta(turtle.getTheta() - DELTA);
  80. break;
  81. case '[':
  82. ts.push(new Turtle(turtle)); // Uncomment this (and comment previous line to avoid using clone)
  83. break;
  84. case ']':
  85. turtle = ts.pop();
  86. break;
  87. default:
  88. System.err.println("character " + ch + " not in grammar");
  89. }
  90. }
  91. setBoundingBoxes();
  92. }
  93. void setBoundingBoxes()
  94. {
  95. boundingBoxes[0] = Collections.min(tempx);
  96. boundingBoxes[1] = Collections.min(tempy);
  97. boundingBoxes[2] = Collections.max(tempx);
  98. boundingBoxes[3] = Collections.max(tempy);
  99. }
  100. public float [] getBoundingBoxes()
  101. {
  102. return boundingBoxes;
  103. }
  104. @Override
  105. public ArrayList<float[]> getPoints()
  106. {
  107. return pts;
  108. }
  109. @Override
  110. public int getPositionx()
  111. {
  112. return positionx;
  113. }
  114. @Override
  115. public int getPositiony()
  116. {
  117. return positiony;
  118. }
  119. @Override
  120. public void setPositionx(int x)
  121. {
  122. positionx = x;
  123. }
  124. @Override
  125. public void setPositiony(int y)
  126. {
  127. positiony = y;
  128. }
  129. @Override
  130. public int getflaeche()
  131. {
  132. return flaeche;
  133. }
  134. @Override
  135. public void setAlgorithm()
  136. {
  137. pts.removeAll(pts);
  138. translateRules();
  139. }
  140. public void setPosition()
  141. {
  142. }
  143. @Override
  144. public void setflaeche(int flaeche)
  145. {
  146. }
  147. }