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

📄 antapplication.java

📁 Java source code for the Ant Colony Optimization Problem.
💻 JAVA
字号:
package jwo.jpss.ants;     // Part of the ant simulation package.

import jwo.jpss.spatial.*; // For spatial footprint.
import javax.swing.*;      // Required for graphical objects.
import java.awt.*;          
import java.util.*;        // For dynamic collections.
import java.awt.event.*;   // For window closing event handling.

//  **************************************************************
/** A top level application window for displaying ant activity.
  * @author   Jo Wood.
  * @version  1.4, 21st October, 2001
  */
//  **************************************************************

public class AntApplication extends JFrame
{   
     // ------------------ Starter method ---------------------
    
    /** Creates the graphical window within which ants can be 
      * observed.
      * @param args Command line parameters (ignored).
      */
    public static void main(String args[])
    {	
    	new AntApplication("Ants in the garden...");
    }
    
    // ----------------- Object variables -------------------
        
    private Garden garden;         // Garden containing ants.
    private JTextField statusBar;  // Status bar for reporting messages.
      	      
    // ------------------- Constructor ----------------------
       
    /** Creates a top level application window with a given title.
      * @param title Title to associate with window.
      */
    public AntApplication(String title)
    {   
    	super(title);	       
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WinMonitor());
        
        // Size window to fit around garden.
        GardenPanel gardenPanel = new GardenPanel();
        gardenPanel.setBorder(BorderFactory.createEtchedBorder());	
        Footprint fp = new Footprint(0,0,400,400);
    	garden = new Garden(fp);      
    	gardenPanel.setPreferredSize(new Dimension((int)fp.getMERWidth(),
                                                   (int)fp.getMERHeight()));
        getContentPane().add(gardenPanel,BorderLayout.CENTER);
       
        // Add status bar.
        statusBar = new JTextField();
        statusBar.setEditable(false);
        statusBar.setBackground(getBackground());
        statusBar.setText(" ");
        getContentPane().add(statusBar,BorderLayout.SOUTH);

        // Size window, make it visible and start simulation.
        pack();
        setVisible(true);
        garden.addGraphicsListener(gardenPanel);
    	garden.startEvolution();
    }

    // -------------------- Private Methods ----------------------
  
    /** Asks the user if they really want to quit, then closes.
      */
    private void closeDown()
    {
        int response = JOptionPane.showConfirmDialog(this,
                                 "Are you sure you want to quit?");
        if (response == JOptionPane.YES_OPTION)
            System.exit(0);    // Exit program.
    }
    
    // ------------------- Nested Classes -------------------

    /** Simple panel for displaying a dynamic garden.
      */
    private class GardenPanel extends JPanel implements GraphicsListener
    {
        /** Draws the simulation on the panel.
          * @param g Graphics context within which to draw.
          */
        public void paintComponent(Graphics g) 
        {        
            garden.paint(g);
        }

        /** Checks whether given spatial object can be drawn on panel.
          * @param spObject Spatial object we wish to draw.
          * @return True if the spatial object can be drawn.
          */
        public boolean canDraw(SpatialObject spObject)
        {
            if (garden.compare(spObject) == SpatialModel.ENCLOSES)
                return true;
            else
                return false;
        }

        /** Reports the list of SpatialObjects assoicated with the given
          * spatial object (within, matching, overlapping or containing). 
          * @param spObject spatial object with which to compare.
          * @return List of spatial objects in contact with the given one.
          */
        public Vector objectsAt(SpatialObject spObject)
        {
            return garden.objectsAt(spObject);
        } 

        /** Redraws any graphics that need updating.
          */
        public void redrawGraphics()
        {   
            Dimension d = getSize();
            paintImmediately(0,0,d.width,d.height);
        }

        /** Displays the given message in the status bar.
          * @param Message Message to display.
          */
        public void displayMessage(String message)
        {
            statusBar.setText(message);
        }
    }

    /** Monitors window closing events and performs a 'clean exit' 
      * when requested.
      */
    private class WinMonitor extends WindowAdapter
    {  
        /** Responds to attempt to close window via the GUI. Checks
          * the user really wants to quite before closing down.
          * @param event Window closing event.
          */
        public void windowClosing(WindowEvent event)
        {
            closeDown();
        }
    }
}

⌨️ 快捷键说明

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