main
Andreas Wurst 2025-06-05 11:17:15 +02:00
parent 31473a49b5
commit 8806561ebf
1 changed files with 30 additions and 22 deletions

View File

@ -3,20 +3,25 @@ import java.awt.*;
import java.util.Stack; import java.util.Stack;
/** /**
* Hauptklasse zum Starten der Türme von Hanoi mit grafischer Darstellung. * Startpunkt der Anwendung zur Visualisierung der Türme von Hanoi.
*/ */
public class HanoiVisualizer { public class HanoiVisualizer {
/**
* Hauptmethode startet die grafische Anwendung.
* @param args Kommandozeilenargumente
*/
public static void main(String[] args) { public static void main(String[] args) {
int n = Integer.parseInt(JOptionPane.showInputDialog("Anzahl der Scheiben:"));
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
HanoiFrame frame = new HanoiFrame(4); // 4 Scheiben HanoiFrame frame = new HanoiFrame(n);
frame.setVisible(true); frame.setVisible(true);
}); });
} }
} }
/** /**
* JFrame zur Darstellung der Türme und Animation der Bewegungen. * JFrame-Fenster zur Darstellung der Türme von Hanoi.
*/ */
class HanoiFrame extends JFrame { class HanoiFrame extends JFrame {
@ -24,12 +29,11 @@ class HanoiFrame extends JFrame {
/** /**
* Konstruktor für das Hauptfenster. * Konstruktor für das Hauptfenster.
*
* @param numDisks Anzahl der Scheiben * @param numDisks Anzahl der Scheiben
*/ */
public HanoiFrame(int numDisks) { public HanoiFrame(int numDisks) {
setTitle("Türme von Hanoi - Visualisierung"); setTitle("Türme von Hanoi - n Scheiben");
setSize(800, 400); setSize(900, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); setLocationRelativeTo(null);
@ -40,7 +44,7 @@ class HanoiFrame extends JFrame {
} }
/** /**
* JPanel, das die drei Türme und die Scheiben zeichnet. * Panel zur grafischen Darstellung der Türme und Animation.
*/ */
class HanoiPanel extends JPanel { class HanoiPanel extends JPanel {
@ -48,8 +52,7 @@ class HanoiPanel extends JPanel {
private final int numDisks; private final int numDisks;
/** /**
* Konstruktor für das Zeichenpanel. * Konstruktor initialisiert die Türme.
*
* @param numDisks Anzahl der Scheiben * @param numDisks Anzahl der Scheiben
*/ */
public HanoiPanel(int numDisks) { public HanoiPanel(int numDisks) {
@ -64,7 +67,7 @@ class HanoiPanel extends JPanel {
} }
/** /**
* Startet den rekursiven Hanoi-Algorithmus. * Startet die Lösung des Hanoi-Problems.
*/ */
public void solve() { public void solve() {
try { try {
@ -75,12 +78,12 @@ class HanoiPanel extends JPanel {
} }
/** /**
* Führt den rekursiven Hanoi-Algorithmus mit 3 Türmen aus. * Rekursive Methode zur Lösung des Hanoi-Problems.
* *
* @param n Anzahl der Scheiben * @param n Anzahl der zu bewegenden Scheiben
* @param from Quell-Turm * @param from Index des Quellturms (02)
* @param to Ziel-Turm * @param to Index des Zielturms (02)
* @param aux Hilfs-Turm * @param aux Index des Hilfsturms (02)
*/ */
private void hanoi(int n, int from, int to, int aux) throws InterruptedException { private void hanoi(int n, int from, int to, int aux) throws InterruptedException {
if (n == 1) { if (n == 1) {
@ -93,10 +96,11 @@ class HanoiPanel extends JPanel {
} }
/** /**
* Bewegt eine Scheibe von einem Turm zum anderen. * Verschiebt eine einzelne Scheibe von einem Turm zu einem anderen.
* *
* @param from Quell-Turm * @param from Quell-Turm
* @param to Ziel-Turm * @param to Ziel-Turm
* @throws InterruptedException für Animation
*/ */
private void moveDisk(int from, int to) throws InterruptedException { private void moveDisk(int from, int to) throws InterruptedException {
int disk = towers[from].pop(); int disk = towers[from].pop();
@ -112,19 +116,23 @@ class HanoiPanel extends JPanel {
} }
/** /**
* Zeichnet die drei Türme und alle Scheiben. * Zeichnet die drei Türme und ihre jeweiligen Scheiben.
* *
* @param g Grafik-Kontext * @param g Grafikobjekt
*/ */
private void drawTowers(Graphics g) { private void drawTowers(Graphics g) {
int width = getWidth(); int width = getWidth();
int height = getHeight(); int height = getHeight();
int towerWidth = 20; int towerWidth = 20;
int towerHeight = 200; int towerHeight = 250;
int baseY = height - 50; int baseY = height - 50;
int[] xPositions = {width / 4, width / 2, 3 * width / 4}; int[] xPositions = {
width / 4,
width / 2,
3 * width / 4
};
// Türme zeichnen // Türme zeichnen
g.setColor(Color.DARK_GRAY); g.setColor(Color.DARK_GRAY);
@ -142,7 +150,7 @@ class HanoiPanel extends JPanel {
int x = xPositions[i] - diskWidth / 2; int x = xPositions[i] - diskWidth / 2;
int y = baseY - (j + 1) * diskHeight; int y = baseY - (j + 1) * diskHeight;
g.setColor(new Color(50 + disk * 40, 100, 200)); g.setColor(Color.getHSBColor(disk * 0.1f, 0.6f, 0.9f));
g.fillRoundRect(x, y, diskWidth, diskHeight, 10, 10); g.fillRoundRect(x, y, diskWidth, diskHeight, 10, 10);
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawRoundRect(x, y, diskWidth, diskHeight, 10, 10); g.drawRoundRect(x, y, diskWidth, diskHeight, 10, 10);