commit 31473a49b5318b54e8c9486c41358e04b9655af0 Author: Andreas Wurst <1720808@stud.hs-mannheim.de> Date: Thu Jun 5 10:59:04 2025 +0200 test diff --git a/HanoiPanel.java b/HanoiPanel.java new file mode 100644 index 0000000..c6b7ff5 --- /dev/null +++ b/HanoiPanel.java @@ -0,0 +1,152 @@ +import javax.swing.*; +import java.awt.*; +import java.util.Stack; + +/** + * Hauptklasse zum Starten der Türme von Hanoi mit grafischer Darstellung. + */ +public class HanoiVisualizer { + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + HanoiFrame frame = new HanoiFrame(4); // 4 Scheiben + frame.setVisible(true); + }); + } +} + +/** + * JFrame zur Darstellung der Türme und Animation der Bewegungen. + */ +class HanoiFrame extends JFrame { + + private final HanoiPanel panel; + + /** + * Konstruktor für das Hauptfenster. + * + * @param numDisks Anzahl der Scheiben + */ + public HanoiFrame(int numDisks) { + setTitle("Türme von Hanoi - Visualisierung"); + setSize(800, 400); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + + panel = new HanoiPanel(numDisks); + add(panel); + new Thread(panel::solve).start(); + } +} + +/** + * JPanel, das die drei Türme und die Scheiben zeichnet. + */ +class HanoiPanel extends JPanel { + + private final Stack[] towers; + private final int numDisks; + + /** + * Konstruktor für das Zeichenpanel. + * + * @param numDisks Anzahl der Scheiben + */ + public HanoiPanel(int numDisks) { + this.numDisks = numDisks; + towers = new Stack[3]; + for (int i = 0; i < 3; i++) { + towers[i] = new Stack<>(); + } + for (int i = numDisks; i >= 1; i--) { + towers[0].push(i); + } + } + + /** + * Startet den rekursiven Hanoi-Algorithmus. + */ + public void solve() { + try { + hanoi(numDisks, 0, 2, 1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + /** + * Führt den rekursiven Hanoi-Algorithmus mit 3 Türmen aus. + * + * @param n Anzahl der Scheiben + * @param from Quell-Turm + * @param to Ziel-Turm + * @param aux Hilfs-Turm + */ + private void hanoi(int n, int from, int to, int aux) throws InterruptedException { + if (n == 1) { + moveDisk(from, to); + } else { + hanoi(n - 1, from, aux, to); + moveDisk(from, to); + hanoi(n - 1, aux, to, from); + } + } + + /** + * Bewegt eine Scheibe von einem Turm zum anderen. + * + * @param from Quell-Turm + * @param to Ziel-Turm + */ + private void moveDisk(int from, int to) throws InterruptedException { + int disk = towers[from].pop(); + towers[to].push(disk); + repaint(); + Thread.sleep(500); // Wartezeit für Animation + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + drawTowers(g); + } + + /** + * Zeichnet die drei Türme und alle Scheiben. + * + * @param g Grafik-Kontext + */ + private void drawTowers(Graphics g) { + int width = getWidth(); + int height = getHeight(); + + int towerWidth = 20; + int towerHeight = 200; + + int baseY = height - 50; + int[] xPositions = {width / 4, width / 2, 3 * width / 4}; + + // Türme zeichnen + g.setColor(Color.DARK_GRAY); + for (int x : xPositions) { + g.fillRect(x - towerWidth / 2, baseY - towerHeight, towerWidth, towerHeight); + } + + // Scheiben zeichnen + for (int i = 0; i < 3; i++) { + Stack tower = towers[i]; + for (int j = 0; j < tower.size(); j++) { + int disk = tower.get(j); + int diskWidth = 30 + disk * 20; + int diskHeight = 20; + int x = xPositions[i] - diskWidth / 2; + int y = baseY - (j + 1) * diskHeight; + + g.setColor(new Color(50 + disk * 40, 100, 200)); + g.fillRoundRect(x, y, diskWidth, diskHeight, 10, 10); + g.setColor(Color.BLACK); + g.drawRoundRect(x, y, diskWidth, diskHeight, 10, 10); + } + } + } +}