NetBeans/Garten/src/garten/model/PflanzeImpl.java
2019-06-23 14:54:19 +02:00

170 lines
3.6 KiB
Java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package garten.model;
/**
*
* @author Jan
*/
import java.text.CharacterIterator;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JComponent;
import lsystem.Grammar;
import lsystem.collection.TurtleStack;
import lsystem.turtle.Turtle;
public class PflanzeImpl extends JComponent implements Pflanze
{
ArrayList<float[]> pts;
protected float DELTA; // 18 degrees
protected Grammar grammar;
final protected String axiom = "F";
protected int generations;
//protected String rule;
protected float startLength;
protected float drawLength;
protected float theta;
protected float xpos;
protected float ypos;
protected int positionx;
protected int positiony;
protected int flaeche;
protected float[] boundingBoxes;
ArrayList<Float> tempx;
ArrayList<Float> tempy;
//Position();
TurtleStack ts;
public PflanzeImpl(int xstart, int ystart, int generation, int startLength)
{
this.positionx = xstart;
this.positiony = ystart;
this.generations = generation;
this.startLength = startLength;
pts = new ArrayList<float[]>();
ts = new TurtleStack();
boundingBoxes=new float[4]; //minx, miny, maxx, maxy
tempx = new ArrayList<>();
tempy = new ArrayList<>();
}
public void translateRules()
{
float x_temp, y_temp;
Turtle turtle = new Turtle(positionx, positiony, (float) ((Math.PI) / 2));
CharacterIterator it = grammar.getIterator();
for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next())
{
switch (ch)
{
case 'F':
x_temp = turtle.getX();
y_temp = turtle.getY();
turtle.setX((float) (x_temp + drawLength * Math.cos((turtle.getTheta()))));
turtle.setY((float) (y_temp - drawLength * Math.sin((turtle.getTheta()))));
float[] temp =
{
x_temp, y_temp, turtle.getX(), turtle.getY()
};
tempx.add(x_temp);
tempx.add(turtle.getX());
tempy.add(y_temp);
tempy.add(turtle.getY());
pts.add(temp);
break;
case '+':
turtle.setTheta(turtle.getTheta() + DELTA);
break;
case '-':
turtle.setTheta(turtle.getTheta() - DELTA);
break;
case '[':
ts.push(new Turtle(turtle)); // Uncomment this (and comment previous line to avoid using clone)
break;
case ']':
turtle = ts.pop();
break;
default:
System.err.println("character " + ch + " not in grammar");
}
}
setBoundingBoxes();
}
void setBoundingBoxes()
{
boundingBoxes[0] = Collections.min(tempx);
boundingBoxes[1] = Collections.min(tempy);
boundingBoxes[2] = Collections.max(tempx);
boundingBoxes[3] = Collections.max(tempy);
}
public float [] getBoundingBoxes()
{
return boundingBoxes;
}
@Override
public ArrayList<float[]> getPoints()
{
return pts;
}
@Override
public int getPositionx()
{
return positionx;
}
@Override
public int getPositiony()
{
return positiony;
}
@Override
public void setPositionx(int x)
{
positionx = x;
}
@Override
public void setPositiony(int y)
{
positiony = y;
}
@Override
public int getflaeche()
{
return flaeche;
}
@Override
public void setAlgorithm()
{
pts.removeAll(pts);
translateRules();
}
public void setPosition()
{
}
@Override
public void setflaeche(int flaeche)
{
}
}