package Enterprise_Astroids;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.media.Manager;
import javax.media.Player;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.PointArray;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import TransoForm.TransoFormGroup;
import com.indie.genesis.j3d.Rock;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
@SuppressWarnings("serial")
public class Game extends JFrame implements KeyListener {
Toolkit tk;
@SuppressWarnings("static-access")
Dimension d = tk.getDefaultToolkit().getScreenSize();
JProgressBar bar;
JFrame console = new JFrame();
JTextArea area = new JTextArea();
SimpleUniverse u;
Canvas3D canvas3D;
Player m_Player;
boolean right = true;
boolean left = true;
boolean StarTrek = false;
boolean StarWars = true;
boolean off = false;
TransformGroup Charakter;
Transform3D charakter;
Vector3f cvector;
float cx = 0;
float cy = -0.8f;
float cz = -4;
float ry = 180;
TransformGroup View;
Transform3D view;
Vector3f vector;
float x = 0;
float y = 0;
float z = 0;
class WindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
@SuppressWarnings( { "static-access", "deprecation" })
public Game() {
super("Start Trek - Astroids");
console.setTitle("RoutenProtokollant");
console.setSize(300, 400);
JMenuBar cop = new JMenuBar();
JMenu copy = new JMenu("Copy");
cop.add(copy);
JMenuItem copyy = new JMenuItem("Route kopieren");
copy.add(copyy);
copyy.setBackground(Color.black);
cop.setBackground(Color.black);
copyy.setForeground(Color.green);
copy.setForeground(Color.green);
copyy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt) {
area.selectAll();
area.copy();
area.select(0, 0);
}
});
JMenu Exit = new JMenu("Exit");
cop.add(Exit);
Exit.setForeground(Color.green);
JMenuItem exit = new JMenuItem("Exit");
Exit.add(exit);
exit.setBackground(Color.black);
exit.setForeground(Color.green);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt) {
System.exit(0);
}
});
JMenuItem close = new JMenuItem("close");
Exit.add(close);
close.setBackground(Color.black);
close.setForeground(Color.green);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt) {
console.setVisible(false);
}
});
console.setJMenuBar(cop);
area = new JTextArea();
area.setBackground(Color.black);
area.setForeground(Color.green);
area.setText("RoutenProtokollant" + "\n");
JScrollPane slider = new JScrollPane(area);
console.setLayout(new GridLayout(1, 1));
console.add(slider);
slider.setBackground(Color.green);
slider.setForeground(Color.black);
try {
m_Player = Manager.createPlayer(new File(
"F:/JExcercises/StarTrek/StarWars.mp3").toURL());
m_Player.start();
} catch (Exception e) {
e.printStackTrace();
}
this.setUndecorated(true);
Toolkit tk = null;
Dimension d = tk.getDefaultToolkit().getScreenSize();
this.setSize(d);
this.addWindowListener(new WindowListener());
JMenuBar b = new JMenuBar();
bar = new JProgressBar();
bar.setValue(50);
b.add(bar);
final Button Sound = new Button("StarWars");
b.add(Sound);
Sound.setBackground(Color.black);
Sound.setForeground(Color.green);
Sound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt) {
if (StarTrek == true) {
StarTrek = false;
StarWars = false;
off = true;
Sound.setLabel("Off");
m_Player.stop();
} else if (StarWars == true) {
StarTrek = true;
StarWars = false;
off = false;
Sound.setLabel("StarTrek");
m_Player.stop();
try {
m_Player = Manager.createPlayer(new File(
"F:/JExcercises/StarTrek/Enterprise.wav")
.toURL());
m_Player.start();
} catch (Exception e) {
e.printStackTrace();
}
} else if (off == true) {
StarTrek = false;
StarWars = true;
off = false;
Sound.setLabel("StarWars");
m_Player.stop();
try {
m_Player = Manager
.createPlayer(new File(
"F:/JExcercises/StarTrek/StarWars.mp3")
.toURL());
m_Player.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
this.setJMenuBar(b);
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
canvas3D.addKeyListener(this);
canvas3D.setBounds(0, 0, d.width, d.height);
this.add(canvas3D);
u = new SimpleUniverse(canvas3D);
BranchGroup scene = createSceneGraph();
u.addBranchGraph(scene);
this.setVisible(true);
this.setLayout(null);
}
public BranchGroup createSceneGraph() {
final BranchGroup objRoot = new BranchGroup();
createStarBackground(objRoot);
TransoFormGroup f1 = new TransoFormGroup(new Vector3f(1, -0.8f, -50),
20, 20, 0, 0.1f);
f1.addChild(new Rock());
objRoot.addChild(f1);
TransoFormGroup f2 = new TransoFormGroup(new Vector3f(0, -0.6f, -70),
0, 20, 20, 0.1f);
f2.addChild(new Rock());
objRoot.addChild(f2);
// //////////////////// //////////////////////////
charakter = new Transform3D();
cvector = new Vector3f();
cvector.setX(cx);
cvector.setY(cy);
cvector.setZ(cz);
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter = new TransformGroup();
Charakter.setTransform(charakter);
Charakter.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Charakter.addChild(new EnterPrise(0.5f));
objRoot.addChild(Charakter);
view = new Transform3D();
vector = new Vector3f();
vector.setX(x);
vector.setY(y);
vector.setZ(z);
view.setTranslation(vector);
View = new TransformGroup();
View.setTransform(view);
View.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
View = u.getViewingPlatform().getViewPlatformTransform();
Appearance app = new Appearance();
app.setMaterial(new Material(new Color3f(0.1f, 0.1f, 0.1f),
new Color3f(0f, 0f, 0f), new Color3f(0.8f, 0.8f, 0.8f),
new Color3f(0.6f, 0.6f, 0.6f), 1f));
app
.setPolygonAttributes(new PolygonAttributes(
PolygonAttributes.POLYGON_LINE,
PolygonAttributes.CULL_NONE, 0));
TransoFormGroup b1 = new TransoFormGroup(new Vector3f(0, -0.7f, -0), 0,
0, 0);
objRoot.addChild(b1);
b1.addChild(new Box(5, 5, 70, app));
TransoFormGroup b2 = new TransoFormGroup(new Vector3f(0, -0.7f, -100),
0, 0, 0);
objRoot.addChild(b2);
b2.addChild(new Box(5, 5, 70, app));
TransoFormGroup b3 = new TransoFormGroup(new Vector3f(0, -0.7f, -200),
0, 0, 0);
objRoot.addChild(b3);
b3.addChild(new Box(5, 5, 70, app));
TransoFormGroup b4 = new TransoFormGroup(new Vector3f(0, -0.7f, -300),
0, 0, 0);
objRoot.addChild(b4);
b4.addChild(new Box(5, 5, 70, app));
TransoFormGroup b5 = new TransoFormGroup(new Vector3f(0, -0.7f, -400),
0, 0, 0);
objRoot.addChild(b5);
b5.addChild(new Box(5, 5, 70, app));
TransoFormGroup b6 = new TransoFormGroup(new Vector3f(0, -0.7f, -500),
0, 0, 0);
objRoot.addChild(b6);
b6.addChild(new Box(5, 5, 70, app));
TransoFormGroup b7 = new TransoFormGroup(new Vector3f(0, -0.7f, -600),
0, 0, 0);
objRoot.addChild(b7);
b7.addChild(new Box(5, 5, 70, app));
TransoFormGroup b8 = new TransoFormGroup(new Vector3f(0, -0.7f, -700),
0, 0, 0);
objRoot.addChild(b8);
b8.addChild(new Box(5, 5, 70, app));
//
TransoFormGroup c1 = new TransoFormGroup(new Vector3f(0, -0.7f, -00),
90, 0, 0);
objRoot.addChild(c1);
c1.addChild(new Cylinder(5, 70, app));
TransoFormGroup c2 = new TransoFormGroup(new Vector3f(0, -0.7f, -100),
90, 0, 0);
objRoot.addChild(c2);
c2.addChild(new Cylinder(5, 70, app));
TransoFormGroup c3 = new TransoFormGroup(new Vector3f(0, -0.7f, -200),
90, 0, 0);
objRoot.addChild(c3);
c3.addChild(new Cylinder(5, 70, app));
TransoFormGroup c4 = new TransoFormGroup(new Vector3f(0, -0.7f, -300),
90, 0, 0);
objRoot.addChild(c4);
c4.addChild(new Cylinder(5, 70, app));
TransoFormGroup c5 = new TransoFormGroup(new Vector3f(0, -0.7f, -400),
90, 0, 0);
objRoot.addChild(c5);
c5.addChild(new Cylinder(5, 70, app));
TransoFormGroup c6 = new TransoFormGroup(new Vector3f(0, -0.7f, -500),
90, 0, 0);
objRoot.addChild(c6);
c6.addChild(new Cylinder(5, 70, app));
TransoFormGroup c7 = new TransoFormGroup(new Vector3f(0, -0.7f, -600),
90, 0, 0);
objRoot.addChild(c7);
c7.addChild(new Cylinder(5, 70, app));
TransoFormGroup c8 = new TransoFormGroup(new Vector3f(0, -0.7f, -700),
90, 0, 0);
objRoot.addChild(c8);
c8.addChild(new Cylinder(5, 70, app));
Appearance App = new Appearance();
App.setTexture((new TextureLoader("F:/JExcercises/StarTrek/Ziel.jpg",
null)).getTexture());
App.setTextureAttributes(new TextureAttributes(
TextureAttributes.MODULATE, new Transform3D(), new Color4f(),
TextureAttributes.FASTEST));
TransoFormGroup ziel = new TransoFormGroup(new Vector3f(0, -0.7f,
-800.25f), 0, 0, 0);
objRoot.addChild(ziel);
ziel.addChild(new Box(5, 0.5f, 0.5f, Box.GENERATE_NORMALS
| Box.GENERATE_TEXTURE_COORDS, App));
(new Thread() {
@SuppressWarnings("deprecation")
@Override
public void run() {
while (!isInterrupted()) {
// //////////////////////////////////////////////
// Abfragen des Ziels
if (cz <= -800) {
setVisible(false);
console.setVisible(true);
this.stop();
}
// das Bewegen
// grundsätlich geradeaus
z = z - 0.1f;
cz = cz - 0.1f;
vector.setX(x);
vector.setY(y);
vector.setZ(z);
view.setTranslation(vector);
View.setTransform(view);
cvector.setX(cx);
cvector.setY(cy);
cvector.setZ(cz);
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
if (ry <= 170) {
right = false;
} else if (ry >= 190) {
left = false;
} else {
right = true;
left = true;
}
System.out.println(cvector);
area.select(area.getText().length() - 1, area.getText()
.length());
area.append("\n" + vector);
// Collisions Detection
boolean brok1 = false;
boolean brok2 = false;
// Boolean convertings
// broken1
if (cz <= -49) {
brok1 = true;
} else if (cz >= -51) {
brok1 = false;
}
// broken2
else if (cz <= -69) {
brok2 = true;
} else if (cz >= -71) {
brok2 = false;
}
// Asking for vectors
if (cy <= -0.8f && cx >= 1f && brok1 == true) {
System.exit(0);
} else if (cy <= -0.8f && cx >= -0.1f && brok2 == true) {
System.exit(0);
}
// //////////////////////////////////////////////
try {
sleep(10);
} catch (InterruptedException e) {
interrupt();
}
}
}
}).start();
Color3f lightColor = new Color3f(.5f, .6f, .6f);
AmbientLight ambientLight = new AmbientLight(lightColor);
ambientLight.setInfluencingBounds(new BoundingSphere(new Point3d(),
100000000000000000000000000000000000000000000.0));
objRoot.addChild(ambientLight);
DirectionalLight directionalLight = new DirectionalLight();
directionalLight.setColor(lightColor);
directionalLight.setInfluencingBounds(new BoundingSphere(new Point3d(),
100000000000000000000000000000000000000000000.0));
objRoot.addChild(directionalLight);
objRoot.compile();
return objRoot;
}
public void createStarBackground(BranchGroup bg) {
java.util.Random rand = new java.util.Random();
float mag;
BranchGroup BGBranch = new BranchGroup();
Background BG = new Background();
BG.setColor(new Color3f(0, 0, 0.4f));
PointArray starfield = new PointArray(15000, PointArray.COORDINATES
| PointArray.COLOR_3);
float[] point = new float[3];
float[] brightness = new float[3];
for (int i = 0; i < 15000; i++) {
point[0] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f : rand
.nextFloat();
point[1] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f : rand
.nextFloat();
point[2] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f : rand
.nextFloat();
starfield.setCoordinate(i, point);
mag = (rand.nextFloat() + 1.5f) / 1.5f;
brightness[0] = mag;
brightness[1] = mag;
brightness[2] = mag;
starfield.setColor(i, brightness);
}
Shape3D StarShape = new Shape3D(starfield);
StarShape.setAppearance(new Appearance());
StarShape.getAppearance().setPointAttributes(
new PointAttributes(1f, true));
BGBranch.addChild(StarShape);
BG.setGeometry(BGBranch);
BG.setApplicationBounds(new BoundingSphere(new Point3d(),
100000000000000000000000000000000000000000000.0));
bg.addChild(BG);
}
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if (cx <= -5) {
;
} else {
x = x - 0.1f;
cx = cx - 0.1f;
vector.setX(x);
vector.setZ(z);
view.setTranslation(vector);
View.setTransform(view);
cvector.setX(cx);
cvector.setZ(cz);
if (left == true) {
ry = ry + 1;
}
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
}
} else if (arg0.getKeyCode() == KeyEvent.VK_UP) {
if (cy >= 5) {
;
} else {
y = y + 0.1f;
vector.setX(x);
vector.setY(y);
vector.setZ(z);
view.setTranslation(vector);
View.setTransform(view);
cy = cy + 0.1f;
cvector.setX(cx);
cvector.setY(cy);
cvector.setZ(cz);
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
}
} else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
if (cy <= -5) {
;
} else {
y = y - 0.1f;
vector.setX(x);
vector.setY(y);
vector.setZ(z);
view.setTranslation(vector);
View.setTransform(view);
cy = cy - 0.1f;
cvector.setX(cx);
cvector.setY(cy);
cvector.setZ(cz);
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
}
} else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if (cx >= 5) {
;
} else {
x = x + 0.1f;
cx = cx + 0.1f;
vector.setX(x);
vector.setZ(z);
view.setTranslation(vector);
View.setTransform(view);
cvector.setX(cx);
cvector.setZ(cz);
if (right == true) {
ry = ry - 1;
}
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
}
} else if (arg0.getKeyChar() == 'c') {
if (console.isVisible() == false) {
console.setUndecorated(true);
console.setVisible(true);
console.setLocation(d.width - (d.width / 3), 0);
console.setSize(d.width / 3, d.height);
setSize(d.width - (d.width / 3), d.height);
remove(canvas3D);
canvas3D.setBounds(0, 0, d.width - (d.width / 3), d.height);
add(canvas3D);
} else if (console.isVisible() == true) {
console.setVisible(false);
setSize(d.width, d.height);
remove(canvas3D);
canvas3D.setBounds(0, 0, d.width, d.height);
add(canvas3D);
}
}
}
public void keyReleased(KeyEvent arg0) {
if (ry >= 180) {
int faktor = 1;
while (faktor <= 1000) {
if (ry >= 180) {
ry = ry - 0.01f;
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
faktor++;
warte(1);
} else {
faktor++;
}
}
}
else if (ry <= 180) {
int faktor = 1;
while (faktor <= 1000) {
if (ry <= 180) {
ry = ry + 0.01f;
charakter.rotY(Math.toRadians(ry));
charakter.setTranslation(cvector);
Charakter.setTransform(charakter);
faktor++;
warte(1);
} else {
faktor++;
}
}
}
}
public void keyTyped(KeyEvent arg0) {
;
}
public static void warte(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
}
}
public static void main(String[] args) {
new Game();
}
}