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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. System.out.println("Punkte :" +x_temp + " " + y_temp + " " + turtle.getX()+ " " + turtle.getY());
  67. pts.add(temp);
  68. break;
  69. case '+':
  70. turtle.setTheta(turtle.getTheta() + DELTA);
  71. break;
  72. case '-':
  73. turtle.setTheta(turtle.getTheta() - DELTA);
  74. break;
  75. case '[':
  76. ts.push(new Turtle(turtle)); // Uncomment this (and comment previous line to avoid using clone)
  77. break;
  78. case ']':
  79. turtle = ts.pop();
  80. break;
  81. default:
  82. System.err.println("character " + ch + " not in grammar");
  83. }
  84. }
  85. }
  86. /*
  87. public void draw()
  88. {
  89. Graphics2D g3 = (Graphics2D) this.getGraphics();
  90. for (float[] pt : pts)
  91. {
  92. g3.draw(new Line2D.Float(pt[0], pt[1], pt[2], pt[3]));
  93. }
  94. g3.dispose();
  95. }
  96. */
  97. public ArrayList<float[]> getPoints()
  98. {
  99. return pts;
  100. }
  101. public int getPositionx()
  102. {
  103. return positionx;
  104. }
  105. public int getPositiony()
  106. {
  107. return positiony;
  108. }
  109. public void setPositionx(int x)
  110. {
  111. positionx = x;
  112. }
  113. public void setPositiony(int y)
  114. {
  115. positiony = y;
  116. }
  117. public void setflaeche(int flaeche)
  118. {
  119. }
  120. public int getflaeche()
  121. {
  122. return flaeche;
  123. }
  124. public void setPosition()
  125. {
  126. }
  127. }