/** * Checks if the score is high enough to get to the high scores list, adds the name and score and * organizes the list. If HighScores.dat is not found, the method generates a blank one. * * @param name The nickname of the person getting to the list. * @param score The score gained. */ public static void addHighScore(String name, int score) { // If we don't yet have a high scores table, we create a blank (and let the user know about it) if (!new File("HighScores.dat").exists()) { // This object matrix actually stores the information of the high scores list Object[][] highScores = new Object[10][3]; // We fill the high scores list with blank entries: #. " " 0 for (int i = 0; i < highScores.length; i++) { highScores[i][0] = (i + 1) + "."; highScores[i][1] = " "; highScores[i][2] = 0; } // This actually writes and makes the high scores file try { ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("HighScores.dat")); o.writeObject(highScores); o.close(); } catch (IOException e) { e.printStackTrace(); } } // We read the file to check if we have a new high score, and then rewrite the highscore // This is done even if we didn't previously have a high scores list try { ObjectInputStream o = new ObjectInputStream(new FileInputStream("HighScores.dat")); // The object matrix does the same as the previous one. // Here we just take what we read from the HighScores.dat to the Object[][] HighScores. Object[][] highScores = (Object[][]) o.readObject(); // Then we start searching for an entry for which the score is smaller than the achieved score for (int i = 0; i < highScores.length; i++) { if ((Integer) highScores[i][2] < score) { // Once found we start to move entries, which are below the score we had, downwards. // I.e. 10. becomes whatever 9. was. 9. becomes what 8. was etc... for (int j = 9; j > i; j--) { highScores[j][0] = (j + 1) + "."; highScores[j][1] = highScores[j - 1][1]; highScores[j][2] = highScores[j - 1][2]; } // Then we write the score and the name we just got to the correct place highScores[i][0] = (i + 1) + "."; highScores[i][1] = name; highScores[i][2] = score; // And break the loop. /*Maybe this could be avoided somehow? I haven't been able to come up with an easy way yet.*/ break; } } try { // And finally we overwrite the HighScores.dat with our highScores object matrix ObjectOutputStream n = new ObjectOutputStream(new FileOutputStream("HighScores.dat")); n.writeObject(highScores); n.close(); } catch (IOException e) { e.printStackTrace(); } } catch (ClassNotFoundException | IOException e) { e.printStackTrace(); } }
/** * A getter for the high scores list. Reads it directly from file and throws an error if the file * is not found (!working on this!). * * @return Object[][] where [i][0] is the rank (String), [i][1] is the name (String) and [i][2] is * the score (Integer). */ public static Object[][] getHighScore() { if (!new File("HighScores.dat").exists()) { // This object matrix actually stores the information of the high scores list Object[][] highScores = new Object[10][3]; // We fill the high scores list with blank entries: #. " " 0 for (int i = 0; i < highScores.length; i++) { highScores[i][0] = (i + 1) + "."; highScores[i][1] = " "; highScores[i][2] = 0; } // This actually writes and makes the high scores file try { ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("HighScores.dat")); o.writeObject(highScores); o.close(); } catch (IOException e) { e.printStackTrace(); } } try { // Read and return the read object matrix ObjectInputStream o = new ObjectInputStream(new FileInputStream("HighScores.dat")); Object[][] highScores = (Object[][]) o.readObject(); o.close(); return highScores; } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return null; }
/** Stop talking to the server */ public void stop() { if (socket != null) { // Close all the streams and socket if (out != null) { try { out.close(); } catch (IOException ioe) { } out = null; } if (in != null) { try { in.close(); } catch (IOException ioe) { } in = null; } if (socket != null) { try { socket.close(); } catch (IOException ioe) { } socket = null; } } else { // Already stopped } // Make sure the right buttons are enabled start_button.setEnabled(true); stop_button.setEnabled(false); setStatus(STATUS_STOPPED); }
public void windowClosing(WindowEvent e) // write file on finish { FileOutputStream out = null; ObjectOutputStream data = null; try { // open file for output out = new FileOutputStream(DB); data = new ObjectOutputStream(out); // write Person objects to file using iterator class Iterator<Person> itr = persons.iterator(); while (itr.hasNext()) { data.writeObject((Person) itr.next()); } data.flush(); data.close(); } catch (Exception ex) { JOptionPane.showMessageDialog( objUpdate.this, "Error processing output file" + "\n" + ex.toString(), "Output Error", JOptionPane.ERROR_MESSAGE); } finally { System.exit(0); } }
private void doSave() { ObjectOutputStream objectStream = getObjectOutputStream(); if (objectStream != null) { try { System.out.println("Saving " + selectedChildrenPaths.size() + " Selected Generations..."); for (int i = 0; i < selectedChildrenPaths.size(); i++) { // Get the userObject at the supplied path Object selectedPath = ((TreePath) selectedChildrenPaths.elementAt(i)).getLastPathComponent(); DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selectedPath; objectStream.writeObject(selectedNode.getUserObject()); } objectStream.close(); System.out.println("Save completed successfully."); } catch (IOException e) { System.err.println(e); } } else { System.out.println("Save Selected Files has been aborted!"); } }
// saves the current configuration (breakpoints, window sizes and positions...) private void saveConfig() { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(idxName + ".config")); Breakpoints.save(out); Properties.save(out); out.close(); } catch (IOException exc) { consoleFrame.echoInfo("Could not save settings: " + exc); } }
// Close crap after chatting private void closeCrap() { showMessage("\n Closing connection... \n "); ableToType(false); try { output.close(); input.close(); connection.close(); } catch (IOException ioException) { ioException.printStackTrace(); } }
private void saveObject(Object obj, String fName) { try { FileOutputStream fos = new FileOutputStream(fName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); oos.flush(); oos.close(); fos.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Problemos saugojant Objektus"); } }
private void doSaveAll() { ObjectOutputStream objectStream = getObjectOutputStream(); if (objectStream != null) { try { System.out.println("Saving All " + generations.getLeafCount() + " Generations..."); for (Enumeration e = generations.depthFirstEnumeration(); e.hasMoreElements(); ) { DefaultMutableTreeNode tmpNode = (DefaultMutableTreeNode) e.nextElement(); if (tmpNode.isLeaf()) objectStream.writeObject(tmpNode.getUserObject()); } objectStream.close(); System.out.println("Save completed successfully."); } catch (IOException e) { System.err.println(e); } } else { System.out.println("Save All Files has been aborted!"); } }
public static boolean save() { if (registry.isSaving) { return false; } boolean status = true; registry.isSaving = true; resolutions.add("800x600"); resolutions.add("1024x768"); resolutions.add("1152x864"); resolutions.add("1280x720"); resolutions.add("1280x768"); resolutions.add("1280x800"); resolutions.add("1280x960"); resolutions.add("1280x1024"); resolutions.add("1360x768"); resolutions.add("1366x768"); resolutions.add("1440x900"); resolutions.add("1600x900"); resolutions.add("1600x1024"); resolutions.add("1680x1050"); resolutions.add("1920x1080"); resolutions.add("1920x1200"); // resolutions.add("Full Screen"); // try to save the settings file try { FileOutputStream settingsFile = new FileOutputStream("SettingsTemp.dat"); ObjectOutputStream settings = new ObjectOutputStream(settingsFile); settings.writeObject(new Integer(1)); // settings file version settings.writeObject(new Integer(resolution)); if (volumeMusic == 0) { settings.writeObject(new Integer(-1)); } else { settings.writeObject(new Integer(volumeMusic)); } if (volumeFX == 0) { settings.writeObject(new Integer(-1)); } else { settings.writeObject(new Integer(volumeFX)); } settings.writeObject(new Integer(buttonMoveRight)); settings.writeObject(new Integer(buttonMoveLeft)); settings.writeObject(new Integer(buttonJump)); settings.writeObject(new Integer(buttonAction)); settings.writeObject(new Integer(buttonRobot)); settings.writeObject(new Integer(buttonInventory)); settings.writeObject(new Integer(buttonPause)); settings.close(); moveFile("SettingsTemp.dat", "Settings.dat"); EIError.debugMsg("Saved Settings", EIError.ErrorLevel.Notice); } catch (Exception e) { status = false; EIError.debugMsg("Couldn't save settings " + e.getMessage(), EIError.ErrorLevel.Error); } // try to save the players for (int i = 1; i <= NUMBER_OF_PLAYER_SLOTS; i++) { try { if (player == i - 1) { FileOutputStream playerFile = new FileOutputStream("PlayerTemp.dat"); ObjectOutputStream playerInfo = new ObjectOutputStream(playerFile); playerInfo.writeObject(new Integer(2)); // settings file version playerInfo.writeObject(players.get(i - 1)); if (registry.getGameController().multiplayerMode != GameController.MultiplayerMode.CLIENT && player == i - 1) { blockManagers.set(i - 1, registry.getBlockManager()); placeableManagers.set(i - 1, registry.getPlaceableManager()); monsterManagers.set(i - 1, registry.getMonsterManager()); } playerInfo.writeObject(blockManagers.get(i - 1)); playerInfo.writeObject(placeableManagers.get(i - 1)); playerInfo.writeObject(monsterManagers.get(i - 1)); playerInfo.close(); moveFile("PlayerTemp.dat", "Player" + i + ".dat"); EIError.debugMsg("Saved Player " + i, EIError.ErrorLevel.Notice); } } catch (Exception e) { status = false; EIError.debugMsg( "Couldn't save Player " + i + " " + e.getMessage(), EIError.ErrorLevel.Error); } } registry.isSaving = false; return status; }
public void actionPerformed(ActionEvent e) { if (e.getSource() == jbSaveLayer) { try { FileOutputStream fout = new FileOutputStream(jtfCengMing.getText() + ".wyf"); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(itemArray); oout.close(); fout.close(); } catch (Exception ea) { ea.printStackTrace(); } } else if (e.getSource() == jbLoadLayer) { try { FileInputStream fin = new FileInputStream(jtfCengMing.getText() + ".wyf"); ObjectInputStream oin = new ObjectInputStream(fin); itemArray = (Item[][]) oin.readObject(); oin.close(); fin.close(); this.flush(); } catch (Exception ea) { ea.printStackTrace(); } lvp.repaint(); } else if (e.getSource() == jbLoadAll) { // 全部铺上当前选中 for (int row = 0; row < 40; row++) { for (int col = 0; col < 60; col++) { Item item = ((Item) (jl.getSelectedValue())).clone(); itemArray[row][col] = item; if (item != null) { item.setPosition(col, row); } } } lvp.repaint(); } else if (e.getSource() == jbCreate) { // 生成源代码 try { FileOutputStream fout = null; DataOutputStream dout = null; fout = new FileOutputStream("maps.so"); dout = new DataOutputStream(fout); int totalBlocks = 0; for (int i = 0; i < 40; i++) { for (int j = 0; j < 60; j++) { Item item = itemArray[i][j]; if (item != null) { totalBlocks++; } } } System.out.println("totalBlocks=" + totalBlocks); // 写入不空块的数量 dout.writeInt(totalBlocks); for (int i = 0; i < 40; i++) { for (int j = 0; j < 60; j++) { Item item = itemArray[i][j]; if (item != null) { int w = item.w; // 元素的图片宽度 int h = item.h; // 元素的图片高度 int col = item.col; // 元素的地图列 int row = item.row; // 元素的地图行 int pCol = item.pCol; // 元素的占位列 int pRow = item.pRow; // 元素的占位行 String leiMing = item.leiMing; // 类名 int[][] notIn = item.notIn; // 不可通过 int[][] keYu = item.keYu; // 可遇矩阵 // 计算图片下标 int outBitmapInxex = 0; if (leiMing.equals("Grass")) { outBitmapInxex = 0; } else if (leiMing.equals("XiaoHua1")) { outBitmapInxex = 1; } else if (leiMing.equals("MuZhuang")) { outBitmapInxex = 2; } else if (leiMing.equals("XiaoHua2")) { outBitmapInxex = 3; } else if (leiMing.equals("Road")) { outBitmapInxex = 4; } else if (leiMing.equals("Jing")) { outBitmapInxex = 5; } dout.writeByte(outBitmapInxex); // 记录图片下标 dout.writeByte(0); // 记录可遇标志 0-不可遇 底层都不可遇 dout.writeByte(w); // 图片宽度 dout.writeByte(h); // 图片高度 dout.writeByte(col); // 总列数 dout.writeByte(row); // 总行数 dout.writeByte(pCol); // 占位列 dout.writeByte(pRow); // 占位行 int bktgCount = notIn.length; // 不可通过点的数量 dout.writeByte(bktgCount); // 写入不可通过点的数量 for (int k = 0; k < bktgCount; k++) { dout.writeByte(notIn[k][0]); dout.writeByte(notIn[k][1]); } } } } dout.close(); fout.close(); } catch (Exception ea) { ea.printStackTrace(); } } }
@Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("下棋")) { audience.setEnabled(false); fighter.setEnabled(false); begin.setEnabled(true); // JOptionPane.showMessageDialog(null, "下棋"); System.out.println("下棋"); try { System.out.println("客户端发送下棋指令"); out66.writeObject("对手"); out66.flush(); out66.writeObject(new char[0][0]); out66.flush(); out66.writeObject(new boolean[0][0]); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } } if (e.getActionCommand().equals("观看")) { submit.setEnabled(false); regret.setEnabled(false); audience.setEnabled(false); fighter.setEnabled(false); // JOptionPane.showMessageDialog(null, "观看"); System.out.println("观看"); try { out66.writeObject("观众"); out66.flush(); out66.writeObject(stateList.get(stateList.size() - 1)); out66.flush(); out66.writeObject(takenList.get(takenList.size() - 1)); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } } /*if (e.getActionCommand().equals("人机对弈")) { audience.setEnabled(false); fighter.setEnabled(false); AIPlayer.setEnabled(false); begin.setEnabled(true); JOptionPane.showMessageDialog(null, "人机对弈"); }*/ if (e.getActionCommand().equals("发送")) { // JOptionPane.showMessageDialog(null, "发送"); System.out.println("发送"); String str = myRole + ": " + " " + jt1.getText() + "\n"; try { out99.writeObject(" " + str); out99.flush(); out99.writeObject(new char[8][8]); out99.flush(); out99.writeObject(new boolean[8][8]); out99.flush(); } catch (IOException e1) { e1.printStackTrace(); } jt2.append(str); jt1.setText(""); } if (e.getActionCommand().equals("取消")) { // JOptionPane.showMessageDialog(null, "取消"); System.out.println("取消"); jt1.setText(""); } if (e.getActionCommand().equals("悔棋")) { // JOptionPane.showMessageDialog(null, "悔棋"); System.out.println("悔棋"); try { out66.writeObject("请求悔棋"); out66.flush(); out66.writeObject(new char[8][8]); out66.flush(); out66.writeObject(new boolean[8][8]); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } // RegretChess(); // ShowChessNumber(); } if (e.getActionCommand().equals("退出")) { int quit = JOptionPane.showConfirmDialog(null, "您确定要强制退出吗?", "请确认您的选择", JOptionPane.YES_NO_OPTION); if (quit == JOptionPane.YES_OPTION) { JOptionPane.showMessageDialog(null, "已强制退出"); System.exit(0); } else return; } if (e.getActionCommand().equals("开始")) { begin.setEnabled(false); System.out.println("客户端发送开始指令"); // JOptionPane.showMessageDialog(null, "开始"); try { out66.writeObject("请求开始"); out66.flush(); out66.writeObject(stateList.get(stateList.size() - 1)); out66.flush(); out66.writeObject(takenList.get(takenList.size() - 1)); out66.flush(); } catch (IOException e1) { e1.printStackTrace(); } Begin(); if (kind == "黑") { for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) if (cell[i][j].taken == false) { CheckPlace(cell[i][j]); if (canPut) { cell[i][j].ChangeBackground(); canPut = false; } } } } if (e.getActionCommand().equals("存盘")) { // JOptionPane.showMessageDialog(null, "存盘"); System.out.println("存盘"); try { System.out.println(); out.writeObject(stateList); out.flush(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } } }
// create a new profile: read the script and possibly execute the queries, // save the stats (if 'import' == true, we assume the profile already exists // and don't run the queries) public void createWkld(String name, String scriptFile, boolean runQueries) { System.gc(); Workload wkld = new Workload(name); if (showCmdsItem.getState()) { consoleFrame.echoCmd((!runQueries ? "importprof " : "newwkld ") + name + " " + scriptFile); } // construct the Workload object from the script; // first, check if the file exists try { FileReader reader = new FileReader(scriptFile); reader.close(); } catch (FileNotFoundException e) { System.out.println("couldn't open " + scriptFile); return; } catch (IOException e) { System.out.println("couldn't close " + scriptFile); } // now, check if it contains only queries int scriptId = 0; try { scriptId = Libgist.openScript(scriptFile); } catch (LibgistException e) { System.out.println("couldn't open (C) " + scriptFile); return; } char[] arg1 = new char[64 * 1024]; StringBuffer arg1Buf = new StringBuffer(); char[] arg2 = new char[64 * 1024]; StringBuffer arg2Buf = new StringBuffer(); // for (;;) { // int cmd = Libgist.getCommand(scriptId, arg1, arg2); // if (cmd == Libgist.EOF) break; // if (cmd != Libgist.FETCH) { // there should only be queries // System.out.println("Script file contains non-SELECT command"); // return; // } // } if (runQueries) { // turn profiling on and execute queries // Libgist.setProfilingEnabled(true); Libgist.disableBps(true); // we don't want to stop at breakpoints // rescan queries try { scriptId = Libgist.openScript(scriptFile); } catch (LibgistException e) { System.out.println("couldn't open (C) " + scriptFile); return; } int cnt = 1; // for (;;) { // int cmd = Libgist.getCommand(scriptId, arg1, arg2); // if (cmd == Libgist.EOF) break; // arg1Buf.setLength(0); // arg1Buf.append(arg1, 0, strlen(arg1)); // arg2Buf.setLength(0); // arg2Buf.append(arg2, 0, strlen(arg2)); // OpThread.execCmd(LibgistCommand.FETCH, arg1Buf.toString(), // arg2Buf.toString(), false); // System.out.print(cnt + " "); // System.out.println(cnt + ": execute " + arg2Buf.toString() + " " // + arg1Buf.toString()); // cnt++; // } System.out.println(); Libgist.disableBps(false); // compute optimal clustering and some more statistics // Libgist.computeMetrics(wkld.filename); } // save profile try { // we're saving Java and C++ data in separate files (filename and filename.prof) // the profile object only contains the filename, the queries will be // read in from the file when the profile is opened (faster that way) ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(wkld.filename)); out.writeObject(wkld); out.close(); System.out.println("copy query file"); Runtime.getRuntime().exec("cp " + scriptFile + " " + wkld.filename + ".queries"); System.out.println("saving tree and profile"); Libgist.saveToFile(wkld.filename + ".idx"); if (runQueries) { // Libgist.saveProfile(wkld.filename + ".prof"); } } catch (Exception e) { System.out.println("Error saving profile: " + e); return; } if (runQueries) { // turn profiling off (after the metrics were computed and // the profile saved) // Libgist.setProfilingEnabled(false); } }
public void actionPerformed(ActionEvent e) { System.out.println("actionPerformed"); if (e.getSource() == pen) // 画笔 { System.out.println("pen"); toolFlag = 0; } if (e.getSource() == eraser) // 橡皮 { System.out.println("eraser"); toolFlag = 1; } if (e.getSource() == clear) // 清除 { System.out.println("clear"); toolFlag = 2; paintInfo.removeAllElements(); repaint(); } if (e.getSource() == drLine) // 画线 { System.out.println("drLine"); toolFlag = 3; } if (e.getSource() == drCircle) // 画圆 { System.out.println("drCircle"); toolFlag = 4; } if (e.getSource() == drRect) // 画矩形 { System.out.println("drRect"); toolFlag = 5; } if (e.getSource() == colchooser) // 调色板 { System.out.println("colchooser"); Color newColor = JColorChooser.showDialog(this, "我的调色板", c); c = newColor; } if (e.getSource() == openPic) // 打开图画 { openPicture.setVisible(true); if (openPicture.getFile() != null) { int tempflag; tempflag = toolFlag; toolFlag = 2; repaint(); try { paintInfo.removeAllElements(); File filein = new File(openPicture.getDirectory(), openPicture.getFile()); picIn = new FileInputStream(filein); VIn = new ObjectInputStream(picIn); paintInfo = (Vector) VIn.readObject(); VIn.close(); repaint(); toolFlag = tempflag; } catch (ClassNotFoundException IOe2) { repaint(); toolFlag = tempflag; System.out.println("can not read object"); } catch (IOException IOe) { repaint(); toolFlag = tempflag; System.out.println("can not read file"); } } } if (e.getSource() == savePic) // 保存图画 { savePicture.setVisible(true); try { File fileout = new File(savePicture.getDirectory(), savePicture.getFile()); picOut = new FileOutputStream(fileout); VOut = new ObjectOutputStream(picOut); VOut.writeObject(paintInfo); VOut.close(); } catch (IOException IOe) { System.out.println("can not write object"); } } }