JAVA——汉诺塔

UI类:管里各类控件,事件响应,并在画板上绘制相应的图形。

repaint()并用不明白,所以改用draw一个白色的矩形来实现清屏。

求大佬指点一下repaint()的用法。

  1 import java.awt.event.*;
  2 import javax.swing.*;
  3 import java.awt.*;
  4 import java.util.*;
  5 import java.math.*;
  6 
  7 public class UI extends JFrame{
  8     
  9     static JPanel MainPanel,ButtonPanel,OpPanel;
 10     static JLabel Steps;
 11     static JButton Set,Solve,Clear;
 12     static JComboBox Diff;
 13     static Font font = new Font("Times New Roman",Font.BOLD,20);
 14     static int[][] mmp = new int[3][20];
 15     static int[] Len = new int[3];
 16     static int nowPage;
 17     static Graphics2D G;
 18     static Hanoi hanoi;
 19     static JButton prev,next;
 20     public UI(){
 21         this.setTitle("Tower of Hanoi");
 22         
 23         ButtonPanel = new JPanel(null);
 24         ButtonPanel.setBackground(Color.BLACK);
 25         ButtonPanel.setPreferredSize(new Dimension(800,50));
 26         this.add(ButtonPanel,BorderLayout.NORTH);
 27         
 28         Set = new JButton("Set");
 29         Set.setBounds(100,5,150,40);
 30         Set.setFont(font);
 31         Set.setEnabled(true);
 32         Set.setMargin(new Insets(0,0,0,0));
 33         Set.addActionListener(new ActionListener() {
 34             public void actionPerformed(ActionEvent e) {
 35                 hanoi = new Hanoi(Diff.getSelectedIndex() + 1);
 36                 G.setColor(Color.WHITE);
 37                 G.fillRect(0, 0, 800, 600);
 38                 Diff.setEnabled(false);
 39                 nowPage = 0;
 40                 Steps.setText("Step  " + String.valueOf(nowPage));
 41                 Draw(0,G);
 42             }
 43         });
 44         ButtonPanel.add(Set);
 45         
 46         Diff = new JComboBox();
 47         for(int i = 1;i <= 6;i ++){Diff.addItem(i);}
 48         Diff.setBounds(250,5,50,40);
 49         Diff.setFont(font);
 50         ButtonPanel.add(Diff);
 51 
 52         Clear = new JButton("Clear");
 53         Clear.setBounds(550,5,150,40);
 54         Clear.setFont(font);
 55         Clear.setEnabled(true);
 56         Clear.setMargin(new Insets(0,0,0,0));
 57         Clear.addActionListener(new ActionListener() {
 58             public void actionPerformed(ActionEvent e) {
 59                 Diff.setEnabled(true);
 60                 hanoi = null;
 61                 Set.setEnabled(true);
 62                 Solve.setEnabled(true);
 63                 G.setColor(Color.WHITE);
 64                 G.fillRect(0, 0, 800, 600);
 65                 Steps.setText("Step  Null");
 66                 next.setEnabled(false);
 67                 prev.setEnabled(false);
 68             }
 69         });
 70         ButtonPanel.add(Clear);
 71         
 72         Solve = new JButton("Solve");
 73         Solve.setBounds(350,5,150,40);
 74         Solve.setFont(font);
 75         Solve.setEnabled(true);
 76         Solve.setMargin(new Insets(0,0,0,0));
 77         Solve.addActionListener(new ActionListener() {
 78             public void actionPerformed(ActionEvent e) {
 79                 if(hanoi == null) {return ;}
 80                 hanoi.Solve();
 81                 Set.setEnabled(false);
 82                 Solve.setEnabled(false);
 83                 next.setEnabled(true);
 84                 prev.setEnabled(true);
 85             }
 86         });
 87         ButtonPanel.add(Solve);
 88         
 89         OpPanel = new JPanel(null);
 90         OpPanel.setBackground(Color.BLACK);
 91         OpPanel.setPreferredSize(new Dimension(800,50));
 92         this.add(OpPanel,BorderLayout.SOUTH);
 93         
 94         prev = new JButton("<<");
 95         prev.setBounds(100,5,150,40);
 96         prev.setFont(font);
 97         prev.setEnabled(false);
 98         prev.setMargin(new Insets(0,0,0,0));
 99         prev.addActionListener(new ActionListener() {
100             public void actionPerformed(ActionEvent e) {
101                 if(nowPage - 1 < 0){return ;}
102                 G.setColor(Color.WHITE);
103                 G.fillRect(0, 0, 800, 600);
104                 nowPage --;
105                 Steps.setText("Step  " + String.valueOf(nowPage));
106                 Draw(nowPage,G);
107             }
108         });
109         OpPanel.add(prev);
110         
111         next = new JButton(">>");
112         next.setBounds(550,5,150,40);
113         next.setFont(font);
114         next.setEnabled(false);
115         next.setMargin(new Insets(0,0,0,0));
116         next.addActionListener(new ActionListener() {
117             public void actionPerformed(ActionEvent e) {
118                 if(nowPage + 1 > hanoi.maxStep){return ;}
119                 G.setColor(Color.WHITE);
120                 G.fillRect(0, 0, 800, 600);
121                 nowPage ++;
122                 Steps.setText("Step  " + String.valueOf(nowPage));
123                 Draw(nowPage,G);
124             }
125         });
126         OpPanel.add(next);
127         
128         Steps = new JLabel("Step  Null",JLabel.CENTER);
129         Steps.setBounds(300,5,150,40);
130         Steps.setFont(new Font("Times New Roman",Font.BOLD,25));
131         Steps.setForeground(Color.WHITE);
132         OpPanel.add(Steps);
133         
134         MainPanel = new JPanel();
135         MainPanel.setBackground(Color.WHITE);
136         this.add(MainPanel);
137         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
138         this.setSize(800, 600);
139         Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize();   
140         this.setLocation((winSize.width - this.getWidth()) / 2,(winSize.height - this.getHeight()) / 2);
141         this.setResizable(false);
142         this.setVisible(true);
143         G = (Graphics2D)MainPanel.getGraphics();
144     }
145     
146     public void Draw(int x,Graphics G) {
147         G.setColor(Color.DARK_GRAY);
148         G.fillRect(50, 350, 200, 30);
149         G.fillRect(300, 350, 200, 30);
150         G.fillRect(550, 350, 200, 30);
151         G.setColor(Color.BLUE);
152         for(int i = 0;i < hanoi.ans[x][0].size();i ++) {
153             int s = hanoi.ans[x][0].get(i);
154             G.drawRect(150 - 15 * s, 320 - 30 * i, 30 * s, 30);
155         }
156         for(int i = 0;i < hanoi.ans[x][1].size();i ++) {
157             int s = hanoi.ans[x][1].get(i);
158             G.drawRect(400 - 15 * s, 320 - 30 * i, 30 * s, 30);
159         }
160         for(int i = 0;i < hanoi.ans[x][2].size();i ++) {
161             int s = hanoi.ans[x][2].get(i);
162             G.drawRect(650 - 15 * s, 320 - 30 * i, 30 * s, 30);
163         }
164     }
165     
166     public static void main(String[] args) {
167         new UI();
168     }
169 
170 }

Hanoi类:主要存放递归算法,以及能够将解答的步骤保存至相应的vector内,在绘制时,只需要知道当前步数,即可直接找到当前状态。

 1 import java.util.*;
 2 
 3 public class Hanoi {
 4     
 5     static Vector<Integer> V[] = new Vector[3];
 6     static Vector<Integer> ans[][] = new Vector[100][3];
 7     static int L,nowStep,maxStep;
 8     
 9     static void Move(int n,int a,int c) {
10         int t = V[a].lastElement();
11         V[a].removeElementAt(V[a].size()-1);
12         V[c].add(t);
13         for(int j = 0;j < 3;j ++) {
14             for(int k = 0;k < V[j].size();k ++) {
15                 ans[nowStep][j].add(V[j].get(k));
16             }
17         }
18         nowStep ++;
19     }
20     
21     static void dfs(int n,int a,int b,int c) {
22         if(n == 0) {return ;}
23         dfs(n - 1,a,c,b);
24         Move(n,a,c);
25         dfs(n - 1,b,a,c);
26     }
27     
28     public void Solve() {
29         dfs(L,0,1,2);
30         
31 //        for(int i = 0;i <= maxStep;i ++) {
32 //            System.out.println("------------------");
33 //            System.out.println(i);
34 //            for(int j = 0;j < 3;j ++) {
35 //                for(int k = 0;k < ans[i][j].size();k ++) {
36 //                    System.out.print(ans[i][j].get(k) + " ");
37 //                }
38 //                System.out.println();
39 //            }
40 //            System.out.println("------------------");
41 //        }
42         
43     }
44     
45     public Hanoi(int x) {
46         this.L= x;
47         maxStep = (int)Math.pow(2,L) - 1;
48         nowStep = 0;
49         for(int i = 0;i < 3;i ++) {V[i] = new Vector<Integer>();V[i].clear();}
50         for(int i = 0;i < 90;i ++) {
51             ans[i][0] = new Vector<Integer>();ans[i][0].clear();
52             ans[i][1] = new Vector<Integer>();ans[i][1].clear();
53             ans[i][2] = new Vector<Integer>();ans[i][2].clear();
54         }
55         for(int i = L;i >= 1;i --){
56             V[0].add(i);
57             ans[nowStep][0].add(i);
58         }
59         nowStep ++;
60     }
61     
62 }
原文地址:https://www.cnblogs.com/love-fromAtoZ/p/11588141.html