import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class J2 extends JApplet implements ActionListener
{
private TokenPane[][] tp=new TokenPane[8][8];
private JButton jb1,jb2;
private JLabel jl;
private Point[] load=new Point[64];
private int step=0;
//void init()
public void init()
{
//初始化组见
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
tp[i][j]=new TokenPane();
tp[i][j].setBackground(Color.WHITE);
}
}
jb1=new JButton("Find Path");
jb2=new JButton("Clear Path");
jl=new JLabel("Path Found");
//添加到框架
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout(8,8,10,10));
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
jp1.add(tp[i][j]);
}
}
JPanel jp2=new JPanel();
jp2.setLayout(new FlowLayout());
jp2.add(jb1);
jp2.add(jb2);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jl,BorderLayout.NORTH);
getContentPane().add(jp1,BorderLayout.CENTER);
getContentPane().add(jp2,BorderLayout.SOUTH);
//注册侦听器
jb1.addActionListener(this);
jb2.addActionListener(this);
}
//事件处理函数
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
findPath();
}
else
{
clearPath();
}
}
//保护函数
protected boolean isHavePath()
{
int startx=0,starty=0;
int xdistance,ydistance;
int direction=0;
//初始化
load[step]=new Point(startx,starty);
step++;
while(true)
{
if(startx==7&&starty==7) return(true);
switch(direction)
{
case 0:
if(starty+1<=7&&tp[startx][starty+1].isHaveToken()==false&&isInLoad(new Point(startx,starty+1))==false)
{
load[step]=new Point(startx,starty+1);
step++;
direction=0;
starty++;
}
else direction++;
break;
case 1:
if(startx+1<=7&&tp[startx+1][starty].isHaveToken()==false&&isInLoad(new Point(startx+1,starty))==false)
{
load[step]=new Point(startx+1,starty);
step++;
direction=0;
startx++;
}
else direction++;
break;
case 2:
if(starty-1>=0&&tp[startx][starty-1].isHaveToken()==false&&isInLoad(new Point(startx,starty-1))==false)
{
load[step]=new Point(startx,starty-1);
step++;
direction=0;
starty--;
}
else direction++;
break;
case 3:
if(startx-1>=0&&tp[startx-1][starty].isHaveToken()==false&&isInLoad(new Point(startx-1,starty))==false)
{
load[step]=new Point(startx-1,starty);
step++;
direction=0;
startx--;
}
else direction++;
break;
default:
step--;
if(step<=0) return(false);
xdistance=load[step].x-load[step-1].x;
ydistance=load[step].y-load[step-1].y;
if(xdistance==0&&ydistance==1)
{
direction=1;
startx=load[step-1].x;
starty=load[step-1].y;
}
else if(xdistance==1&&ydistance==0)
{
direction=2;
startx=load[step-1].x;
starty=load[step-1].y;
}
else if(xdistance==0&&ydistance==-1)
{
direction=3;
startx=load[step-1].x;
starty=load[step-1].y;
}
else
{
direction=4;
}
break;
}
}
}
//void findPath
protected void findPath()
{
if(isHavePath()==true)
{
for(int i=0;i
tp[load[i].x][load[i].y].setBackground(Color.GREEN);
}
step=0;
}
else
{
JOptionPane.showMessageDialog(null,"no load","error",JOptionPane.ERROR_MESSAGE);
}
}
//void clearPath()
protected void clearPath()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(tp[i][j].isHaveToken()==false)
{
tp[i][j].setBackground(Color.WHITE);
}
}
}
}
//boolean isInLoad(Point pt)
public boolean isInLoad(Point pt)
{
for(int i=0;i
if(load[i].equals(pt))
{
return true;
}
}
return false;
}
//main函数
public static void main(String[] args)
{
JFrame jf=new JFrame("Exercise 12.1");
J2 ap=new J2();
jf.add(ap);
//
ap.init();
ap.start();
//
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(600,600);
jf.setVisible(true);
}
}
//TokenPane类
class TokenPane extends JPanel implements MouseListener
{
private boolean token=false;
TokenPane()
{
addMouseListener(this);
}
//
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(token==true)
{
g.drawLine(0,0,getWidth(),getHeight());
g.drawLine(getWidth(),0,0,getHeight());
}
}
//setToken()
public void setToken()
{
token=true;
}
public boolean isHaveToken()
{
return token;
}
public void clearToken()
{
token=false;
}
//事件处理函数
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
if(token==true)
{
token=false;
repaint();
}
else
{
token=true;
repaint();
}
}
}
鼠标单击方格产生标记,
find path 找出路
你给的分数太低
没人做的
就是有现成的 人家也是懒得发