// JavaProject FibonacciSpiral
// FibonacciSpiral.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
class Canvas extends JPanel {
private static final long serialVersionUID = 0L;
int n = 0; // Rekursionstiefe
Graphics2D g2d;
Rectangle2D.Float rectangle = new Rectangle2D.Float(1f, 1f, 1016f, 1016f);
BasicStroke stroke = new BasicStroke(4.0f);
private Turtle turtle; //
Turtle-Grafik
public Canvas() {
setBounds(3, 3, 256, 256);
setBackground(Color.WHITE);
}
private void defineRendering(Graphics2D g) {
RenderingHints rh;
rh = new RenderingHints (
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
g.setRenderingHints(rh);
}
private int fib(int n) { //
Rekursion
int result = 0;
if (n < 2) {
if (n == 0) result = 1;
if (n == 1) result = 1;
}
else result = (fib(n-1) + fib(n-2));
return result;
}
private void drawFibonacciSpiral(int n) {
turtle = new Turtle(this, 360, 680, -30);
drawArc(n, 0);
}
private void drawArc(int n, int m) {
if (n == -1) return;
turtle.goInACircleToTheLeft(10*fib(m), 90);
drawArc(n-1, m+1);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g.create();
g2d.scale(0.25, 0.25);
defineRendering(g2d);
g2d.setPaint(Color.GRAY);
g2d.setStroke(stroke);
g2d.draw(rectangle);
g2d.setPaint(Color.RED);
drawFibonacciSpiral(n);
g2d.dispose();
}
}
class CFrame extends JFrame {
private static final long serialVersionUID = 0L;
Image icon;
Canvas canvas;
BContainer container;
public CFrame() {
setTitle("Fibonacci-Spirale");
icon = Toolkit.getDefaultToolkit().getImage("dh.png");
setIconImage(icon);
setSize(349, 301);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
canvas = new Canvas();
add(canvas);
container = new BContainer(canvas);
add(container);
setVisible(true);
}
}
public class FibonacciSpiral {
public static void main(String[] args) {
new CFrame();
}
}
// Turtle.java
import java.awt.geom.*;
public class Turtle {
private double x, y, angle;
private Canvas canvas;
public Turtle(Canvas canvas, double x, double y, double angle) {
this.canvas = canvas;
this.x = x;
this.y = y;
this.angle = angle;
}
public void setPosition(double x, double y) {
this.x = x;
this.y = y;
}
public void turn(double d) {
angle += d;
}
public void goForward(double step) {
double xold = x;
double yold = y;
x += step * Math.cos(Math.toRadians(angle));
y += step * (-1)*Math.sin(Math.toRadians(angle));
Line2D.Double line = new Line2D.Double(xold, yold, x, y);
canvas.g2d.draw(line);
}
private double getXd(double angle, double r) {
double v = Math.tan(Math.toRadians(angle));
return r/Math.sqrt(1/(v*v) + 1);
}
private double getYd(double angle, double r) {
double v = Math.tan(Math.toRadians(angle));
return r/Math.sqrt(v*v + 1);
}
private void goInACircle(double r, double arcAngle) {
double xold = x;
double yold = y;
double signXd = Math.signum(Math.sin(Math.toRadians(angle)));
double signYd = Math.signum(Math.cos(Math.toRadians(angle)));
int xr = (int) Math.round(xold - signXd*getXd(angle, r) - r);
int yr = (int) Math.round(yold - signYd*getYd(angle, r) - r);
int s = (int) Math.round(2*r);
int w_start = (int) (Math.round(angle-90) );
int w_arc = (int) Math.round(arcAngle);
canvas.g2d.drawArc(xr, yr, s, s, w_start, w_arc);
int w_end = w_start + w_arc;
double xnew = xr + r + r*Math.cos(Math.toRadians(-w_end));
double ynew = yr + r + r*Math.sin(Math.toRadians(-w_end));
x = (int) (Math.round(xnew));
y = (int) (Math.round(ynew));
angle = (arcAngle < 0) ? w_end - 90 : w_end + 90 ;
}
public void goInACircleToTheLeft(double r, double arcAngle) {
if (arcAngle < 0) arcAngle = -arcAngle;
if (r < 0) r =-r;
goInACircle(r, arcAngle);
}
public void goInACircleToTheRight(double r, double arcAngle) {
if (arcAngle < 0) arcAngle = -arcAngle;
if (r < 0) r =-r;
angle = angle + 180;
goInACircle(r, -arcAngle);
}
}
// BContainer.java
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;
class Button extends JButton {
private static final long serialVersionUID = 0L;
public Button(String txt) {
setText(txt);
}
}
public class BContainer extends Container implements ActionListener {
private static final long serialVersionUID = 0L;
Canvas canvas;
Button b0 = new Button("n = 0");
Button b1 = new Button("n = 1");
Button b2 = new Button("n = 2");
Button b3 = new Button("n = 3");
Button b4 = new Button("n = 4");
Button b5 = new Button("n = 5");
Button b6 = new Button("n = 6");
Button b7 = new Button("n = 7");
Button b8 = new Button("n = 8");
Button b9 = new Button("n = 9");
Button[] b = {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9};
public BContainer(Canvas canvas) {
this.canvas = canvas;
setBounds(262, 3, 68, 257);
setLayout(new GridLayout(10, 1, 2, 2));
for (int i=0; i<10; i++) {
b[i].addActionListener(this);
add(b[i]);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b0) canvas.n = 0;
if (e.getSource() == b1) canvas.n = 1;
if (e.getSource() == b2) canvas.n = 2;
if (e.getSource() == b3) canvas.n = 3;
if (e.getSource() == b4) canvas.n = 4;
if (e.getSource() == b5) canvas.n = 5;
if (e.getSource() == b6) canvas.n = 6;
if (e.getSource() == b7) canvas.n = 7;
if (e.getSource() == b8) canvas.n = 8;
if (e.getSource() == b9) canvas.n = 9;
canvas.repaint();
}
}
Download FibonacciSpiral