📄 javaants.java~
字号:
/*
Copyright (C) 2004 Julia Handl
Email: Julia.Handl@gmx.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*****************************************************************
Julia Handl - 18004385
Monash University, 1.7.2001
File: JavaAnts.java
Package: JavaAnts.TopicMap
Description:
* Main program and GUI
*****************************************************************/
package javaants.topicmap;
import javaants.*;
import javaants.exception.*;
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/** Main program and GUI */
public class JavaAnts {
private static TopicMap map; // current topic map
private static Configuration configuration; // parameter configuration
private static Data data; // current data collection
private static String [] keywords; // current keywords
private static Document [] documents; // current documents
private static Frame frame;
private static boolean init = false; // is map already initialized?
private static boolean antMode = true; // shall I use ants?
/** Main routine
* @param no arguments necessary
*/
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
/// dann halt nicht ....
}
try {
// create configuration
configuration = new Configuration();
Configuration c = configuration.read();
if (c != null) configuration = c;
// create frame and menu bars
frame = new Frame();
frame.setTitle("Ant-based clustering");
frame.setBackground(Color.lightGray);
frame.setLayout(new BorderLayout());
MenuBar antBar = new MenuBar();
// File menu
Menu file = new Menu("Data");
// Work with hardcoded test data
MenuItem create = new MenuItem("Generate Test Data");
create.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
configuration.setTextMode(0);
data = generate(configuration.getchoice());
if (configuration.getchoice() == 0) {
configuration.setTestData(true);
}
else {
configuration.setTestData(false);
}
init(false,"");
}});
file.add(create);
antBar.add(file);
Menu dist = new Menu("Mode");
// Toggle between Ants versions
MenuItem toggle = new MenuItem("Ant-Q");
toggle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
configuration.setMethod(0);
System.out.println("Switching to Ant-Q Mode");
antMode = true;
if (init == true) init(false,"");
}});
MenuItem toggle1 = new MenuItem("Ants");
toggle1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
configuration.setMethod(1);
System.out.println("Switching to Ant Mode");
antMode = true;
if (init == true) init(false,"");
}});
dist.add(toggle);
dist.add(toggle1);
antBar.add(dist);
// Configuration menu
Menu confs = new Menu("Configuration");
// Allow configuration modifications
MenuItem ant = new MenuItem("Modify");
ant.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ConfDialog(configuration, frame, map, init);
init = false;
}});
confs.add(ant);
antBar.add(confs);
// Execute menu
Menu execute = new Menu("Ants");
// Scatter the documents randomly on the map
MenuItem scatter = new MenuItem("Scatter Documents");
scatter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
try {
if (antMode == false) throw new WrongModeException();
if (init == false) throw new NoInitException();
if (init == true) init(false,"");
} catch (NoInitException e) {
System.out.println(e.getMessage());
} catch(WrongModeException e) {
System.out.println(e.getMessage());
}
}});
execute.add(scatter);
// Run ants as own thread
MenuItem run = new MenuItem("Start / Resume Sorting");
run.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
if (init == false) throw new NoInitException();
if (antMode == false) throw new WrongModeException();
new Thread(map).start();
} catch (NoInitException e) {
System.out.println(e.getMessage());
} catch(WrongModeException e) {
System.out.println(e.getMessage());
}
}});
execute.add(run);
// Halt execution of ants sorting
MenuItem stop = new MenuItem("Stop Sorting");
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
if (init == false) throw new NoInitException();
if (antMode == false) throw new WrongModeException();
map.stop();
} catch (NoInitException e) {
System.out.println(e.getMessage());
} catch(WrongModeException e) {
System.out.println(e.getMessage());
}
}});
execute.add(stop);
antBar.add(execute);
// Help menu
Menu info = new Menu("Info");
MenuItem help = new MenuItem("Help");
final String helpstring =
"How to use (step by step):\n\n" +
"1) Specify operating mode." +
" Menu: Mode -> (Ant-Q | Ants)\n" +
"2) Specify configuration settings if desired. Otherwise default options are used." +
" Menu: Configuration->Modify\n" +
"3) Generate an artificial data set using Data -> Generate Test Data\n" +
"4) Start sorting, stop it and possibly restart it." +
" Menu: Ants -> (Start sorting | Stop sorting | Scatter)\n\n\n" +
"Additional info:\n\n" +
"- Ants: Basic ant-based clustering algorithm\n" +
"- Ant-Q: Extended ant-based clustering algorithm (described in Handl and Meyer 2002)\n\n";
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ImageIcon icon = new ImageIcon("images/ant.gif");
JOptionPane.showMessageDialog(null,
helpstring, "Help",
JOptionPane.INFORMATION_MESSAGE, icon);
}});
info.add(help);
MenuItem about = new MenuItem("About");
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ImageIcon icon = new ImageIcon("images/ant.gif");
JOptionPane.showMessageDialog(null,
"Basic ant-based clustering demo \nCopyright (c) Julia Handl, Monash University, 2001", "About",
JOptionPane.INFORMATION_MESSAGE, icon);
}});
info.add(about);
antBar.add(info);
frame.setMenuBar(antBar);
frame.addWindowListener(new WindowCloser());
frame.pack();
frame.setSize(54*7+10, 54*7+48);
frame.show();
frame.setVisible(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -