`
java.jianan
  • 浏览: 19390 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

数据结构

阅读更多
package com.struc;

import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;

public class Hanoi extends JFrame {

private static final long serialVersionUID = 1L;

static int n = Integer.parseInt(JOptionPane
.showInputDialog("输入盘子数目 int:"));

static int time = Integer.parseInt(JOptionPane
.showInputDialog("输入移动盘子的频率 int:"));
static int num = 0;

static char[] array = new char[n];

MyPanel panel = new MyPanel();
static String string = "";
static JTextArea text = new JTextArea();

public Hanoi() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(text), BorderLayout.EAST);
getContentPane().add(panel, BorderLayout.CENTER);
}

public static void main(String[] args) {
for (int i = 0; i < n; i++) {
array[i] = 'A';
}
Hanoi hanoi = new Hanoi();
hanoi.setTitle("HanoiZiJiZuo Tower");
hanoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hanoi.setSize(1450, 400);
hanoi.setVisible(true);
hanoi.moveDisks(n, 'A', 'C', 'B');
}

public class MyPanel extends JPanel {

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(50, 310, 1100, 310);
g.fillRect(200, 10, 10, 300);
g.drawString("A", 203, 340);
g.fillRect(500, 10, 10, 300);
g.drawString("B", 503, 340);
g.fillRect(800, 10, 10, 300);
g.drawString("C", 803, 340);
draw(g);
repaint();
}

void draw(Graphics g) {
g.setColor(Color.YELLOW);
num = 0;
for (int i = n; i > 0; i--) {

if (array[(int) (i - 1.0)] == 'A') {
g.fillOval(205 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);

g.setColor(Color.BLACK);
g.drawString("" + i, 203, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}

}
num = 0;
for (int i = n; i > 0; i--) {

if (array[(int) (i - 1)] == 'B') {
g.fillOval(505 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);
g.setColor(Color.BLACK);
g.drawString("" + i, 503, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}

}
num = 0;

for (int i = n; i > 0; i--) {
if (array[(int) (i - 1)] == 'C') {
g.fillOval(805 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);
g.setColor(Color.BLACK);
g.drawString("" + i, 803, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}
}
g.setColor(Color.BLACK);
}

}

public void moveDisks(int a, char fromTower, char toTower, char auxTower) {
// Stopping condition
if (a == 1) {
System.out.println("Move disk " + a + " from " + fromTower + " to "
+ toTower);
string += "Move disk " + a + " from " + fromTower + " to "
+ toTower + "         .\n";
text.setText(string);
array[a - 1] = toTower;
pause();
} else {
moveDisks(a - 1, fromTower, auxTower, toTower);
System.out.println("Move disk " + a + " from " + fromTower + " to "
+ toTower);
string += "Move disk " + a + " from " + fromTower + " to "
+ toTower + "         .\n";
text.setText(string);
array[a - 1] = toTower;
pause();
moveDisks(a - 1, auxTower, toTower, fromTower);

}

}

public static void pause() {
try {
Thread.sleep(time * 100);// 可以修改此值加快盘子移动的速度
} catch (InterruptedException e) {
}
}
}
分享到:
评论

相关推荐

    PTA-数据结构与算法题目集.zip

    PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 ...

    上海交大数据结构课件 上海交大数据结构课件

    数据结构是计算机科学中的核心课程之一,主要研究如何在计算机中高效地组织和存储数据,以便于进行各种操作。上海交通大学的数据结构课件是学习这一主题的重要资源,它涵盖了广泛的知识点,帮助学生深入理解数据结构...

    数据结构1800题(含详解答案)

    ### 数据结构基础知识点详解 #### 一、基础知识概念解析 **1. 算法的复杂性** - **题目**: 算法的计算量的大小称为计算的()。 - **答案**: B. 复杂性 - **解析**: 算法的复杂性通常用来衡量算法执行效率的...

    数据结构1800试题.pdf

    数据结构是计算机科学中的核心课程,它探讨如何高效地组织和管理数据,以便进行快速查找、插入和删除等操作。这份“数据结构1800试题”提供了丰富的练习题目,涵盖了数据结构的主要概念和算法,适合学生进行复习和...

    西安理工大学863数据结构真题 -西安理工大学863数据结构真题需要的滴滴我,都是我去年备考时的真题资料,还有复试资料哦~

    西安理工大学863数据结构真题集锦 作为一名 IT 行业大师,我将根据提供的文件信息,生成相关的知识点,以下是详细的输出结果: 一、数据结构概述 数据结构是计算机科学中的一门基础学科,旨在研究如何存储和组织...

Global site tag (gtag.js) - Google Analytics