001: /*
002: * $RCSfile: Positions.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:37 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.four_by_four;
046:
047: import java.applet.Applet;
048: import java.awt.event.*;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.util.BitSet;
052: import com.sun.j3d.utils.geometry.Sphere;
053:
054: /**
055: * Class: Positions
056: *
057: * Description: Creates the position markers.
058: *
059: * Version: 1.0
060: *
061: */
062: public class Positions extends Object {
063:
064: final static int UNOCCUPIED = 0;
065: final static int HUMAN = 1;
066: final static int MACHINE = 2;
067: final static int END = 3;
068:
069: private Vector3f point[];
070: private Switch posSwitch;
071: private Switch humanSwitch;
072: private Switch machineSwitch;
073: private BitSet posMask;
074: private BitSet humanMask;
075: private BitSet machineMask;
076: private Group group;
077: private Material redMat;
078: private Material blueMat;
079: private Material yellowMat;
080: private Material whiteMat;
081: private Appearance redApp;
082: private Appearance blueApp;
083: private Appearance yellowApp;
084: private Appearance whiteApp;
085: private Board board;
086: private Sphere posSphere[];
087: private BigCube cube[];
088: private TransformGroup tgroup;
089: private boolean winnerFlag = false;
090:
091: public Positions() {
092:
093: // Define colors for lighting
094: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
095: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
096: Color3f red = new Color3f(0.9f, 0.1f, 0.2f);
097: Color3f blue = new Color3f(0.3f, 0.3f, 0.8f);
098: Color3f yellow = new Color3f(1.0f, 1.0f, 0.0f);
099: Color3f ambRed = new Color3f(0.3f, 0.03f, 0.03f);
100: Color3f ambBlue = new Color3f(0.03f, 0.03f, 0.3f);
101: Color3f ambYellow = new Color3f(0.3f, 0.3f, 0.03f);
102: Color3f ambWhite = new Color3f(0.3f, 0.3f, 0.3f);
103: Color3f specular = new Color3f(1.0f, 1.0f, 1.0f);
104:
105: // Create the red appearance node
106: redMat = new Material(ambRed, black, red, specular, 100.f);
107: redMat.setLightingEnable(true);
108: redApp = new Appearance();
109: redApp.setMaterial(redMat);
110:
111: // Create the blue appearance node
112: blueMat = new Material(ambBlue, black, blue, specular, 100.f);
113: blueMat.setLightingEnable(true);
114: blueApp = new Appearance();
115: blueApp.setMaterial(blueMat);
116:
117: // Create the yellow appearance node
118: yellowMat = new Material(ambYellow, black, yellow, specular,
119: 100.f);
120: yellowMat.setLightingEnable(true);
121: yellowApp = new Appearance();
122: yellowApp.setMaterial(yellowMat);
123:
124: // Create the white appearance node
125: whiteMat = new Material(ambWhite, black, white, specular, 100.f);
126: whiteMat.setLightingEnable(true);
127: whiteApp = new Appearance();
128: whiteApp.setMaterial(whiteMat);
129:
130: // Load the point array with the offset (coordinates) for each of
131: // the 64 positions.
132: point = new Vector3f[64];
133: int count = 0;
134: for (int i = -30; i < 40; i += 20) {
135: for (int j = -30; j < 40; j += 20) {
136: for (int k = -30; k < 40; k += 20) {
137: point[count] = new Vector3f((float) k, (float) j,
138: (float) i);
139: count++;
140: }
141: }
142: }
143:
144: // Create the switch nodes
145: posSwitch = new Switch(Switch.CHILD_MASK);
146: humanSwitch = new Switch(Switch.CHILD_MASK);
147: machineSwitch = new Switch(Switch.CHILD_MASK);
148:
149: // Set the capability bits
150: posSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
151: posSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
152:
153: humanSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
154: humanSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
155:
156: machineSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
157: machineSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
158:
159: // Create the bit masks
160: posMask = new BitSet();
161: humanMask = new BitSet();
162: machineMask = new BitSet();
163:
164: // Create the small white spheres that mark unoccupied
165: // positions.
166: posSphere = new Sphere[64];
167: for (int i = 0; i < 64; i++) {
168: Transform3D transform3D = new Transform3D();
169: transform3D.set(point[i]);
170: TransformGroup transformGroup = new TransformGroup(
171: transform3D);
172: posSphere[i] = new Sphere(2.0f, Sphere.GENERATE_NORMALS
173: | Sphere.ENABLE_APPEARANCE_MODIFY, 12, whiteApp);
174: Shape3D shape = posSphere[i].getShape();
175: ID id = new ID(i);
176: shape.setUserData(id);
177: transformGroup.addChild(posSphere[i]);
178: posSwitch.addChild(transformGroup);
179: posMask.set(i);
180: }
181:
182: // Create the red spheres that mark the user's positions.
183: for (int i = 0; i < 64; i++) {
184: Transform3D transform3D = new Transform3D();
185: transform3D.set(point[i]);
186: TransformGroup transformGroup = new TransformGroup(
187: transform3D);
188: transformGroup.addChild(new Sphere(7.0f, redApp));
189: humanSwitch.addChild(transformGroup);
190: humanMask.clear(i);
191: }
192:
193: // Create the blue cubes that mark the computer's positions.
194: for (int i = 0; i < 64; i++) {
195: Transform3D transform3D = new Transform3D();
196: transform3D.set(point[i]);
197: TransformGroup transformGroup = new TransformGroup(
198: transform3D);
199: BigCube cube = new BigCube(blueApp);
200: transformGroup.addChild(cube.getChild());
201: machineSwitch.addChild(transformGroup);
202: machineMask.clear(i);
203: }
204:
205: // Set the positions mask
206: posSwitch.setChildMask(posMask);
207: humanSwitch.setChildMask(humanMask);
208: machineSwitch.setChildMask(machineMask);
209:
210: // Throw everything into a single group
211: group = new Group();
212: group.addChild(posSwitch);
213: group.addChild(humanSwitch);
214: group.addChild(machineSwitch);
215: }
216:
217: public void setTransformGroup(TransformGroup transformGroup) {
218: tgroup = transformGroup;
219: }
220:
221: public Group getChild() {
222: return group;
223: }
224:
225: public void setBoard(Board board) {
226: this .board = board;
227: }
228:
229: public void winner() {
230: winnerFlag = true;
231: }
232:
233: public void noWinner() {
234: winnerFlag = false;
235: }
236:
237: public void setHighlight(int pos) {
238: posSphere[pos].setAppearance(yellowApp);
239: }
240:
241: public void clearHighlight(int pos) {
242: posSphere[pos].setAppearance(whiteApp);
243: }
244:
245: public void newGame() {
246:
247: // Clear the board
248: for (int i = 0; i < 64; i++) {
249: posMask.set(i);
250: humanMask.clear(i);
251: machineMask.clear(i);
252: }
253: posSwitch.setChildMask(posMask);
254: humanSwitch.setChildMask(humanMask);
255: machineSwitch.setChildMask(machineMask);
256:
257: // The following three lines fix a bug in J3D
258: Transform3D t = new Transform3D();
259: tgroup.getTransform(t);
260: tgroup.setTransform(t);
261:
262: // Reset the winner flag
263: winnerFlag = false;
264: }
265:
266: public void set(int pos, int player) {
267:
268: // Stop accepting selections when the game
269: // is over.
270: if (winnerFlag)
271: return;
272:
273: // Make sure the position is not occupied.
274: if (player == HUMAN)
275: if (!board.unoccupied(pos))
276: return;
277:
278: // Turn off the position marker for the given position
279: posMask.clear(pos);
280: posSwitch.setChildMask(posMask);
281:
282: // Turn on the player marker
283: if (player == Positions.HUMAN) {
284: humanMask.set(pos);
285: humanSwitch.setChildMask(humanMask);
286: board.selection(pos, Positions.HUMAN);
287: } else {
288: machineMask.set(pos);
289: machineSwitch.setChildMask(machineMask);
290: }
291:
292: // The following three lines fix a bug in J3D
293: Transform3D t = new Transform3D();
294: tgroup.getTransform(t);
295: tgroup.setTransform(t);
296: }
297:
298: public void clear(int pos) {
299:
300: // Turn on the position marker
301: posMask.set(pos);
302: posSwitch.setChildMask(posMask);
303:
304: // Turn off the player marker
305: humanMask.clear(pos);
306: humanSwitch.setChildMask(humanMask);
307: machineMask.clear(pos);
308: machineSwitch.setChildMask(machineMask);
309:
310: // The following three lines are a workaround for a bug
311: // in dev09 in which the transform3D of certain items are
312: // not updated properly. Scheduled to be fixed in dev10
313: Transform3D t = new Transform3D();
314: tgroup.getTransform(t);
315: tgroup.setTransform(t);
316: }
317:
318: }
|