public GameFile() { game = ""; // load character names and descriptions characters = ""; File characterFile = new File("/home/cory/Programming/treasure_hunt/data/characters.data"); try { BufferedReader in = new BufferedReader(new FileReader(characterFile)); for (String s = in.readLine(); s != null; s = in.readLine()) { characters += s + "\n"; } characters.trim(); in.close(); } catch (IOException e) { System.out.println("File I/O error! Couldn't load character data file."); System.exit(1); } // load weapon names and descriptions weapons = ""; File weaponFile = new File("/home/cory/Programming/treasure_hunt/data/weapons.data"); try { BufferedReader in = new BufferedReader(new FileReader(weaponFile)); for (String s = in.readLine(); s != null; s = in.readLine()) { weapons += s + "\n"; } weapons.trim(); in.close(); } catch (IOException e) { System.out.println("File I/O error! Couldn't load weapon data file."); System.exit(1); } // load treasure names and desciptions treasures = ""; File treasureFile = new File("/home/cory/Programming/treasure_hunt/data/treasures.data"); try { BufferedReader in = new BufferedReader(new FileReader(treasureFile)); for (String s = in.readLine(); s != null; s = in.readLine()) { treasures += s + "\n"; } treasures.trim(); in.close(); } catch (IOException e) { System.out.println("File I/O error! Couldn't load treasure data file."); System.exit(1); } }
public ArrayList<String> getNewFoe(int race, int name) { ArrayList<String> foeDes = new ArrayList<>(); int index = 0; switch (race) { case 1: index = characters.indexOf("Gnome"); break; case 2: index = characters.indexOf("Halfling"); break; case 3: index = characters.indexOf("Goblin"); break; case 4: index = characters.indexOf("Human"); break; case 5: index = characters.indexOf("Dwarf"); break; case 6: index = characters.indexOf("Elf"); break; case 7: index = characters.indexOf("Orc"); break; case 8: index = characters.indexOf("Troll"); break; default: System.out.println("Couldn't generate characters"); System.exit(1); break; } String temp = characters.substring(index); // need to remove an extra line if female because of file format if (name > 10) { index = temp.indexOf("\n"); temp = temp.substring(index + 1); } for (; name > 0; name--) { index = temp.indexOf("\n"); temp = temp.substring(index + 1); } index = temp.indexOf(" "); foeDes.add(temp.substring(0, index)); foeDes.add(temp.substring(index + 1, temp.indexOf("\n"))); return foeDes; }
public void actionPerformed(ActionEvent e) { String cmd = (e.getActionCommand()); if (cmd.equals(aboutItem.getText())) JOptionPane.showMessageDialog( this, "Simple Image Program for DB2004\nversion 0.1\nThanks to BvS", "About imageLab", JOptionPane.INFORMATION_MESSAGE); else if (cmd.equals(quitItem.getText())) System.exit(0); else if (cmd.equals(openItem.getText())) { int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { pic2 = new Picture(chooser.getSelectedFile().getName()); pic1 = new Picture(pic2.width(), pic2.height()); lab.setIcon(pic2.getJLabel().getIcon()); sliderPanel.setVisible(false); pack(); repaint(); } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Could not open " + chooser.getSelectedFile().getName() + "\n" + ex.getMessage(), "Open Error", JOptionPane.INFORMATION_MESSAGE); } } } else if (cmd.equals(saveItem.getText())) { int returnVal = chooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { pic2.save(chooser.getSelectedFile().getName()); } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Could not write " + chooser.getSelectedFile().getName() + "\n" + ex.getMessage(), "Save Error", JOptionPane.INFORMATION_MESSAGE); } } } }
public void actionPerformed(ActionEvent e) { try { FileOutputStream f_out = new FileOutputStream(LOG_DIRECTORY + "\\save.data"); // Write object with ObjectOutputStream ObjectOutputStream obj_out = new ObjectOutputStream(f_out); // Write object out to disk obj_out.writeObject(directoryList); obj_out.writeObject(ERROR_LOG_NAME); obj_out.writeObject(MOVE_LOG_NAME); obj_out.flush(); obj_out.close(); printer.printError(LOG_DIRECTORY); } catch (IOException x) { printer.printError(x.toString()); } System.exit(0); }
/** Implementing the action of selection the exit menu item from the file menu. */ public void exitMenuItem_actionPerformed(ActionEvent e) { System.exit(0); }