📄 hanoi.java
字号:
int dDiskNum = (diskNum[dPillar]>0)? diskNum[dPillar] - 1 : 0 ; //数组坐标化
int dDiskL = Pillar[dPillar][dDiskNum]; //目标柱子上最上面的盘子长度
if(diskNum[dPillar] == 0 || dDiskL > diskL) //如果目标柱子上没盘或长于移动的盘..
{
diskLabel[arrowHead][diskNum[arrowHead]-1].setVisible(false); //隐藏原图
diskLabel[dPillar][diskNum[dPillar]].setIcon(diskLabel[arrowHead][diskNum[arrowHead]-1].getIcon()); //移动盘图
diskLabel[dPillar][diskNum[dPillar]].setHorizontalAlignment(JLabel.CENTER); //居中
diskLabel[dPillar][diskNum[dPillar]].setVisible(true); //显示
diskNum[arrowHead] -- ; //柱子盘数更改
diskNum[dPillar] ++ ;
Pillar[dPillar][diskNum[dPillar]-1] = diskL ;
Pillar[arrowHead][diskNum[arrowHead]] = 0 ;
arrowLabel[dPillar].setIcon(arrowLabel[arrowHead].getIcon()); //移动箭头
arrowLabel[dPillar].setHorizontalAlignment(JLabel.CENTER);
arrowLabel[arrowHead].setVisible(false);
arrowLabel[dPillar].setVisible(true);
arrowHead = dPillar ;
totalSteps ++ ;
stepsLabel.setText(" 步骤:"+totalSteps);
}
}
//移动箭头
public void moveArrow(int LR)
{
if(threadTime!=null && !threadTime.isAlive())
threadTime.start();
int dPillar = (arrowHead+LR+3)%3 ; //目标柱子
arrowLabel[dPillar].setIcon(arrowLabel[arrowHead].getIcon());
arrowLabel[dPillar].setHorizontalAlignment(JLabel.CENTER);
arrowLabel[arrowHead].setVisible(false);
arrowLabel[dPillar].setVisible(true);
if(diskNum[arrowHead] != 0)
{
int n = Pillar[arrowHead][diskNum[arrowHead]-1]; //移动前箭头指向的盘子长度
diskLabel[arrowHead][diskNum[arrowHead]-1].setIcon(diskIcon[n-1]); //更改移动前盘子图片
}
if(diskNum[dPillar] != 0)
{
int n = Pillar[dPillar][diskNum[dPillar]-1] ; //目标柱子最上的盘长度
diskLabel[dPillar][diskNum[dPillar]-1].setIcon(diskSelIcon[n-1]);
}
arrowHead = dPillar ;
}
public int getDiskTotalNum() { return diskTotalNum ; }
public long getDelay() { return delay ; }
public String getTime() { return times ; }
public void setDelay(long n) { delay = n ; }
public void setDiskTotalNum(int n) { diskTotalNum = n ; }
public void setTime(int n) { times = String.valueOf((float)n/10); }
public void printTime() { timeLabel.setText("时间:"+times+" ");}
public void timeStart() { threadTime = new ThreadTime(this); threadTime.start();}
public void timeStop() { if(threadTime != null) { threadTime.stop() ; threadTime = null ;}}
public void autopStop() { if(threadp != null) { threadp.stop() ; threadp = null ; }}
public File getRecordFile() { return recordFile ;}
public String getRecord(int n) //n(1-8)行
{
String data = null ;
try
{
BufferedReader in = new BufferedReader(new FileReader(recordFile));
for(int i = 1 ; i <= n ; i ++)
{
data = (String)in.readLine();
}
in.close();
}catch (Exception e){}
return data ;
}
public void setRecord(String s,int n) //n(1-8)行
{
try
{
String data[] = new String[8] ;
BufferedReader in = new BufferedReader(new FileReader(recordFile));
for(int i = 0 ; i < 8 ; i ++)
{
data[i] = (String)in.readLine();
}
in.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(recordFile));
PrintWriter out = new PrintWriter(bw,true);
data[n-1] = s ;
for(int i = 0 ; i < 8 ; i ++)
{
out.println(data[i]);
}
out.close();
}catch(IOException e){}
}
public boolean isDigital(String s)
{
if(s==null)
return false ;
for(int i = 0 ; i < s.length() ; i ++)
{
if (!(s.charAt(i) >= 48 && s.charAt(i) <= 57 || s.charAt(i) == 46))
return false ;
}
return true ;
}
//******************************自动演示********************************
public void autoPlay()
{
initGame();
threadp = new ThreadAutoPlay(this);
threadp.start();
}
//*******************************
//******* 事件处理 *******
//*******************************
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if(obj == menuG_Exit){ System.exit(0) ; }
else if(obj == menuH_About) { new DialogAbout(this) ; }
else if(obj == menuH_Content) { new DialogHelp(this) ;}
else if(obj == menuG_Record) { recordWindow.initRecord() ;}
else
{
timeStop(); //如果演示正在进行就停止演示
autopStop() ;
if(obj == menuG_newGame ) { newGame() ; }
else if(obj == startLabel) { newGame(); }
else if(obj == menuG_autoPlay) { autoPlay() ; }
else if(obj == demoButton) { autoPlay() ; }
else if(obj == menuS_Set) { new DialogOption(this) ; newGame() ;}
else if(obj == gradeItems[0]) { setDiskTotalNum(3) ; newGame() ;}
else if(obj == gradeItems[1]) { setDiskTotalNum(6) ; newGame() ;}
else if(obj == gradeItems[2]) { setDiskTotalNum(9) ; newGame() ;}
else if(obj == gradeItems[3]) { new DialogCustomDisk(this) ; newGame() ;}
}
}
}
//***************************背景为图片的JPanel********************************
class ImageJPanel extends JPanel
{
private ImageIcon image = null;
public ImageJPanel(String s )
{
try
{
this.image = new ImageIcon(s);
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
protected void paintComponent(Graphics g)
{
//super.paintComponent(g);
Dimension d = getSize();
if(d.width >= image.getIconWidth() && d.height >= image.getIconHeight()) //当JPanel的大与等于Image的大小
{ //居中显示
int x1 = (d.width - image.getIconWidth()) / 2 ;
int y1 = (d.height - image.getIconHeight()) / 2 ;
g.drawImage( image.getImage(), x1, y1, null, null );
}
else //否则局部填充或显示
{
for( int x = 0; x < d.width; x += image.getIconWidth() )
for( int y = 0; y < d.height; y += image.getIconHeight() )
g.drawImage( image.getImage(), x, y, null, null );
}
}
}
//**************************startButton***************************
class LabelButton extends JLabel implements MouseListener , MouseMotionListener
{
Hanoi mainFrame ;
ImageIcon image1 , image2 ,image3 ;
public LabelButton(Hanoi f,String s1,String s2,String s3)
{
mainFrame = f ;
image1 = new ImageIcon(s1);
image2 = new ImageIcon(s2);
image3 = new ImageIcon(s3);
setIcon(image1);
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e)
{
this.setIcon(image1);
mainFrame.autopStop();
mainFrame.timeStop();
mainFrame.newGame();
}
public void mouseEntered(MouseEvent e)
{
this.setIcon(image2);
}
public void mouseExited(MouseEvent e)
{
this.setIcon(image1);
}
public void mousePressed(MouseEvent e)
{
this.setIcon(image3);
}
public void mouseDragged(MouseEvent e)
{
this.setIcon(image3);
}
public void mouseReleased(MouseEvent e)
{
this.setIcon(image1);
}
public void mouseMoved(MouseEvent e) { }
}
//****************************启动界面***********************************
class StartUp extends JWindow implements Runnable
{
private JFrame mainFrame;
private String imageName;
private JPanel panel;
public StartUp(JFrame f , String n)
{
mainFrame = f ;
panel = new ImageJPanel(n);
add(panel);
Thread threadStart =new Thread(this);
threadStart.start();
}
public void run()
{
setSize(500,300);
setLocationRelativeTo(null);
setVisible(true);
toFront();
try
{
Thread.sleep(1200);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
setVisible(false);
mainFrame.setVisible(true);
}
}
//*******************************排行榜***********************************
class RecordWindow extends JWindow
{
private Hanoi mainFrame ;
private JPanel gridPanel ;
private JLabel labels[] ;
public RecordWindow(Hanoi f)
{
mainFrame = f ;
initPanel();
this.add(gridPanel);
this.setSize(180,250);
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
setVisible(false);
}
});
}
public void initPanel()
{
Font defaultFont = new Font("宋体",Font.PLAIN,14);
Border border = BorderFactory.createLineBorder(new Color(204,236,255));
gridPanel = new JPanel(new GridLayout(9,2)); //最上面的是1和2,下一行3,4。。。
gridPanel.setBackground(new Color(185,211,238));
gridPanel.setPreferredSize(new Dimension(180,250));
gridPanel.setBorder(new EmptyBorder(4,4,0,4));
labels = new JLabel[18];
for(int i = 0 ; i < 18 ; i ++)
{
labels[i] = new JLabel();
labels[i].setFont(defaultFont);
labels[i].setBorder(border);
labels[i].setHorizontalAlignment(JLabel.CENTER);
gridPanel.add(labels[i]);
}
labels[0].setText("关 数") ;
labels[1].setText("最高纪录");
for(int i = 2 ; i < 17 ; i+=2)
{
labels[i].setText("第"+(i/2)+"关:");
}
}
public void initRecord()
{
File record = mainFrame.getRecordFile();
try
{
BufferedReader in = new BufferedReader(new FileReader(record));
for(int i = 0 ; i < 8 ; i ++)
{
String data = (String)in.readLine();
if(mainFrame.isDigital(data))
{
labels[2*i+3].setText(data+" 秒 ");
labels[2*i+3].setHorizontalAlignment(JLabel.RIGHT);
}
else
{
labels[2*i+3].setText("无");
labels[2*i+3].setHorizontalAlignment(JLabel.CENTER);
}
}
in.close();
}catch (Exception e){}
this.setLocationRelativeTo(null);
this.show();
}
}
//*********************************关于JDialog***************************
class DialogAbout extends JDialog
{
JFrame mainFrame ;
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JTextArea jTextArea = new JTextArea();
JLabel MyImage = new JLabel();
JPanel jPanel3 = new JPanel();
JButton Close = new JButton();
Border border1;
Border border2;
BorderLayout borderLayout1 = new BorderLayout();
BorderLayout borderLayout2 = new BorderLayout();
Font defaultFont = new Font("宋体",Font.PLAIN,12);
public DialogAbout(JFrame f)
{
border1 = BorderFactory.createEmptyBorder(10,10,25,20);
border2 = BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.lightGray,1),BorderFactory.createEmptyBorder(2,10,2,10));
mainFrame = f ;
this.setTitle("关 于 JAVA汉诺塔");
this.getContentPane().setBackground(Color.white);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(new Dimension(420, 209));
jPanel2.setBackground(Color.white);
jPanel2.setMinimumSize(new Dimension(260, 28));
jPanel2.setPreferredSize(new Dimension(260, 28));
jPanel2.setLayout(borderLayout1);
jPanel1.setBackground(Color.white);
jPanel1.setMinimumSize(new Dimension(160, 10));
jPanel1.setPreferredSize(new Dimension(160, 10));
jPanel1.setLayout(borderLayout2);
jPanel3.setBackground(Color.white);
jPanel3.setMinimumSize(new Dimension(260, 35));
jPanel3.setPreferredSize(new Dimension(260, 35));
jTextArea.setBorder(border1);
jTextArea.setEditable(false);
jTextArea.setText(" JAVA汉诺塔\n\n"
+" 制作人:汉诺塔专业开发组\n"
+" 版 权:谁看就是谁的!\n"
+" 版 本:1.01.23 完美破解中文版\n"
+" 如果你需要,请联系原作者:\n CIGA woshinige@163.com");
jTextArea.setLineWrap(true);
jTextArea.setForeground(new Color(55, 77, 118));
this.add(jPanel1, BorderLayout.WEST);
MyImage.setIcon(new ImageIcon("images/dialogabout.jpg"));
jPanel1.add(MyImage, BorderLayout.EAST);
this.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jTextArea, BorderLayout.CENTER);
jPanel2.add(jPanel3, BorderLayout.SOUTH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -