📄 fourbyfour.java
字号:
// Let the positions object know about the transform group positions.setTransformGroup(transformGroup); // Create the mouse pick and drag behavior node PickDragBehavior behavior = new PickDragBehavior(canvas2D, canvas3D, positions, branchGroup, transformGroup); behavior.setSchedulingBounds(bounds); transformGroup.addChild(behavior); return branchGroup; } public void actionPerformed (ActionEvent event) { Object target = event.getSource(); // Process the button events. if (target == skill_return_button) { skill_panel.setVisible(false); skill_return_button.setVisible(false); c_container.setVisible(true); b_container.setVisible(true); newGame(); } else if (target == winner_return_button) { if (winner_flag) { String name = winner_name.getText(); String tmp_name = new String(""); int tmp_score = 0; boolean insert_flag = false; winner_flag = false; for (int i=0; i<20; i++) { if (insert_flag) { name = names[i]; score = scores[i]; names[i] = tmp_name; scores[i] = tmp_score; tmp_name = name; tmp_score = score; } if (!insert_flag && score > scores[i]) { tmp_name = names[i]; tmp_score = scores[i]; scores[i] = score; names[i] = name; insert_flag = true; } high_names[i].setText(names[i]); high_scores[i].setText(Integer.toString(scores[i])); } scoresString = new String(""); int place; for (int i=0; i<20; i++) { place = (int) places[i]; scoresString += Integer.toString(place); scoresString += "\t"; scoresString += names[i]; scoresString += " "; scoresString += Integer.toString(scores[i]); scoresString += "\n"; } if(writeScoresFile) { if (appletFlag) { try { OutputStreamWriter outFile = new OutputStreamWriter(new FileOutputStream("scores.txt")); outFile.write(scoresString); outFile.flush(); outFile.close(); outFile = null; } catch (IOException ioe) { System.out.println("Error: " + ioe.toString()); } catch (Exception e) { System.out.println("Error: " + e.toString()); } } else { try { OutputStreamWriter outFile = new OutputStreamWriter(new FileOutputStream("scores.txt")); outFile.write(scoresString); outFile.flush(); outFile.close(); outFile = null; } catch (IOException ioe) { System.out.println("Error: " + ioe.toString()); } } } } winner_panel.setVisible(false); winner_return_button.setVisible(false); winner_label.setVisible(false); winner_score_label.setVisible(false); winner_name_label.setVisible(false); winner_top_label.setVisible(false); winner_name.setVisible(false); c_container.setVisible(true); b_container.setVisible(true); } else if (target == high_return_button) { high_return_button.setVisible(false); high_panel.setVisible(false); c_container.setVisible(true); b_container.setVisible(true); } else if (target == instruct_return_button) { instruct_text.setVisible(false); instruct_return_button.setVisible(false); instruct_text.repaint(); c_container.setVisible(true); b_container.setVisible(true); } else if (target == undo_button) { board.undo_move(); canvas2D.repaint(); } else if (target == instruct_button) { c_container.setVisible(false); b_container.setVisible(false); instruct_text.setVisible(true); instruct_return_button.setVisible(true); } else if (target == new_button) { newGame(); } else if (target == skill_button) { c_container.setVisible(false); b_container.setVisible(false); skill_panel.setVisible(true); skill_return_button.setVisible(true); } else if (target == high_button) { // Read the high scores file. if (appletFlag) { try { URL scoreURL = Resources.getResource("four_by_four/scores.txt"); inStream = new BufferedInputStream(scoreURL.openStream(), 8192); Reader read = new BufferedReader(new InputStreamReader(inStream)); StreamTokenizer st = new StreamTokenizer(read); st.whitespaceChars(32,44); st.eolIsSignificant(false); int count = 0; int token = st.nextToken(); boolean scoreFlag = true; String string; while (count<20) { places[count] = (int) st.nval; string = new String(""); token = st.nextToken(); while (token == StreamTokenizer.TT_WORD) { string += st.sval; string += " "; token = st.nextToken(); } names[count] = string; scores[count] = (int) st.nval; token = st.nextToken(); count++; } inStream.close(); } catch(Exception ioe) { System.out.println("Error: " + ioe.toString()); } } else { try { URL scoreURL = Resources.getResource("four_by_four/scores.txt"); inStream = new BufferedInputStream(scoreURL.openStream(), 8192); Reader read = new BufferedReader(new InputStreamReader(inStream)); StreamTokenizer st = new StreamTokenizer(read); st.whitespaceChars(32,44); st.eolIsSignificant(false); int count = 0; int token = st.nextToken(); boolean scoreFlag = true; String string; while (count<20) { places[count] = (int) st.nval; string = new String(""); token = st.nextToken(); while (token == StreamTokenizer.TT_WORD) { string += st.sval; string += " "; token = st.nextToken(); } names[count] = string; scores[count] = (int) st.nval; token = st.nextToken(); count++; } inStream.close(); } catch(Exception ioe) { System.out.println("Error: " + ioe.toString()); } } c_container.setVisible(false); b_container.setVisible(false); high_panel.setVisible(true); high_return_button.setVisible(true); } Checkbox box = group.getSelectedCheckbox(); String label = box.getLabel(); if (label.equals("Babe in the Woods ")) { board.set_skill_level(0); } else if (label.equals("Walk and Chew Gum ")) { board.set_skill_level(1); } else if (label.equals("Jeopardy Contestant ")) { board.set_skill_level(2); } else if (label.equals("Rocket Scientist ")) { board.set_skill_level(3); } else if (label.equals("Be afraid, be very afraid")) { board.set_skill_level(4); } } public void newGame() { board.newGame(); canvas2D.repaint(); } public void start() { if (appletFlag) showStatus("FourByFour"); } public void winner(int player, int level, int nmoves, long time) { if (player == 1) { score = level * level_weight + (66 - nmoves) * move_weight - (int) Math.min(time * time_weight, 5000); winner_label.setText("Game over, you win!"); winner_label.setLocation(290, 90); winner_score_label.setText("Score = " + score); winner_score_label.setVisible(true); winner_score_label.setLocation(315, 120); if (score > scores[19]) { winner_name_label.setVisible(true); winner_top_label.setVisible(true); winner_name.setVisible(true); winner_flag = true; } } else { winner_label.setText("Game over, the computer wins!"); winner_label.setLocation(250, 150); } c_container.setVisible(false); b_container.setVisible(false); winner_panel.setVisible(true); winner_label.setVisible(true); winner_return_button.setVisible(true); repaint(); } /** * Inner class used to "kill" the window when running as * an application. */ static class killAdapter extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } } /** * Main method, only used when running as an application. */ public static void main(String[] args) { FourByFour.appletFlag = false; new MainFrame(new FourByFour(), 730, 450); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -