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

📄 myframe.java

📁 Dijkstra algorithm in Java.
💻 JAVA
字号:
/*
 * MyFrame.java
 *
 * Created on June 2, 2007, 10:22 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package termproject_dijkstra;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 *
 * @author Peto
 */


public class MyFrame extends Frame{
    
    MyCanvas cScene;
    Button btDijkstra;                // Perfrom Dijkstra
    Panel panelGUI;
    
    
    
      
    
    /** Creates a new instance of MyFrame */
    public MyFrame() 
    {
        super.setTitle("XE36PJV - Term Project (Dijkstra) , Peter Kristof");
         this.setSize(GlobalVars.frameDim[0],GlobalVars.frameDim[1]);
        this.setLayout(new GridLayout(1,0,0,0));
        int CanvasDim[] = {GlobalVars.frameDim[0]/2,GlobalVars.frameDim[1]};
        cScene = new MyCanvas(CanvasDim);
        
        this.add(cScene);
        
        MenuBar mb = new MenuBar();   
        Menu meInfo = new Menu("Info");
        MenuItem meItem = new MenuItem("App Description");
        meItem.addActionListener(new Listener_menu(this));
        meInfo.add(meItem);
        mb.add(meInfo);
        this.setMenuBar(mb);
        
       
        InitGuiPanel();
        this.add(panelGUI);        
        
        int delay = 15; //milliseconds
        ActionListener taskPerformer = new ActionListener() 
        {
            public void actionPerformed(ActionEvent evt) 
            {
                cScene.update();
            }
        };
        new Timer(delay, taskPerformer).start();
        
        addWindowListener(new WindowAdapter() 
        {
             public void windowClosing(WindowEvent e) 
             {
                System.exit(0);
             }
        });

        cScene.m_graph.LoadNodes("EdgesList.txt");
    }
    
    TextField txfStartNode,
              txfNodeToMove,txfMoveX,txfMoveY,
              txfGraphFile;
    Panel panelGUILoadGraph,
          panelGUIStartNode,
          panelGUIDijkstra,panelGUIup,panelGUICheckbox,
          panelGUINodeMove;
    Label lbsError;
    Checkbox cbOnlyPath;
    Button btNodeMove,btLoadGraph,btBrowse;

    private void InitGuiPanel()
    {
        panelGUI= new Panel();
        panelGUI.setLayout(new GridLayout(3,1));
             
        panelGUIup = new Panel();
        panelGUIup.setLayout(new GridLayout(0,1));
        panelGUI.add(panelGUIup);
        
        // Button || FileName || Button
        panelGUILoadGraph = new Panel();
        panelGUILoadGraph.setLayout(new FlowLayout(FlowLayout.LEFT));
        btLoadGraph = new Button("Load Graph:");
        btLoadGraph.addActionListener(new Listener_btLoadGraph());
        panelGUILoadGraph.add(btLoadGraph);
        txfGraphFile = new TextField(12);
        panelGUILoadGraph.add(txfGraphFile);
        btBrowse = new Button("Browse");
        btBrowse.addActionListener(new Listener_btFileDialog(this));
        panelGUILoadGraph.add(btBrowse);
        panelGUIup.add(panelGUILoadGraph);
        
       
        // Button || Start node
        panelGUIDijkstra = new Panel();
        panelGUIDijkstra.setLayout(new FlowLayout(FlowLayout.LEFT));
        btDijkstra = new Button("Path(Start Node)");
        btDijkstra.addActionListener( new Listener_btd());
        panelGUIDijkstra.add(btDijkstra);
        txfStartNode = new TextField(4);
        panelGUIDijkstra.add(txfStartNode);
        panelGUIup.add(panelGUIDijkstra);
        
        // Checkbox
        panelGUICheckbox = new Panel();
        cbOnlyPath = new Checkbox("Show only shortest paths",false);
        cbOnlyPath.addItemListener(new Listener_cb());
        panelGUIup.add(cbOnlyPath);

        
        // Button || Node Name || Label || X || Y 
        panelGUINodeMove = new Panel();
        panelGUINodeMove.setLayout(new FlowLayout(FlowLayout.LEFT));
        btNodeMove = new Button("Move (Node,X,Y)");
        btNodeMove.addActionListener(new Listener_btNodeMove());
        panelGUINodeMove.add(btNodeMove);
        txfNodeToMove = new TextField(4);
        panelGUINodeMove.add(txfNodeToMove);
        txfMoveX = new TextField(2);
        panelGUINodeMove.add(txfMoveX);
        txfMoveY = new TextField(2);
        panelGUINodeMove.add(txfMoveY);
        panelGUIup.add(panelGUINodeMove);
        
        // Error output
        //panelGUIDijkstra = new Panel();
        //panelGUIDijkstra.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        lbsError = new Label("Status:                         ");
        panelGUIup.add(lbsError);
        
   
    }
    
    class Listener_menu implements ActionListener
    {
        
        Frame m_parent;
        
        Listener_menu(Frame parent)
        {
            m_parent = parent;
        }
        
        public void actionPerformed(ActionEvent e)
        {
            new DescriptionDialog(m_parent,true);
        }
    }
    
    
    // Performs dijkstra on the graph
    class Listener_btd implements ActionListener
    {        
        public void actionPerformed(ActionEvent e)
        {
            if (cScene.m_graph.m_pStart != null)
            {
                String sStartNode = txfStartNode.getText();
            
                if (cScene.m_graph.m_pStart.GetNode(sStartNode)==null)
                {
                    lbsError.setText("Status: Error - wrong node name");
                }
                else
                {
                    cScene.m_graph.ShortestPaths(sStartNode);
                    lbsError.setText("Status: Dijkstra finished");
                }
            }
        }
    }

    // moves node
    class Listener_btNodeMove implements ActionListener
    {        
        public void actionPerformed(ActionEvent e)
        {
            if (cScene.m_graph.m_pStart != null)
            {
                try
                {
                    String sNode = txfNodeToMove.getText();
                    int iX = Integer.parseInt(txfMoveX.getText());
                    int iY = Integer.parseInt(txfMoveY.getText());
                    Node node = cScene.m_graph.m_pStart.GetNode(sNode);
                    if (node==null)
                    {
                        lbsError.setText("Status: Error - wrong node name");
                    }
                    else
                    {
                        node.m_xyPos[0] += iX;
                        node.m_xyPos[1] += iY;
                        lbsError.setText("Status: Move - finished");
                    }
                }
                catch(NumberFormatException ee)
                {
             
                }
               

            }
        }
    }  

    // LoadGraph
    class Listener_btLoadGraph implements ActionListener
    {        
        public void actionPerformed(ActionEvent e)
        {
            String sFileName = txfGraphFile.getText();
            File f = new File(sFileName);
            if(f.exists())
            {
                lbsError.setText("Status: "+ cScene.m_graph.LoadNodes(sFileName));
            }
            else
            {
                lbsError.setText("Status: File doesn't exist");
            }

            
        }
    }      
    
    class Listener_btFileDialog implements ActionListener
    {
        Frame m_parent;
        
        Listener_btFileDialog(Frame parent)
        {
            m_parent = parent;
        }
        public void actionPerformed(ActionEvent e)
        {
            
            FileDialog fd = new FileDialog(m_parent,"Select",FileDialog.LOAD);
            
            fd.setDirectory(System.getProperty("user.dir"));
            
            fd.setVisible(true);
            
            if (fd.getFile() != null)
            {
                txfGraphFile.setText(fd.getFile());
            }
        }
    }
  
    
    // Turn on/off drawing just paths to start node
    class Listener_cb implements ItemListener 
    {
        public void itemStateChanged(ItemEvent e)
        {
            GlobalVars.bDrawOnlyPaths = cbOnlyPath.getState();
        }
    }
    
    public static void main(String[] args)
    {
        new MyFrame().setVisible(true);
        
    }
    
}

⌨️ 快捷键说明

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