📄 fourbyfour.java
字号:
/* * $RCSfile: FourByFour.java,v $ * * Copyright (c) 2006 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.3 $ * $Date: 2006/03/07 00:13:10 $ * $State: Exp $ */package org.jdesktop.j3d.examples.four_by_four;import java.applet.Applet;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.applet.MainFrame;import org.jdesktop.j3d.examples.Resources;/** * Class FourByFour * * Description: High level class for the game FourByFour * * Version: 1.2 * */public class FourByFour extends Applet implements ActionListener { // To write scores to scores file private static final boolean writeScoresFile = false; String host; // Host from which this applet came from int port; // Port number for writing high scores Image backbuffer2D; // Backbuffer image used for 2D double buffering int width, height; // Size of the graphics window in pixels int score; // Final game score int level_weight; // Weighting factor for skill level int move_weight; // Weighting factor for number of moves to win int time_weight; // Weighting factor for amount of time it took to win int skill_level; // Skill level, 0 - 4 Canvas2D canvas2D; // 2D rendering canvas Canvas3D canvas3D; // 3D rendering canvas Board board; // Game board object Panel b_container; // Container to hold the buttons Panel c_container; // Container to hold the canvas Panel l_container; // Container to hold the labels Panel skill_panel; // Panel to hold skill levels Panel instruct_panel; // Panel to hold instructions Panel winner_panel; // Panel to hold winner announcement Panel high_panel; // Panel to hold high scores Button instruct_button; // Instructions button Button new_button; // New Game button Button skill_button; // Skill Level button Button high_button; // High Scores button Button undo_button; // Undo Move button Label skill_label; // Label on skill panel Label winner_label; // Label on winner panel Label winner_score_label; // Score label on winner panel Label winner_name_label; // Name label on winner panel Label winner_top_label; // Top 20 label on winner panel Label high_label; // High score label Label high_places[]; // Labels to hold places Label high_names[]; // Labels to hold names Label high_scores[]; // Labels to hold scores TextArea instruct_text; // TextArea object that holds instructions TextArea high_text; // TextArea object that holds top 20 scores TextField winner_name; // TextField object that holds winner's name Button instruct_return_button; // Return button for instruction panel Button skill_return_button; // Return button for skill level panel Button winner_return_button; // Return button for winner panel Button high_return_button; // Return button for high scores panel CheckboxGroup group; // CheckboxGroup object for skill level panel InputStream inStream; // Input stream for reading instructions and high scores static boolean appletFlag = true; // Applet flag boolean winner_flag = false; // Winner flag byte text[]; // Temporary storage area for reading instructions file byte outText[]; // Temporary storage area for writing high scores file String textString; // Storage area for instructions String scoresString; // String used for writing high scores file int places[]; // Storage area for high score places int scores[]; // Storage area for high score scores String names[]; // Storage area for high score names Positions positions; // Positions object, used to render player positions private SimpleUniverse universe = null; /** * Initialization */ public void init() { // Set the port number. port = 4111; // Set the graphics window size. width = 350; height = 350; // Set the weighting factors used for scoring. level_weight = 1311; move_weight = 111; time_weight = 1000; // Create the "base" color for the AWT components. setBackground(new Color(200, 200, 200)); // Read the instructions file. if (appletFlag) { // Get the host from which this applet came. host = getCodeBase().getHost(); try { URL instrURL = Resources.getResource("four_by_four/instructions.txt"); inStream = new BufferedInputStream((instrURL).openStream(), 8192); text = new byte[5000]; int character = inStream.read(); int count = 0; while (character != -1) { text[count++] = (byte) character; character = inStream.read(); } textString = new String(text); inStream.close(); } catch(Exception e) { System.out.println("Error: " + e.toString()); } } else { try { URL instrURL = Resources.getResource("four_by_four/instructions.txt"); inStream = new BufferedInputStream((instrURL).openStream(), 8192); text = new byte[5000]; int character = inStream.read(); int count = 0; while (character != -1) { text[count++] = (byte) character; character = inStream.read(); } textString = new String(text); inStream.close(); } catch(Exception e) { System.out.println("Error: " + e.toString()); } } // Read the high-scores file. places = new int[20]; scores = new int[20]; names = new String[20]; 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 e) { System.out.println("Error: " + e.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 e) { System.out.println("Error: " + e.toString()); } } // The positions object sets up the switch nodes which // control the rendering of the player's positions. positions = new Positions(); // Create the game board object which is responsible // for keeping track of the moves on the game board // and determining what move the computer should make. board = new Board(this, positions, width, height); positions.setBoard(board); // Create a 2D graphics canvas. canvas2D = new Canvas2D(board); canvas2D.setSize(width, height); canvas2D.setLocation(width+10, 5); canvas2D.addMouseListener(canvas2D); board.setCanvas(canvas2D); // Create the 2D backbuffer backbuffer2D = createImage(width, height); canvas2D.setBuffer(backbuffer2D); // Create a 3D graphics canvas. canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); canvas3D.setSize(width, height); canvas3D.setLocation(5, 5); // Create the scene branchgroup. BranchGroup scene3D = createScene3D();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -