Hallo hier mal ein kleine Programm das einen binären Baum oder einen Pythagoras Baum zeichnet nach ein paar bestimmten angaben, ich finde das einfach gammer geil wie der das zeichnet könnte ich mir stunden anschaun
Bild des gezeichneten Baumes:
BinBaumGui
Java
import java.awt.*;import javax.swing.*;public class BinBaumGui extends JFrame { BinBaumKlasse baum = new BinBaumKlasse(); BinBaumActionListener listener = new BinBaumActionListener(this); Panel draw = new Panel(); JPanel controlEast = new JPanel(); JPanel controlNorth = new JPanel(); JLabel lblBaumtiefe = new JLabel("Baumtiefe:"); JLabel lblAstlaenge = new JLabel("Astlaenge:"); JLabel lblWinkel = new JLabel("Winkel:"); JTextField txtBaumtiefe = new JTextField(); JTextField txtAstlaenge = new JTextField(); JTextField txtWinkel = new JTextField(); JButton btnErstelle = new JButton("Zeige Baum!"); BinBaumGui() { super("BinBaum"); } public void GuiErstellen() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new BorderLayout()); this.add(controlEast, BorderLayout.EAST); controlEast.add(controlNorth, BorderLayout.NORTH); controlNorth.setLayout(new BoxLayout(controlNorth, BoxLayout.Y_AXIS)); controlNorth.add(lblBaumtiefe); controlNorth.add(txtBaumtiefe); controlNorth.add(lblAstlaenge); controlNorth.add(txtAstlaenge); controlNorth.add(lblWinkel); controlNorth.add(txtWinkel); controlNorth.add(btnErstelle); controlNorth.setBackground(Color.LIGHT_GRAY); controlEast.setBackground(Color.LIGHT_GRAY); draw.setBackground(Color.WHITE); baum.setBackground(Color.WHITE); draw.add(baum); draw.setLayout(new BorderLayout()); draw.add(baum,BorderLayout.CENTER); btnErstelle.addActionListener(listener); this.add(draw); this.setSize(700, 700); this.setVisible(true); } public static void main(String args []) { BinBaumGui meineGui = new BinBaumGui(); meineGui.GuiErstellen(); }}
TurtleKlasse
Java
import java.awt.*;class Turtle{ protected double posX, posY; // Aktuelle Position der Turtle protected double winkel; // Aktueller Blickwinkel protected Color farbe=Color.black; // Standard-Zeichenfarbe protected Container c; // Der Zeichencontainer protected double homeX, homeY; // Home-Koordinaten protected boolean stiftUnten; protected int breiteX, hoeheY; protected Graphics g; public Turtle(Container cont) { winkel=0; stiftUnten = true; c=cont; // merken des Containers g=cont.getGraphics(); // und des GrafikObjektes breiteX = c.getBounds().width; hoeheY = c.getBounds().height; posX = breiteX / 2; posY = hoeheY; // positioniert die Turtle in die linke untere Ecke homeX = posX; homeY = posY; } public void homePosition() { posX=homeX; posY=homeY; winkel=0; } public void bewege(double laenge) { g.setColor(farbe); double neuX = posX + Math.cos(bogen(winkel))*laenge; double neuY; neuY = posY - Math.sin(bogen(winkel))*laenge; if (stiftUnten) g.drawLine((int) posX, (int) posY, (int) neuX, (int) neuY); posX = neuX; posY = neuY; } public void dreheLinks(double grad) { winkel = winkel + grad; // if (winkel > 360) winkel = winkel -360; } public void dreheRechts(double grad) { winkel = winkel - grad; // if (winkel<0) winkel = 360+winkel ; } public void back(int laenge){ dreheLinks(180); stiftHoch(); bewege(laenge); dreheRechts(180); stiftRunter(); } public void loesche() { homePosition(); int x=c.getBounds().width; int y=c.getBounds().height; g.clearRect(0, 0, x, y); } public void setFarbe(Color c) { farbe = c; } public void stiftHoch() { stiftUnten=false; } public void stiftRunter() { stiftUnten=true; } public void zeichneNormal() { g.setPaintMode(); } private double bogen(double winkel) { return winkel*Math.PI/180; }}
Die BinBaumKlasse
Java
import java.awt.*;public class BinBaumKlasse extends Panel { Turtle meineKroete; int winkel; int astLaenge; int baumTiefe = 4; double faktor = 0.8; int c = 120; int alpha = 23; int n = 0; BinBaumKlasse() { } public void paint(Graphics g){ meineKroete = new Turtle((Container)this); meineKroete.homePosition(); meineKroete.dreheLinks(90); zeichneBinBaum(astLaenge, baumTiefe); // das auskommentieren und das andere darunter wieder rein... //zeichnePytaBaum(c, alpha, n); } public void zeichneBinBaum(double laenge, int tiefe){ zeichneStamm(laenge); if(tiefe > 0) { meineKroete.dreheLinks(winkel); zeichneBinBaum(laenge * faktor, tiefe - 1); meineKroete.dreheRechts(winkel * 2); zeichneBinBaum(laenge * faktor, tiefe - 1); meineKroete.dreheLinks(winkel); } meineKroete.stiftHoch(); zeichneStamm(-laenge); meineKroete.stiftRunter(); } public void zeichnePytaBaum(double c, double alpha, int n) { double a; double b; double beta = 90 - alpha; double winkel = alpha * Math.PI / 180; if(n <= baumTiefe) { a = Math.sin(winkel) * c; b = Math.sqrt(- Math.pow(a, 2) + Math.pow(c, 2)); meineKroete.dreheLinks(90); meineKroete.bewege(c); meineKroete.dreheRechts(90); meineKroete.bewege(c); meineKroete.dreheRechts(90); meineKroete.dreheLinks(alpha); meineKroete.bewege(b); meineKroete.dreheLinks(90); zeichnePytaBaum(b, alpha, n + 1); meineKroete.dreheRechts(90); meineKroete.bewege(- b); meineKroete.dreheRechts(alpha); meineKroete.bewege(c); meineKroete.dreheLinks(180 - beta); meineKroete.bewege(a); meineKroete.bewege(- a); meineKroete.dreheRechts(90); zeichnePytaBaum(a, alpha, n + 1); meineKroete.dreheRechts(180 - beta); meineKroete.bewege(c); meineKroete.dreheRechts(180); } } public void zeichneStamm(double laenge){ meineKroete.bewege(laenge); } public void setAstLaenge(int AstLaenge) { astLaenge = AstLaenge; } public void setBaumTiefe(int BaumTiefe) { baumTiefe = BaumTiefe; } public void setFaktor(double Faktor) { faktor = Faktor; } public void setWinkel(int Winkel) { winkel = Winkel; }}
Und der kleine ActionListener
Java
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class BinBaumActionListener implements ActionListener { BinBaumGui Fenster; BinBaumActionListener(BinBaumGui dasFenster) { Fenster = dasFenster; } public void actionPerformed(ActionEvent e) { Fenster.baum.setAstLaenge(Integer.parseInt(Fenster.txtAstlaenge.getText())); Fenster.baum.setBaumTiefe(Integer.parseInt(Fenster.txtBaumtiefe.getText())); Fenster.baum.setWinkel(Integer.parseInt(Fenster.txtWinkel.getText())); Fenster.baum.repaint(); }}
Hier hab ich das ganze noch mal als jar Archiv zum direkten ausführen entweder mit doppelklick oder einfach in der konsolemit:
http://rapidshare.com/files/202904411/binbaum.jar
Achja wenn ihr die eine zeile änder also binbaum dingens auskommentiert und pytabaum rein dann kommt ein Pythagoras - Baum
Mfg ladies