package gemini.tool;
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.ArrayList;
import javax.swing.BoxLayout; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener;
/** * @author Leif.Wang Last Modified:2004-10-13 * */ public class XbTool extends JPanel implements ActionListener, ListSelectionListener { private static final String NEW_LINE = "\n"; private static final String SPLIT = " | "; private static final String CAPTION = "XB tool"; private static final String ATTACK = "主动攻击"; private static final String GANG = "群体"; private static final String POISON = "中毒"; private static final String DESTROY = "破坏盔甲"; private static final int POS_X = 250; private static final int POS_Y = 180; private static final int LIST_WIDTH = 200; private static final int LIST_HEIGHT = 300; private static final int FRAME_WIDTH = 500; private static final int FRAME_HEIGHT = 500;
private JButton openButton, saveButton; private JCheckBox attackBox, gangBox, poisonBox, destroyBox; private JFileChooser fc; private ArrayList monsters; private JList list; private DefaultListModel listModel; private JTextField levelField; private JLabel filePathLabel; public XbTool() { super(new BorderLayout());
//Create a file chooser fc = new JFileChooser();
//Create the open button. We use the image from the JLF //Graphics Repository (but we extracted it from the jar). openButton = new JButton("Open a File..."); openButton.addActionListener(this);
//Create the save button. We use the image from the JLF //Graphics Repository (but we extracted it from the jar). saveButton = new JButton("Save a File..."); saveButton.addActionListener(this);
//For layout purposes, put the buttons in a separate panel JPanel buttonPanel = new JPanel(); //use FlowLayout buttonPanel.add(openButton); buttonPanel.add(saveButton);
// Creates a list model listModel = new DefaultListModel(); listModel.addElement("ID" + SPLIT + "Name");
// Creates a list list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); list.setLayoutOrientation(JList.VERTICAL); list.setVisibleRowCount(-1); list.addListSelectionListener(this);
// Creates a scroll pane for the list JScrollPane listScroller = new JScrollPane(list); listScroller.setPreferredSize(new Dimension(LIST_WIDTH, LIST_HEIGHT));
// Creates text field for level levelField = new JTextField(); levelField.addActionListener(this);
// Creates check boxes attackBox = new JCheckBox(ATTACK); attackBox.setSelected(false); attackBox.addActionListener(this);
gangBox = new JCheckBox(GANG); gangBox.setSelected(false); gangBox.addActionListener(this);
poisonBox = new JCheckBox(POISON); poisonBox.setSelected(false); poisonBox.addActionListener(this);
destroyBox = new JCheckBox(DESTROY); destroyBox.setSelected(false); destroyBox.addActionListener(this);
filePathLabel = new JLabel("Please open a cfg file!"); // Creates a panel for the check boxes JPanel checkPanel = new JPanel(); checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.PAGE_AXIS)); checkPanel.add(filePathLabel); checkPanel.add(levelField); checkPanel.add(attackBox); checkPanel.add(gangBox); checkPanel.add(poisonBox); checkPanel.add(destroyBox);
//Add the buttons and the log to this panel. add(buttonPanel, BorderLayout.PAGE_START); add(listScroller, BorderLayout.WEST); add(checkPanel, BorderLayout.CENTER); }
/** * Handles event for buttons */ public void actionPerformed(ActionEvent e) { Object source = e.getSource();
if (source == openButton) { if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); filePathLabel.setText(file.getPath()); // Gets monsters from file monsters = (ArrayList) Monster.loadMonstersFromFile(file);
for (int i = 0; i < monsters.size(); i++) { Monster monster = (Monster) monsters.get(i); listModel.addElement(monster.getId() + SPLIT + monster.getName()); } } } else if (source == saveButton) { if (monsters != null && fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); Monster.saveMonstersIntoFile(monsters, file); } } else { if (list.getSelectedIndex() < 1) return; // Gets the monster Monster monster = (Monster) monsters.get(list.getSelectedIndex() - 1);
// Sets the properites if (source == attackBox) { monster.setAttack(attackBox.isSelected()); } else if (source == gangBox) { monster.setGang(gangBox.isSelected()); } else if (source == poisonBox) { monster.setPoison(poisonBox.isSelected()); } else if (source == destroyBox) { monster.setDestroy(destroyBox.isSelected()); } else if (source == levelField) { try { monster.setLevel(Integer.parseInt(levelField.getText())); } catch (NumberFormatException e1) { JOptionPane.showMessageDialog( null, "Please input a number!", "Information", JOptionPane.INFORMATION_MESSAGE); } } } }
/** * Handles event for list */ public void valueChanged(ListSelectionEvent e) { int index = list.getSelectedIndex(); if (index == 0) return;
Monster monster = (Monster) monsters.get(index - 1);
levelField.setText(monster.getLevel() + ""); attackBox.setSelected(monster.isAttack()); gangBox.setSelected(monster.isGang()); poisonBox.setSelected(monster.isPoison()); destroyBox.setSelected(monster.isDestroy()); }
/** * Create the GUI and show it. For thread safety, this method should be * invoked from the event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true);
//Create and set up the window. JFrame frame = new JFrame(CAPTION); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(POS_X, POS_Y);
//Create and set up the content pane. JComponent newContentPane = new XbTool(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane);
//Display the window. frame.pack(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setVisible(true); }
public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }
}
//////////////////////////////////////////////////////////////////////////// package gemini.tool;
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collection;
/** * @author Leif.Wang * Last Modified:2004-10-13 * */ public class Monster { private static final String SPLIT = "\t,"; private static final String REG_SPLIT = "\\,"; private int id; private String name; private int level; private boolean attack; private boolean gang; private boolean poison; private boolean destroy;
public void setDestroy(boolean destroy) { this.destroy = destroy; }
public boolean isDestroy() { return destroy; }
public void setPoison(boolean poison) { this.poison = poison; }
public boolean isPoison() { return poison; }
public void setGang(boolean gang) { this.gang = gang; }
public boolean isGang() { return gang; }
public void setAttack(boolean attack) { this.attack = attack; }
public boolean isAttack() { return attack; }
public void setLevel(int level) { this.level = level; }
public int getLevel() { return level; }
public String getName() { return name; }
public int getId() { return id; }
/** * Default constructor */ public Monster() { id = 0; name = ""; setLevel(0); setAttack(false); setGang(false); setPoison(false); setDestroy(false); }
/** * Constructor with parameters */ public Monster(int id, String name, int level, boolean attack, boolean gang, boolean poison, boolean destroy) { this.id = id; this.name = name; this.setLevel(level); this.setAttack(attack); this.setGang(gang); this.setPoison(poison); this.setDestroy(destroy); }
/** * Overides Objects toString method to display monster information * * @see java.lang.Object#toString() */ public String toString() { return "$" + getId() + SPLIT + getName() + SPLIT + getLevel() + SPLIT + (isAttack() ? "1" : "0") + SPLIT + (isGang() ? "1" : "0") + SPLIT + (isPoison() ? "1" : "0") + SPLIT + (isDestroy() ? "1" : "0") + "\t;"; }
/** * Gets monsters from file */ public static Collection loadMonstersFromFile(File file) { try { ArrayList monsters = new ArrayList(); BufferedReader buffer = new BufferedReader(new FileReader(file));
String line = buffer.readLine(); Monster monster = null;
while (line != null) { String[] parameters = line.split(REG_SPLIT);
monster = new Monster( Integer.parseInt(parameters[0].trim().substring(1)), parameters[1].trim(), Integer.parseInt(parameters[2].trim()), "1".equals(parameters[3].trim()), "1".equals(parameters[4].trim()), "1".equals(parameters[5].trim()), "1".equals(parameters[6].charAt(0) + "") ); monsters.add(monster); line = buffer.readLine(); }
return monsters; } catch (Exception e) { e.printStackTrace(); return null; } } /** * Save the monsters information into file */ public static void saveMonstersIntoFile(ArrayList monsters, File file) { DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream(file)); for (int i = 0; i < monsters.size(); i++) { out.writeBytes(monsters.get(i).toString() + "\n"); } out.flush(); out.close(); } catch (Exception e) { try { out.close(); } catch (IOException e1) { e1.printStackTrace(); } e.printStackTrace(); } } }

|