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

📄 tspmenu.java.svn-base

📁 Traveling Salesman Problem Java Genetic Algorithm Solution, Hope all enjoy it.
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
	                    zip.write(b,0,size);
	                 }
	                 zip.closeEntry();
	              }
	              i.close();
        	   }
           }
           zip.close();
           JOptionPane.showMessageDialog(parent.gui,"OK, maps exported to the file: \n"+f,"Info",JOptionPane.INFORMATION_MESSAGE);
        } catch(Throwable ex) {
           ex.printStackTrace();
           JOptionPane.showMessageDialog(parent.gui,"Can not export maps.","Error",JOptionPane.WARNING_MESSAGE);
        }
     }
  }
  
  /**
  * preview save dir for PDF report
  */
  File reportPrevDir=null;
  
  /**
   * Creates PDF report for current computation
   * NOTE: when running application on some server machines, no all needed graphics
   * libraries were installed there and image operations and even antialiasing for example
   * were not working there (in general).
   * @param e
   */
  protected void actionPDFReport(@SuppressWarnings("unused") ActionEvent e) {
     JFileChooser fileChooser=new JFileChooser();

     if(reportPrevDir==null) {
        reportPrevDir=new File(".").getAbsoluteFile();
     }
     fileChooser.setCurrentDirectory(reportPrevDir);
     String timestamp=new SimpleDateFormat("yyyy_MM_dd_HH_mm").format(Calendar.getInstance().getTime());
     fileChooser.setSelectedFile(new File(reportPrevDir,"tsp_report_"+timestamp+".pdf"));
     
     fileChooser.setFileFilter(new FileFilter(){
      @Override
      public boolean accept(File f) {
         if(f.isDirectory()) return true;
         if(f.getAbsolutePath().toLowerCase().endsWith(".pdf")) return true;
         return false;
      }

      @Override
      public String getDescription() {
         return "*.pdf";
      }

       });
     if(fileChooser.showSaveDialog(parent.gui)==JFileChooser.APPROVE_OPTION) {
        try {
           reportPrevDir=fileChooser.getCurrentDirectory();
           File filePDF=fileChooser.getSelectedFile();
           //set .pdf extension
           String fileName=filePDF.getName();
           if(!fileName.toLowerCase().endsWith(".pdf")) {
              String ext="pdf";
              if(!fileName.endsWith(".")) {
                 ext="."+ext;
              }
              fileName+=ext;
              File parentDir=filePDF.getParentFile();
              filePDF=new File(parentDir,fileName);
           }
           
           if(filePDF.exists()) {
              if(JOptionPane.showConfirmDialog(parent.gui,"Should be existing file overwritten ?","Question",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION) {
                 return;
              }
           }

          //get parent window image
           Component c=parent.gui.getContentPane();
           BufferedImage screenImage = (BufferedImage) c.createImage(c.getWidth(), c.getHeight());
           Graphics2D graphics=screenImage.createGraphics();

           //switch on antialiasing
           boolean antialiasing=parent.configuration.antialiasing;
           parent.configuration.antialiasing=true;
           graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
           graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC );

           parent.gui.statusBar.setVisible(false);
           c.paint(graphics);
           parent.gui.statusBar.setVisible(true);
           parent.configuration.antialiasing=antialiasing;
           
           //create parameters
           Map<String,String> params=Report.getResultInfo(parent);
                   
           //reorder cities so the list starts and ends with the start city
           City cities[]=parent.cities;
           if(parent.bestChromosome!=null) {
              cities=parent.bestChromosome.cities;
           }
           City cities2[]=new City[cities.length+1];
           
           int i;
           int iStart=0;
           //start cities from starting city
           for(i=0; i<cities.length; i++) {
              if(cities[i].startCity) {
                 iStart=i;
                 break;
              }
           }
           
           i=0;
           while(i<cities.length) {
              cities2[i]=cities[iStart];
              iStart++;
              i++;
              if(iStart>=cities.length) 
                 iStart=0;
           }

           //add the start city again at the end
           cities2[cities.length]=cities2[0];
           
           //create report
           new Report().saveReport(filePDF,cities2,screenImage,params,Report.getSystemProperties());
           
           JOptionPane.showMessageDialog(parent.gui,"OK, report created to the file: \n"+filePDF,"Info",JOptionPane.INFORMATION_MESSAGE);

           //open the report file
           //only at windows workstations
           try {
              Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",filePDF.getAbsolutePath()});
           } catch(Throwable ex2) {
              try {
                 Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start",filePDF.getAbsolutePath()});
              } catch(Throwable ex3) {
                 // nop
              }
           }
        } catch(Throwable ex) {
           ex.printStackTrace();
           JOptionPane.showMessageDialog(parent.gui,"Can not create pdf report.","Error",JOptionPane.WARNING_MESSAGE);
        }
     }
  }
  
  /**
    * Menu item action listeners
    */
   protected void setMenuProgramActionListeners() {
      menuItemExit.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            System.exit(0);
         }
      });
      menuItemStart.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionStart(e);
         }
      });
      menuItemPause.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionPause(e);
         }
      });
      menuItemPDFReport.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionPDFReport(e);
         }
      });
      
      menuItemXMLReport.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionXMLReport(null);
         }
      });

      menuItemXML2PDFReport.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionXML2PDFReport(e);
         }
      });
      
   }

   /**
    * Menu item action listeners
    */
   protected void setMenuSettingsActionListeners() {
      menuItemPopulationSize.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            menuItemPDFReport.setEnabled(false);
            menuItemXMLReport.setEnabled(false);
            Integer value=intInputDialog("Initial population size",
                  parent.configuration.getInitialPopulationSize());
            if(value != null) {
               parent.configuration.setInitialPopulationSize(value);
               resetMenu();
            }
         }
      });
      menuItemPopulationGrow.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            menuItemPDFReport.setEnabled(false);
            menuItemXMLReport.setEnabled(false);
            Double value=doubleInputDialog(
                  "Population grow (float number <= 1)", parent.configuration
                        .getPopulationGrow());
            if(value != null) {
               parent.configuration.setPopulationGrow(value);
               resetMenu();
            }
         }
      });
      menuItemMutationRatio.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            menuItemPDFReport.setEnabled(false);
            menuItemXMLReport.setEnabled(false);
            Double value=doubleInputDialog(
                  "Mutation ratio (float number <= 1)", parent.configuration
                        .getMutationRatio());
            if(value != null) {
               parent.configuration.setMutationRatio(value);
               resetMenu();
            }
         }
      });
      menuItemMaxBestAge.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            menuItemPDFReport.setEnabled(false);
            menuItemXMLReport.setEnabled(false);
            Integer value=intInputDialog("Maximum best cost age",
                  parent.configuration.getMaxBestCostAge());
            if(value != null) {
               parent.configuration.setMaxBestCostAge(value);
               resetMenu();
            }
         }
      });
      menuItemRMS.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            menuItemPDFReport.setEnabled(false);
            menuItemXMLReport.setEnabled(false);
            if(menuItemRMS.isSelected()) {
               parent.configuration.setRmsCost(true);
            } else {
               parent.configuration.setRmsCost(false);
            }
         }
      });
   }

   /**
    * Menu item action listeners
    */
   protected void addMenuMapsItems() {
      ButtonGroup group=new ButtonGroup();
      for(String m : TSP.mapFiles) {
    	  if(m==null) {
    		menuMaps.addSeparator();  
    	  } else {
	         JRadioButtonMenuItem menu=new JAntialiasedRadioButtonMenuItem();
	         menu.setText(m);
	         if(parent.mapFile.equals(m)) {
	            menu.setSelected(true);
	         }
	         group.add(menu);
	         menuMaps.add(menu);
    	  }
      }
   }

   /**
    * Menu item action listeners
    */
   protected void setMenuMapsActionListeners() {
      for(Component m : menuMaps.getMenuComponents()) {
    	  if(m instanceof JMenuItem) {
	         ((JMenuItem) m).addActionListener(new ActionListener() {
	
	            public void actionPerformed(ActionEvent e) {
	               menuItemPDFReport.setEnabled(false);
	               menuItemXMLReport.setEnabled(false);
	               JMenuItem menu=(JMenuItem) e.getSource();
	               parent.mapFile=menu.getText();
	               parent.gui.createCityMap(true);
	            }
	         });
    	  }
      }
      menuItemExportMaps.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionExportMaps(e);
         }
      });
   }

   /**
    * Menu item action listeners
    */
   protected void setMenuGraphicsActionListeners() {
      menuItemAntialiasing.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionAntialiasing(e);
         }
      });
   }

   /**
    * Menu item action listeners
    */
   protected void setMenuHelpActionListeners() {
      menuItemAbout.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent e) {
            actionAbout(e);
         }
      });  
   }

   
   /**
    * Creates computation report to XML file 
    * @param XMLFileName fileName of output report, if NULL, then dialog will be displayed
    */
   protected void actionXMLReport(String XMLFileName) {
      if(XMLFileName==null) {
		  JFileChooser fileChooser=new JFileChooser();
	
	      if(reportPrevDir==null) {
	         reportPrevDir=new File(".").getAbsoluteFile();
	      }
	      fileChooser.setCurrentDirectory(reportPrevDir);

	      String timestamp=new SimpleDateFormat("yyyy_MM_dd_HH_mm").format(Calendar.getInstance().getTime());
	      fileChooser.setSelectedFile(new File(reportPrevDir,"tsp_report_"+timestamp+".xml"));
	      
	      fileChooser.setFileFilter(new FileFilter(){
	       @Override
	       public boolean accept(File f) {
	          if(f.isDirectory()) return true;
	          if(f.getAbsolutePath().toLowerCase().endsWith(".xml")) return true;
	          return false;
	       }
	
	       @Override
	       public String getDescription() {
	          return "*.xml";
	       }
	
	        });
	      if(fileChooser.showSaveDialog(parent.gui)==JFileChooser.APPROVE_OPTION) {
	            reportPrevDir=fileChooser.getCurrentDirectory();
	            XMLFileName=fileChooser.getSelectedFile().getAbsolutePath();
	      } else {
	    	  return;
	      }
      } // no XMLFileName passed
     try {
        File fileXML=new File(XMLFileName);
        //set .xml extension
        String fileName=fileXML.getName();
        if(!fileName.toLowerCase().endsWith(".xml")) {
           String ext="xml";
           if(!fileName.endsWith(".")) {
              ext="."+ext;
           }
           fileName+=ext;
           File parentDir=fileXML.getParentFile();
           fileXML=new File(parentDir,fileName);
        }
        
        if(!parent.configuration.console) {
            if(fileXML.exists()) {
               if(JOptionPane.showConfirmDialog(parent.gui,"Should be existing file overwritten ?","Question",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION) {
                  return;
               }

⌨️ 快捷键说明

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