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

📄 node.java

📁 Dijkstra algorithm in Java.
💻 JAVA
字号:
/*
 * Node.java
 *
 * Created on May 31, 2007, 10:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package termproject_dijkstra;
import java.awt.*;

/**
 *
 * @author Peto
 */
public class Node 
{
    
    String  m_sName;
    int     m_iDist;
    
    ListEdges m_listNeighbours;
    Node    m_next;
    Node    m_pathFrom;
 
 
   int[] m_xyPos = {0,0};

 
    
    /** Creates a new instance of Node */
    public Node(String sName,int iDist) 
    {
        m_sName = new String(sName);
        m_listNeighbours = new ListEdges();
        m_iDist = iDist;
        m_next = null;
    }
    
   
    // Pushes a new node
    public void Push(Node node)
    {
        if (node != this)
        {
            if (m_next!=null)
                m_next.Push(node);
            else
                m_next = node;
        }
    }    
    // Inserts before current node
    private void Insert(String sName, int iDist)
    {
        Node temp = new Node(m_sName,iDist);
        temp.m_iDist = m_iDist;
        temp.m_next = m_next;
        temp.m_listNeighbours = m_listNeighbours;
        m_sName = sName;
        m_iDist = iDist;
        m_listNeighbours = new ListEdges();
        m_next = temp;
    }
    
     // Inserts before current node
    private void Insert(Node node)
    {
        int iTempDist = node.m_iDist;
        String sStringName = node.m_sName;
        ListEdges edgesTemp = node.m_listNeighbours;
                
        node.m_iDist = m_iDist;
        node.m_next = m_next;
        node.m_listNeighbours = m_listNeighbours;
        node.m_sName = m_sName;
        
        
        m_sName = sStringName;
        m_iDist = iTempDist;
        m_listNeighbours = edgesTemp;
        m_next = node;
    }   
    public void PushNeighbor(Node node,int iWeight)
    {
        m_listNeighbours.PushByWeight(node,iWeight);
    }
    
    public Node GetNode(String sName)
    {
        if (m_sName.compareTo(sName)==0)
            return this;
        else if (m_next!=null)
            return m_next.GetNode(sName);
        else
            return null;                  
    }
     
    public void DrawNode(Graphics g,float fxInc, float fyInc) 
    {
        
        int xPos =(int) (( m_xyPos[0]-GlobalVars.nodeRad) * fxInc);
        int yPos =(int) ((m_xyPos[1]-GlobalVars.nodeRad) * fyInc);
        
        g.setColor(new Color(0,255,0));
        g.fillOval(xPos,yPos,(int)(2*GlobalVars.nodeRad*fxInc),(int)(2*GlobalVars.nodeRad*fyInc));
        
        g.setColor(new Color(0,0,0));
        String dist;
        if (m_iDist >=GlobalVars.iINFINITY)
            dist = "INF";
        else
            dist = Integer.toString(m_iDist);

        g.drawString(m_sName+" ["+dist+"]",xPos+8,yPos+(int)(2*GlobalVars.nodeRad*fyInc)+2*GlobalVars.edgeDirRad);    
    }
   
    public void DrawEdges(Graphics g,float fxInc, float fyInc) 
    {
        
        int xPos =(int) ( m_xyPos[0] * fxInc);
        int yPos =(int) (m_xyPos[1] * fyInc);
        int x2Pos,y2Pos;
        int xDif,yDif;
        int xP,yP;
        Color cubeC = new Color(255,0,0);
        Color lineC = new Color(0,0,0);
        float len;      // line's length
        
        m_listNeighbours.InitTraversing();
        ListEdgesElement edge;
        
        while((edge=m_listNeighbours.GetAndMove())!=null)
        {
            if (GlobalVars.bDrawOnlyPaths && edge.m_pNode.m_pathFrom != this)
                continue;
            
            g.setColor(lineC);
            x2Pos =(int) ( edge.GetNode().m_xyPos[0] * fxInc);
            y2Pos =(int) ( edge.GetNode().m_xyPos[1] * fyInc);
            g.drawLine(xPos,yPos,x2Pos,y2Pos);
            g.drawString(Integer.toString(edge.m_iWeight),(xPos+x2Pos)/2+3,(yPos+y2Pos)/2+10);
            xDif = x2Pos - xPos;
            yDif = y2Pos - yPos;
            len = (float)Math.sqrt(Math.pow((double)xDif,2)+Math.pow((double)yDif,2));
            xP = x2Pos - (int)(((float)xDif)/len*(GlobalVars.nodeRad*fxInc))-(int)(GlobalVars.edgeDirRad*fxInc);
            yP = y2Pos - (int)(((float)yDif)/len*(GlobalVars.nodeRad*fyInc))-(int)(GlobalVars.edgeDirRad*fyInc);
            g.setColor(cubeC);
            g.fillOval(xP,yP,(int)(2*GlobalVars.edgeDirRad*fyInc),(int)(2*GlobalVars.edgeDirRad*fyInc));
            
        }
            
    }
}

⌨️ 快捷键说明

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