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 } 67 }); 68 ButtonPanel.add(Clear); 69 70 Solve = new JButton("Solve"); 71 Solve.setBounds(350,5,150,40); 72 Solve.setFont(font); 73 Solve.setEnabled(true); 74 Solve.setMargin(new Insets(0,0,0,0)); 75 Solve.addActionListener(new ActionListener() { 76 public void actionPerformed(ActionEvent e) { 77 if(hanoi == null) {return ;} 78 hanoi.Solve(); 79 Set.setEnabled(false); 80 Solve.setEnabled(false); 81 } 82 }); 83 ButtonPanel.add(Solve); 84 85 OpPanel = new JPanel(null); 86 OpPanel.setBackground(Color.BLACK); 87 OpPanel.setPreferredSize(new Dimension(800,50)); 88 this.add(OpPanel,BorderLayout.SOUTH); 89 90 prev = new JButton("<<"); 91 prev.setBounds(100,5,150,40); 92 prev.setFont(font); 93 prev.setEnabled(true); 94 prev.setMargin(new Insets(0,0,0,0)); 95 prev.addActionListener(new ActionListener() { 96 public void actionPerformed(ActionEvent e) { 97 if(nowPage - 1 < 0){return ;} 98 G.setColor(Color.WHITE); 99 G.fillRect(0, 0, 800, 600); 100 nowPage --; 101 Steps.setText("Step " + String.valueOf(nowPage)); 102 Draw(nowPage,G); 103 } 104 }); 105 OpPanel.add(prev); 106 107 next = new JButton(">>"); 108 next.setBounds(550,5,150,40); 109 next.setFont(font); 110 next.setEnabled(true); 111 next.setMargin(new Insets(0,0,0,0)); 112 next.addActionListener(new ActionListener() { 113 public void actionPerformed(ActionEvent e) { 114 if(nowPage + 1 > hanoi.maxStep){return ;} 115 G.setColor(Color.WHITE); 116 G.fillRect(0, 0, 800, 600); 117 nowPage ++; 118 Steps.setText("Step " + String.valueOf(nowPage)); 119 Draw(nowPage,G); 120 } 121 }); 122 OpPanel.add(next); 123 124 Steps = new JLabel("Step Null",JLabel.CENTER); 125 Steps.setBounds(300,5,150,40); 126 Steps.setFont(new Font("Times New Roman",Font.BOLD,25)); 127 Steps.setForeground(Color.WHITE); 128 OpPanel.add(Steps); 129 130 MainPanel = new JPanel(); 131 MainPanel.setBackground(Color.WHITE); 132 this.add(MainPanel); 133 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 134 this.setSize(800, 600); 135 Dimension winSize = Toolkit.getDefaultToolkit().getScreenSize(); 136 this.setLocation((winSize.width - this.getWidth()) / 2,(winSize.height - this.getHeight()) / 2); 137 this.setResizable(false); 138 this.setVisible(true); 139 G = (Graphics2D)MainPanel.getGraphics(); 140 } 141 142 public void Draw(int x,Graphics G) { 143 G.setColor(Color.DARK_GRAY); 144 G.fillRect(50, 350, 200, 30); 145 G.fillRect(300, 350, 200, 30); 146 G.fillRect(550, 350, 200, 30); 147 G.setColor(Color.BLUE); 148 for(int i = 0;i < hanoi.ans[x][0].size();i ++) { 149 int s = hanoi.ans[x][0].get(i); 150 G.drawRect(150 - 15 * s, 320 - 30 * i, 30 * s, 30); 151 } 152 for(int i = 0;i < hanoi.ans[x][1].size();i ++) { 153 int s = hanoi.ans[x][1].get(i); 154 G.drawRect(400 - 15 * s, 320 - 30 * i, 30 * s, 30); 155 } 156 for(int i = 0;i < hanoi.ans[x][2].size();i ++) { 157 int s = hanoi.ans[x][2].get(i); 158 G.drawRect(650 - 15 * s, 320 - 30 * i, 30 * s, 30); 159 } 160 } 161 162 public static void main(String[] args) { 163 new UI(); 164 } 165 166 }
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 }