Test
parent
31473a49b5
commit
8806561ebf
|
|
@ -3,20 +3,25 @@ import java.awt.*;
|
|||
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 {
|
||||
|
||||
/**
|
||||
* Hauptmethode – startet die grafische Anwendung.
|
||||
* @param args Kommandozeilenargumente
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int n = Integer.parseInt(JOptionPane.showInputDialog("Anzahl der Scheiben:"));
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
HanoiFrame frame = new HanoiFrame(4); // 4 Scheiben
|
||||
HanoiFrame frame = new HanoiFrame(n);
|
||||
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 {
|
||||
|
||||
|
|
@ -24,12 +29,11 @@ class HanoiFrame extends JFrame {
|
|||
|
||||
/**
|
||||
* Konstruktor für das Hauptfenster.
|
||||
*
|
||||
* @param numDisks Anzahl der Scheiben
|
||||
*/
|
||||
public HanoiFrame(int numDisks) {
|
||||
setTitle("Türme von Hanoi - Visualisierung");
|
||||
setSize(800, 400);
|
||||
setTitle("Türme von Hanoi - n Scheiben");
|
||||
setSize(900, 500);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
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 {
|
||||
|
||||
|
|
@ -48,8 +52,7 @@ class HanoiPanel extends JPanel {
|
|||
private final int numDisks;
|
||||
|
||||
/**
|
||||
* Konstruktor für das Zeichenpanel.
|
||||
*
|
||||
* Konstruktor – initialisiert die Türme.
|
||||
* @param numDisks Anzahl der Scheiben
|
||||
*/
|
||||
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() {
|
||||
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 from Quell-Turm
|
||||
* @param to Ziel-Turm
|
||||
* @param aux Hilfs-Turm
|
||||
* @param n Anzahl der zu bewegenden Scheiben
|
||||
* @param from Index des Quellturms (0–2)
|
||||
* @param to Index des Zielturms (0–2)
|
||||
* @param aux Index des Hilfsturms (0–2)
|
||||
*/
|
||||
private void hanoi(int n, int from, int to, int aux) throws InterruptedException {
|
||||
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 to Ziel-Turm
|
||||
* @param to Ziel-Turm
|
||||
* @throws InterruptedException für Animation
|
||||
*/
|
||||
private void moveDisk(int from, int to) throws InterruptedException {
|
||||
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) {
|
||||
int width = getWidth();
|
||||
int height = getHeight();
|
||||
|
||||
int towerWidth = 20;
|
||||
int towerHeight = 200;
|
||||
int towerHeight = 250;
|
||||
|
||||
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
|
||||
g.setColor(Color.DARK_GRAY);
|
||||
|
|
@ -142,7 +150,7 @@ class HanoiPanel extends JPanel {
|
|||
int x = xPositions[i] - diskWidth / 2;
|
||||
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.setColor(Color.BLACK);
|
||||
g.drawRoundRect(x, y, diskWidth, diskHeight, 10, 10);
|
||||
Loading…
Reference in New Issue