⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linesight.java

📁 基本路径搜索之遇到障碍物问题 并且可以标记出所走路线
💻 JAVA
字号:
/*
 * LineSight.java
 *
 * Created on 2007年3月29日, 下午1:08
 */

package ailinesight;
import com.sun.swing.internal.plaf.metal.resources.metal;
import java.util.*;
/**
 *
 * @author  Administrator
 */
public class LineSight extends javax.swing.JPanel{
    private class ToPoint{
        public ToPoint(int x, int y){
            this.x = x;
            this.y = y;
        }
        public boolean compare(int x, int y){
            if(this.x==x && this.y == y){
                return true;
            }
            return false;
        }
        public boolean compare(ToPoint t){
            if(this.x==t.x && this.y == t.y){
                return true;
            }
            return false;
        }
        public int x;
        public int y;
    }
    /** Creates new form LineSight */
    final static int WH = 20;
    final static int SPEED = 1;
    static boolean PAINT = false;
    static boolean WEIGHT = false;
    
    private ToPoint goal;
    private ArrayList controller;
    private ToPoint nowTo;
    private ArrayList bRect;
    private ToPoint to;
    private java.awt.Color lineColor = new java.awt.Color(0,128,0);
    
    public LineSight() {
        bRect = new ArrayList();
        controller = new ArrayList();
        initComponents();
        repaint();
    }
    public void addGoal(int x, int y){
        goal = new ToPoint(x,y);
    }
    
    public void addController(int x, int y){
        nowTo = new ToPoint(x,y);
        controller.add(new ToPoint(x,y));
        this.repaint();
    }
    public void removeController(int x, int y){
        controller.remove(new ToPoint(x,y));
    }
    public void addBlackRect(int x, int y){
        bRect.add(new ToPoint(x,y));
    }
    public void removeBlackRect(int x, int y){
        bRect.remove(new ToPoint(x,y));
    }
    public void paint(java.awt.Graphics g){
        g.setColor(java.awt.Color.WHITE);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        g.setColor(lineColor);
        int i;
        for(i=0; i<this.getHeight(); i++){
            g.drawLine(0, i*WH, this.getWidth(), i*WH);
            g.drawString((i)+"", 0, i*WH);
        }
        for(i=0; i<this.getWidth(); i++){
            g.drawLine(i*WH, 0, i*WH, this.getHeight());
            g.drawString((i+1)+"", i*WH, 10);
        }
         
        for(i=0; i<bRect.size(); i++ ){
            g.setColor(java.awt.Color.BLACK);
            to = (ToPoint)bRect.get(i);
            g.fillRect((to.x-1)*WH, (to.y-1)*WH, WH, WH);
        }
        if(PAINT==false){
            g.setColor(java.awt.Color.MAGENTA);
            g.fillOval(((nowTo.x-1)*WH), ((nowTo.y-1)*WH), WH, WH);
        }
        else{
            for(i=0; i<controller.size(); i++ ){
                g.setColor(java.awt.Color.MAGENTA);
                to = (ToPoint)controller.get(i);
                g.fillOval(((to.x-1)*WH), ((to.y-1)*WH), WH, WH);
            }
        }
        for(i=0; i<1; i++){
            g.setColor(java.awt.Color.RED);
            g.fillOval(((goal.x-1)*WH), ((goal.y-1)*WH), WH, WH);
        }
        
    }
    public void run(){
        patternOne();
    }
    
    
    public boolean checkDirectios(int x, int y){
        for(int i=1; i<=8; i++){
            if(checkAnDirection(x,y,i)==null){
                return true;//have
            }
        }
        return false;//don't have
    }
    public ToPoint checkAnDirection(int x, int y, int flag){
        // 1    2   3
        // 4    0   5
        // 6    7   8
        ToPoint to = null;
        switch(flag){
            case 0:
                to = new ToPoint(x,y);
                break;
            case 1:
                to = new ToPoint(x-1, y-1);
                break;
            case 2:
                to = new ToPoint(x, y-1);
                break;
            case 3:
                to = new ToPoint(x+1, y-1);
                break;
            case 4:
                to = new ToPoint(x-1, y);
                break;
            case 5:
                to = new ToPoint(x+1, y);
                break;
            case 6:
                to = new ToPoint(x-1, y+1);
                break;
            case 7:
                to = new ToPoint(x, y+1);
                break;
            case 8:
                to = new ToPoint(x+1, y+1);
                break;
        }
        for(int i=0; i<bRect.size(); i++){
            if(((ToPoint)(bRect.get(i))).compare(to)==true){
                return null;
            }
        }
        return to;
    }
    public void clear(int x, int y){
        controller.clear();
        this.addController(x, y);
    }
    public boolean checkPassed(int x, int y){
        for(int i=0; i<controller.size(); i++){
            if(((ToPoint)controller.get(i)).compare(x,y)==true){
                return false;
            }
        }
        return true;
    }
    public ToPoint getNextStep(ToPoint next){
        if(nowTo.x < goal.x){
            next.x=nowTo.x+SPEED;
        }
        else if(nowTo.x > goal.x){
            next.x=nowTo.x-SPEED;
        }
        if(nowTo.y > goal.y){
            next.y=nowTo.y-SPEED;
        }
        else if(nowTo.y < goal.y){
            next.y=nowTo.y+SPEED;
        }
        return next;
    }
    public void patternOne(){
        int x=0, y=0;
        ToPoint to=new ToPoint(0,0);
        while(true){
            x = to.x;
            y = to.y;
            if(nowTo.x==goal.x && nowTo.y==goal.y){
                break;
            }
            try{
                Thread.sleep(200);
            }catch(Exception e){}
            to = getNextStep(to);
            if(this.checkAnDirection(to.x,to.y,0)!=null){//no way
                this.addController(to.x, to.y);
            }
            else{
                ToPoint uo=patternTwo(to.x,to.y,x,y);
                if(uo==null){
                    return;
                }
                else{
                    to = uo;
                }
            }
        }
    }
   
    public ToPoint patternTwo(int tox, int toy, int x, int y){
        ToPoint tp = new ToPoint(0,0);
        while(true){
            if(WEIGHT == false){
                for(int i=1; i<=8; i++){
                    tp = this.checkAnDirection(x,y,i);
                    if(tp!=null &&
                       this.checkDirectios(tp.x, tp.y)==true &&
                       this.checkPassed(tp.x, tp.y)==true){
                       this.addController(tp.x, tp.y);
                        break;
                    }
                    else if(i==8){
                        for(int j=1; j<=8; j++){
                            tp = this.checkAnDirection(x,y,j);
                            if(tp!=null &&
                               this.checkDirectios(tp.x, tp.y)==true){
                               this.addController(tp.x, tp.y);
                                break;
                            }
                        }
                    }
                }
            }
            else{
                for(int i=8; i>=1; i--){
                    tp = this.checkAnDirection(x,y,i);
                    if(tp!=null &&
                       this.checkDirectios(tp.x, tp.y)==true &&
                       this.checkPassed(tp.x, tp.y)==true){
                       this.addController(tp.x, tp.y);
                        break;
                    }
                    else if(i==1){
                        for(int j=1; j<=8; j++){
                            tp = this.checkAnDirection(x,y,j);
                            if(tp!=null &&
                               this.checkDirectios(tp.x, tp.y)==true){
                               this.addController(tp.x, tp.y);
                                break;
                            }
                        }
                    }
                }
            }
            x = tp.x;
            y = tp.y;
            tp=getNextStep(tp);
            if(this.checkAnDirection(tp.x,tp.y,0)!=null &&
                this.checkPassed(tp.x, tp.y)==true){
                tp.x = x;
                tp.y = y;
                return tp;
            }
            if(nowTo.x==goal.x && nowTo.y==goal.y){
                return null;
            }
            try{
                Thread.sleep(200);
            }catch(Exception e){}
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 414, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 325, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -