---
title: "Java[伪]寻径追踪实现(由Loonframework提供)"
author: "mmdev"
date: "2007-10-19T09:52:00Z"
tags: []
slug: "java-loonframework"
draft: false
lastmod: "2012-04-14T20:15:01Z"
id: 1486385
url: "https://mmdev.iteye.com/blog/1486385"
---

我们都知道，在诸如魔兽的很多战略游戏中，会出现要求某A角色接近某B角色的情况（攻击或是怎么着咱不考虑……），玩家很简单的一步操作，却会引发开发者“怎么走过去”这个“复杂”的算法问题，也就是所谓的“Path-finding”——寻径。

好的寻径算法，不但能避免不必要的资源损耗，而且能令游戏友好度增加。反之则会严重影响游戏的可玩性或诸如GIS等系统的可靠性。

在本次，我以Java为例，演示一个最简单的寻径系统——甚至可以说是伪寻径系统的实现。 (^^)

首先，我们建立一个追踪者（一说到这里我就想起了生化里的掠食者……）

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.test.tracker;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Color;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:追踪者。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassTracker...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintSIZE=TrackerPanel.SIZE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicintx;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicinty;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*析构函数，默认坐标为0,0  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicTracker()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this(0,0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*析构函数,设定x,y坐标  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramx  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramy  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicTracker(intx,inty)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.x=x;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.y=y;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*设定追踪对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramprey  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidchase(Objectiveobjective)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//始终以目标坐标矫正追踪者坐标。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(x&gt;objective.x)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)x--;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}elseif(x&lt;objective.x)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)x++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(y&gt;objective.y)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)y--;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}elseif(y&lt;objective.y)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)y++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*绘制追踪者  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramg  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoiddraw(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.setColor(Color.RED);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.fillRect(x\*SIZE,y\*SIZE,SIZE,SIZE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

而后，我们建立一个被追踪的目标。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.test.tracker;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Color;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:被跟踪的目标对象。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassObjective...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintSIZE=TrackerPanel.SIZE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintUP=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintDOWN=1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintLEFT=2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintRIGHT=3;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicintx;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicinty;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*析构函数，内部转换x,y坐标。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicObjective()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)x=TrackerPanel.COL/2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)y=TrackerPanel.ROW/2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*析构函数，设定x,y坐标。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicObjective(intx,inty)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.x=x;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.y=y;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*移动目标  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramdir  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidmove(intdir)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)switch(dir)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseUP:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)y--;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseDOWN:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)y++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseLEFT:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)x--;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseRIGHT:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)x++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*绘制目标  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@paramg  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoiddraw(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.setColor(Color.BLUE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.fillOval(x\*SIZE,y\*SIZE,SIZE,SIZE);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

最后，我们建立一个面板，用以绘制图形。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packageorg.test.tracker;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Color;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Dimension;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Frame;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Graphics;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Image;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.Panel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.KeyEvent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.event.KeyListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.awt.image.BufferedImage;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:追踪演示面板。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassTrackerPanelextendsPanelimplementsRunnable,KeyListener...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinallongserialVersionUID=1L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定窗体宽与高  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintWIDTH=480;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintHEIGHT=480;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//描绘的正方体大小  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)staticfinalintSIZE=8;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获得对应列数,即将frame转化为类数组的存在。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticfinalintROW=HEIGHT/SIZE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticfinalintCOL=WIDTH/SIZE;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//获得对应行数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintUP=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintDOWN=1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintLEFT=2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticfinalintRIGHT=3;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//追踪者  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateTrackertracker;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//目标  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateObjectiveobjective;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateThreadthread;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateImagescreen=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateGraphicsbg=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicTrackerPanel()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setPreferredSize(newDimension(WIDTH,HEIGHT));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)setFocusable(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)addKeyListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)screen=newBufferedImage(WIDTH,HEIGHT,1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bg=screen.getGraphics();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//追踪者初始位置,0行,0列  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)tracker=newTracker(0,0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//目标初始位置,50行,50列  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)objective=newObjective(50,50);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thread=newThread(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)thread.start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidpaint(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定底色为白色，并清屏  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bg.setColor(Color.white);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)bg.fillRect(0,0,WIDTH,HEIGHT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//绘制追踪者  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)tracker.draw(bg);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//绘制目标  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)objective.draw(bg);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.drawImage(screen,0,0,this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g.dispose();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidupdate(Graphicsg)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)paint(g);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidrun()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)while(true)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定追击的目标。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)tracker.chase(objective);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)repaint();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Thread.sleep(110);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(InterruptedExceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidkeyTyped(KeyEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidkeyPressed(KeyEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intkey=e.getKeyCode();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)switch(key)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseKeyEvent.VK\_UP:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)objective.move(UP);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseKeyEvent.VK\_DOWN:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)objective.move(DOWN);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseKeyEvent.VK\_LEFT:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)objective.move(LEFT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)caseKeyEvent.VK\_RIGHT:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)objective.move(RIGHT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)repaint();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicvoidkeyReleased(KeyEvente)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidmain(String\[]args)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Framefrm=newFrame();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setTitle("简单的Java图形寻径追踪实现(由Loonframework提供)");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setSize(WIDTH,HEIGHT);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setResizable(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.add(newTrackerPanel());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)frm.setVisible(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

这时，画面上显示的红色正方体为追踪者，蓝色圆球为被追踪目标，而无论篮球如何移动，红色正方体都将始终向篮球靠拢。

实际上，通过代码我们就可以知道，这时两者间的移动路径，被没有经过复杂的方法演算，而是以目标的坐标来决定的，颇有独孤九剑那种，敌人若是只有一招，我也只有一招，他若是有千招万招，我也自是千招万招。但是，这种方法在实际的寻径处理中，并不能很好的解决如障碍物，区域转换等问题，所以只是一种\[伪]寻径，  
或者说是在无障碍情况下的简单解决方案。

如果关注寻径算法在Java中的进一步实现，还请继续关注我的blog……

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/find.gif)