Initial commit of jframe to applet conversion

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-04-15 17:26:41 -04:00
parent a8c959700b
commit fee5a5bbf9
11 changed files with 120 additions and 175 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@@ -1,11 +1,11 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.compiler.source=1.6

View File

@@ -33,7 +33,7 @@ public class Asteroid extends MovingSpaceObject implements Collider {
private final int mRadius; private final int mRadius;
private final double mMinVelocity; private final double mMinVelocity;
private final double mMaxVelocity; private final double mMaxVelocity;
private final AsteroidGame mGame; private final AsteroidGameThread mGame;
/** /**
* Creates a new Asteroid * Creates a new Asteroid
@@ -45,7 +45,7 @@ public class Asteroid extends MovingSpaceObject implements Collider {
* @param hitsLeft number of hits left * @param hitsLeft number of hits left
* @author ricky barrette * @author ricky barrette
*/ */
public Asteroid(double x, double y, double minVelocity, double maxVelocity, int radius, int numberSplit, int hitsLeft, AsteroidGame game) { public Asteroid(double x, double y, double minVelocity, double maxVelocity, int radius, int numberSplit, int hitsLeft, AsteroidGameThread game) {
mGame = game; mGame = game;
mColor = new Color(66,33,0); mColor = new Color(66,33,0);
mX = x; mX = x;

View File

@@ -26,11 +26,11 @@ import java.util.Random;
* This class maintain's the game logic. It is the main driver * This class maintain's the game logic. It is the main driver
* @author ricky barrette * @author ricky barrette
*/ */
public class AsteroidGame extends Thread { public class AsteroidGameThread extends Thread {
private static final int DELAY_IN_MSEC = 50; private static final int DELAY_IN_MSEC = 50;
private final ArrayList<Object> mWorld; private final ArrayList<Object> mWorld;
private final GameFrame mGameFrame; private final GameApplet mGameApplet;
public boolean isStarted = false; public boolean isStarted = false;
private long mLastTime; private long mLastTime;
@@ -38,8 +38,8 @@ public class AsteroidGame extends Thread {
* Creates an new Asteroids game * Creates an new Asteroids game
* @author ricky barrette * @author ricky barrette
*/ */
public AsteroidGame() { public AsteroidGameThread(GameApplet gameFrame) {
mGameFrame = new GameFrame(this); mGameApplet = gameFrame;
mWorld = new ArrayList<Object>(); mWorld = new ArrayList<Object>();
//TODO simulate game play unitll game ist started //TODO simulate game play unitll game ist started
this.start(); this.start();
@@ -76,25 +76,25 @@ public class AsteroidGame extends Thread {
s.hyperJump(); s.hyperJump();
} }
mGameFrame.getStatusBar().decrementShipCount(); mGameApplet.getStatusBar().decrementShipCount();
if(mGameFrame.getStatusBar().getShipCount() > 0){ if(mGameApplet.getStatusBar().getShipCount() > 0){
pauseGame(); pauseGame();
mGameFrame.setDisplayText("You died, You can hyper jump to a safe place now... Press start when ready."); mGameApplet.setDisplayText("You died, You can hyper jump to a safe place now... Press start when ready.");
} else { } else {
mGameFrame.setDisplayText("Game Over"); mGameApplet.setDisplayText("Game Over");
if(s != null) if(s != null)
mWorld.remove(s); mWorld.remove(s);
} }
mGameFrame.repaint(); mGameApplet.repaint();
} }
/** /**
* @return the game's frame * @return the game's frame
* @author ricky barrette * @author ricky barrette
*/ */
public GameFrame getGameFrame() { public GameApplet getGameFrame() {
return this.mGameFrame; return this.mGameApplet;
} }
/** /**
@@ -114,10 +114,10 @@ public class AsteroidGame extends Thread {
/* /*
* added a asteroid per level * added a asteroid per level
*/ */
for(int i = 0; i < mGameFrame.getStatusBar().getLevel(); i ++) for(int i = 0; i < mGameApplet.getStatusBar().getLevel(); i ++)
addElement(new Asteroid(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 1, 10, 50, 3, 3, this)); addElement(new Asteroid(gen.nextInt(mGameApplet.getDisplayWidth()), gen.nextInt(mGameApplet.getDispalyHeight()), 1, 10, 50, 3, 3, this));
notification("Level "+ mGameFrame.getStatusBar().getLevel()); notification("Level "+ mGameApplet.getStatusBar().getLevel());
} }
/** /**
@@ -135,23 +135,23 @@ public class AsteroidGame extends Thread {
public void newGame() { public void newGame() {
Random gen = new Random(); Random gen = new Random();
mWorld.clear(); mWorld.clear();
mGameFrame.setDisplayText(null); mGameApplet.setDisplayText(null);
mGameFrame.getStatusBar().setShipCount(3); mGameApplet.getStatusBar().setShipCount(3);
mGameFrame.getStatusBar().setScore(0); mGameApplet.getStatusBar().setScore(0);
mGameFrame.getStatusBar().setAsteroidCount(1); mGameApplet.getStatusBar().setAsteroidCount(1);
mGameFrame.getStatusBar().setTime(0); mGameApplet.getStatusBar().setTime(0);
mGameFrame.getStatusBar().setShotCount(0); mGameApplet.getStatusBar().setShotCount(0);
mGameFrame.getStatusBar().setLevel(1); mGameApplet.getStatusBar().setLevel(1);
mWorld.add(new Ship(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 0, .35, .98, .2, 1, this)); mWorld.add(new Ship(gen.nextInt(mGameApplet.getDisplayWidth()), gen.nextInt(mGameApplet.getDispalyHeight()), 0, .35, .98, .2, 1, this));
initLevel(); initLevel();
startGame(); startGame();
notification("Level "+ mGameFrame.getStatusBar().getLevel()); notification("Level "+ mGameApplet.getStatusBar().getLevel());
mGameFrame.repaintDispaly(); mGameApplet.repaintDispaly();
} }
/** /**
@@ -160,7 +160,7 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
private void notification(final String string) { private void notification(final String string) {
mGameFrame.setDisplayText(string); mGameApplet.setDisplayText(string);
new Thread(new Runnable(){ new Thread(new Runnable(){
@Override @Override
@@ -171,7 +171,7 @@ public class AsteroidGame extends Thread {
e.printStackTrace(); e.printStackTrace();
} }
if(isStarted) if(isStarted)
mGameFrame.setDisplayText(null); mGameApplet.setDisplayText(null);
} }
}).start(); }).start();
} }
@@ -182,7 +182,7 @@ public class AsteroidGame extends Thread {
*/ */
public synchronized void pauseGame(){ public synchronized void pauseGame(){
isStarted = false; isStarted = false;
mGameFrame.setDisplayText("Paused"); mGameApplet.setDisplayText("Paused");
setMovingSpaceObjectsEnabled(false); setMovingSpaceObjectsEnabled(false);
} }
@@ -193,7 +193,7 @@ public class AsteroidGame extends Thread {
*/ */
public synchronized void removeElement(final Object o) { public synchronized void removeElement(final Object o) {
if(o instanceof Asteroid) { if(o instanceof Asteroid) {
mGameFrame.getStatusBar().incrementScore(2); mGameApplet.getStatusBar().incrementScore(2);
} }
mWorld.remove(o); mWorld.remove(o);
} }
@@ -216,14 +216,14 @@ public class AsteroidGame extends Thread {
/* /*
* increment time * increment time
*/ */
mGameFrame.getStatusBar().incrementTime(System.currentTimeMillis() - mLastTime); mGameApplet.getStatusBar().incrementTime(System.currentTimeMillis() - mLastTime);
mLastTime = System.currentTimeMillis(); mLastTime = System.currentTimeMillis();
/* /*
* update the display and stats * update the display and stats
*/ */
mGameFrame.repaintDispaly(); mGameApplet.repaintDispaly();
mGameFrame.getStatusBar().updateStatus(); mGameApplet.getStatusBar().updateStatus();
/* /*
* check for collsions * check for collsions
@@ -250,16 +250,16 @@ public class AsteroidGame extends Thread {
* if there are no more asteroids, then increment the level * if there are no more asteroids, then increment the level
*/ */
if(asteroidCount == 0){ if(asteroidCount == 0){
mGameFrame.getStatusBar().incrementLevel(); mGameApplet.getStatusBar().incrementLevel();
initLevel(); initLevel();
} }
/* /*
* 1up every 200 points * 1up every 200 points
*/ */
if(mGameFrame.getStatusBar().getScore() > 0 && mGameFrame.getStatusBar().getScore() % 200 == 0){ if(mGameApplet.getStatusBar().getScore() > 0 && mGameApplet.getStatusBar().getScore() % 200 == 0){
if(!hasOneUped){ if(!hasOneUped){
mGameFrame.getStatusBar().incrementShipCount(); mGameApplet.getStatusBar().incrementShipCount();
hasOneUped = true; hasOneUped = true;
notification("1up!"); notification("1up!");
@@ -270,8 +270,8 @@ public class AsteroidGame extends Thread {
/* /*
* update the status bar with the new counts * update the status bar with the new counts
*/ */
mGameFrame.getStatusBar().setShotCount(shotCount); mGameApplet.getStatusBar().setShotCount(shotCount);
mGameFrame.getStatusBar().setAsteroidCount(asteroidCount); mGameApplet.getStatusBar().setAsteroidCount(asteroidCount);
/* /*
* reset counters * reset counters
@@ -315,7 +315,7 @@ public class AsteroidGame extends Thread {
*/ */
public synchronized void startGame(){ public synchronized void startGame(){
mLastTime = System.currentTimeMillis(); mLastTime = System.currentTimeMillis();
mGameFrame.setDisplayText(null); mGameApplet.setDisplayText(null);
setMovingSpaceObjectsEnabled(true); setMovingSpaceObjectsEnabled(true);
isStarted = true; isStarted = true;
} }

View File

@@ -35,7 +35,7 @@ import javax.swing.JPanel;
public class Display extends JPanel { public class Display extends JPanel {
private static final long serialVersionUID = -9105117186423881937L; private static final long serialVersionUID = -9105117186423881937L;
private final AsteroidGame mGame; private final AsteroidGameThread mGame;
private final Font mFont; private final Font mFont;
private String mText; private String mText;
@@ -45,7 +45,7 @@ public class Display extends JPanel {
* @param g * @param g
* @author ricky barrette * @author ricky barrette
*/ */
public Display(final Container c, final AsteroidGame g) { public Display(final Container c, final AsteroidGameThread g) {
mGame = g; mGame = g;
this.setBackground(new Color(0, 0, 0)); this.setBackground(new Color(0, 0, 0));
c.add(this, BorderLayout.CENTER); c.add(this, BorderLayout.CENTER);

View File

@@ -26,7 +26,7 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import javax.swing.JFrame; import javax.swing.JApplet;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
@@ -36,67 +36,13 @@ import javax.swing.JMenuItem;
* It will be used to display all the game's information to the user and handle key events from the user * It will be used to display all the game's information to the user and handle key events from the user
* @author ricky barrette * @author ricky barrette
*/ */
public class GameFrame extends JFrame implements KeyListener, ActionListener{ public class GameApplet extends JApplet implements ActionListener, KeyListener {
private static final long serialVersionUID = -2051298505681885632L; private static final long serialVersionUID = -2051298505681885632L;
private final Status mStatusBar; private Status mStatusBar;
private final Display mDisplay; private Display mDisplay;
private final AsteroidGame mGame; private AsteroidGameThread mGameThread;
/**
* Creates a new GameFrame
* @param g
* @author ricky barrette
*/
public GameFrame(final AsteroidGame g) {
super("ASTEROIDS");
mGame = g;
/*
* set up the game's menus
*/
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
/*
* file menu
*/
JMenu fileMenu = new JMenu("File");
JMenuItem menuNewGame = new JMenuItem("New Game");
JMenuItem menuStartGame = new JMenuItem("Start");
JMenuItem menuPauseGame = new JMenuItem("Pause");
JMenuItem menuQuit = new JMenuItem("Quit");
fileMenu.add(menuNewGame);
fileMenu.addSeparator();
fileMenu.add(menuStartGame);
fileMenu.add(menuPauseGame);
fileMenu.addSeparator();
fileMenu.add(menuQuit);
menuNewGame.addActionListener(this);
menuQuit.addActionListener(this);
menuStartGame.addActionListener(this);
menuPauseGame.addActionListener(this);
menuBar.add(fileMenu);
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
Container container = getContentPane();
mStatusBar = new Status(container, mGame);
mDisplay = new Display(container, mGame);
addKeyListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// sets up window's location and sets size****
setSize(1000, 800);
setVisible(true);
}
/** /**
* Called when a menu item is selected from the benu bar * Called when a menu item is selected from the benu bar
@@ -105,15 +51,15 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
*/ */
public void actionPerformed(final ActionEvent e) { public void actionPerformed(final ActionEvent e) {
if (e.getActionCommand().equals("New Game")) { if (e.getActionCommand().equals("New Game")) {
mGame.newGame(); mGameThread.newGame();
} }
if (e.getActionCommand().equals("Start")) { if (e.getActionCommand().equals("Start")) {
mGame.startGame(); mGameThread.startGame();
} }
if (e.getActionCommand().equals("Pause")) { if (e.getActionCommand().equals("Pause")) {
mGame.pauseGame(); mGameThread.pauseGame();
} }
if (e.getActionCommand().equals("Quit")) if (e.getActionCommand().equals("Quit"))
@@ -129,7 +75,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
private void driveShip(final KeyEvent e, final boolean isKeyPressed) { private void driveShip(final KeyEvent e, final boolean isKeyPressed) {
Ship ship = null; Ship ship = null;
//get the user's ship //get the user's ship
for (Object item : mGame.getWorld()) { for (Object item : mGameThread.getWorld()) {
if (item instanceof Ship) { if (item instanceof Ship) {
ship = (Ship) item; ship = (Ship) item;
} }
@@ -216,6 +162,62 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
return mStatusBar; return mStatusBar;
} }
/**
* (non-Javadoc)
* @see java.applet.Applet#init()
*/
@Override
public void init() {
mGameThread = new AsteroidGameThread(this);
/*
* set up the game's menus
*/
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
/*
* file menu
*/
JMenu fileMenu = new JMenu("File");
JMenuItem menuNewGame = new JMenuItem("New Game");
JMenuItem menuStartGame = new JMenuItem("Start");
JMenuItem menuPauseGame = new JMenuItem("Pause");
JMenuItem menuQuit = new JMenuItem("Quit");
fileMenu.add(menuNewGame);
fileMenu.addSeparator();
fileMenu.add(menuStartGame);
fileMenu.add(menuPauseGame);
fileMenu.addSeparator();
fileMenu.add(menuQuit);
menuNewGame.addActionListener(this);
menuQuit.addActionListener(this);
menuStartGame.addActionListener(this);
menuPauseGame.addActionListener(this);
menuBar.add(fileMenu);
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
Container container = getContentPane();
mStatusBar = new Status(container, mGameThread);
mDisplay = new Display(container, mGameThread);
this.addKeyListener(this);
this.setFocusable(true);
this.setFocusCycleRoot(true);
this.requestFocusInWindow();
setSize(1000, 800);
setVisible(true);
repaint();
mGameThread.newGame();
}
/** /**
* Called when a key is pressed * Called when a key is pressed
* (non-Javadoc) * (non-Javadoc)
@@ -230,10 +232,10 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* Start of pause the game * Start of pause the game
*/ */
case KeyEvent.VK_ENTER: case KeyEvent.VK_ENTER:
if(mGame.isStarted) if(mGameThread.isStarted)
mGame.pauseGame(); mGameThread.pauseGame();
else else
mGame.startGame(); mGameThread.startGame();
break; break;
} }

View File

@@ -1,51 +0,0 @@
/**
* Main.java
* @date Mar 31, 2012
* @author ricky barrette
*
* Copyright 2012 Richard Barrette
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.RickBarrette.asteroids;
/**
* This is the main class of my asteroids game.
* This application is based from "A Guide to Programming Asteroids As a Java Applet" by Brandon Carroll.
* @author ricky barrette
*/
public class Main {
public static final boolean DEBUG = false;
private static final AsteroidGame mGame;
static {
mGame = new AsteroidGame();
}
/**
* Creates a new Main
* @author ricky barrette
*/
public Main() {
mGame.newGame();
}
/**
* @param args
* @author ricky barrette
*/
public static void main(String[] args) {
new Main();
}
}

View File

@@ -130,9 +130,6 @@ public abstract class MovingSpaceObject extends SpaceObject implements Moveable{
// adds accel to velocity in direction pointed // adds accel to velocity in direction pointed
if (isAccelerating) { if (isAccelerating) {
if(Main.DEBUG)
System.out.println("accelerating by "+mAcceleration);
// calculates components of accel and adds them to velocity // calculates components of accel and adds them to velocity
mXVelocity += mAcceleration * Math.cos(mAngle); mXVelocity += mAcceleration * Math.cos(mAngle);
mYVelocity += mAcceleration * Math.sin(mAngle); mYVelocity += mAcceleration * Math.sin(mAngle);

View File

@@ -36,7 +36,7 @@ public class Ship extends MovingSpaceObject {
private final int mShotDelay; private final int mShotDelay;
private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3]; private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
private int mShotDelayLeft; private int mShotDelayLeft;
private final AsteroidGame mGame; private final AsteroidGameThread mGame;
/** /**
* radius of circle used to approximate the ship * radius of circle used to approximate the ship
@@ -55,7 +55,7 @@ public class Ship extends MovingSpaceObject {
* @author ricky barrette * @author ricky barrette
* @param game * @param game
*/ */
public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay, final AsteroidGame game) { public Ship(final double x, final double y, final double angle, final double acceleration, final double velocityDecay, final double rotationalSpeed, final int shotDelay, final AsteroidGameThread game) {
this.mX = x; this.mX = x;
this.mY = y; this.mY = y;
this.mAngle = angle; this.mAngle = angle;
@@ -97,9 +97,6 @@ public class Ship extends MovingSpaceObject {
@Override @Override
public void draw(final Graphics g) { public void draw(final Graphics g) {
if(Main.DEBUG)
System.out.println("draw()"+ mX + ", "+ mY);
/* /*
* rotate the points, translate them to the ship's location (by * rotate the points, translate them to the ship's location (by
* adding x and y), then round them by adding .5 and casting them * adding x and y), then round them by adding .5 and casting them

View File

@@ -30,7 +30,7 @@ public class Shot extends MovingSpaceObject {
public static final int TOTAL_DRAWS = 50; public static final int TOTAL_DRAWS = 50;
public static final int SPEED = 10; public static final int SPEED = 10;
private final AsteroidGame mGame;; private final AsteroidGameThread mGame;;
private int mCount = 0; private int mCount = 0;
/** /**
@@ -38,7 +38,7 @@ public class Shot extends MovingSpaceObject {
* @author ricky barrette * @author ricky barrette
* @param mGame * @param mGame
*/ */
public Shot(final double x, final double y, final double angle, final double shipXVel, final double shipYVel, final AsteroidGame game) { public Shot(final double x, final double y, final double angle, final double shipXVel, final double shipYVel, final AsteroidGameThread game) {
mX = x; mX = x;
mY = y; mY = y;
mAngle = angle; mAngle = angle;

View File

@@ -48,7 +48,7 @@ public class Status extends JPanel {
* @param g * @param g
* @author ricky barrette * @author ricky barrette
*/ */
public Status(final Container container, final AsteroidGame g) { public Status(final Container container, final AsteroidGameThread g) {
JPanel northSubPanel = new JPanel(); JPanel northSubPanel = new JPanel();
status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0"); status = new JLabel("Missiles 0 Asteroids 0 Ships 0 Score 0 Time: 0");
northSubPanel.add(status); northSubPanel.add(status);