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"?>
<classpath>
<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"/>
</classpath>

View File

@@ -1,11 +1,11 @@
eclipse.preferences.version=1
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.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.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=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 double mMinVelocity;
private final double mMaxVelocity;
private final AsteroidGame mGame;
private final AsteroidGameThread mGame;
/**
* Creates a new Asteroid
@@ -45,7 +45,7 @@ public class Asteroid extends MovingSpaceObject implements Collider {
* @param hitsLeft number of hits left
* @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;
mColor = new Color(66,33,0);
mX = x;

View File

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

View File

@@ -35,7 +35,7 @@ import javax.swing.JPanel;
public class Display extends JPanel {
private static final long serialVersionUID = -9105117186423881937L;
private final AsteroidGame mGame;
private final AsteroidGameThread mGame;
private final Font mFont;
private String mText;
@@ -45,7 +45,7 @@ public class Display extends JPanel {
* @param g
* @author ricky barrette
*/
public Display(final Container c, final AsteroidGame g) {
public Display(final Container c, final AsteroidGameThread g) {
mGame = g;
this.setBackground(new Color(0, 0, 0));
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.KeyListener;
import javax.swing.JFrame;
import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
@@ -36,68 +36,14 @@ 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
* @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 final Status mStatusBar;
private final Display mDisplay;
private final AsteroidGame mGame;
/**
* 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);
}
private Status mStatusBar;
private Display mDisplay;
private AsteroidGameThread mGameThread;
/**
* Called when a menu item is selected from the benu bar
* (non-Javadoc)
@@ -105,21 +51,21 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
*/
public void actionPerformed(final ActionEvent e) {
if (e.getActionCommand().equals("New Game")) {
mGame.newGame();
mGameThread.newGame();
}
if (e.getActionCommand().equals("Start")) {
mGame.startGame();
mGameThread.startGame();
}
if (e.getActionCommand().equals("Pause")) {
mGame.pauseGame();
mGameThread.pauseGame();
}
if (e.getActionCommand().equals("Quit"))
System.exit(0);
}
/**
* Drives the user's ship
* @param e
@@ -129,7 +75,7 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
private void driveShip(final KeyEvent e, final boolean isKeyPressed) {
Ship ship = null;
//get the user's ship
for (Object item : mGame.getWorld()) {
for (Object item : mGameThread.getWorld()) {
if (item instanceof Ship) {
ship = (Ship) item;
}
@@ -216,6 +162,62 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
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
* (non-Javadoc)
@@ -230,10 +232,10 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
* Start of pause the game
*/
case KeyEvent.VK_ENTER:
if(mGame.isStarted)
mGame.pauseGame();
if(mGameThread.isStarted)
mGameThread.pauseGame();
else
mGame.startGame();
mGameThread.startGame();
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
if (isAccelerating) {
if(Main.DEBUG)
System.out.println("accelerating by "+mAcceleration);
// calculates components of accel and adds them to velocity
mXVelocity += mAcceleration * Math.cos(mAngle);
mYVelocity += mAcceleration * Math.sin(mAngle);

View File

@@ -36,7 +36,7 @@ public class Ship extends MovingSpaceObject {
private final int mShotDelay;
private int[] mXpoints = new int[4], mYpoints = new int[4], mFlameXpoints = new int[3], mFlameYpoints = new int[3];
private int mShotDelayLeft;
private final AsteroidGame mGame;
private final AsteroidGameThread mGame;
/**
* radius of circle used to approximate the ship
@@ -55,7 +55,7 @@ public class Ship extends MovingSpaceObject {
* @author ricky barrette
* @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.mY = y;
this.mAngle = angle;
@@ -97,9 +97,6 @@ public class Ship extends MovingSpaceObject {
@Override
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
* 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 SPEED = 10;
private final AsteroidGame mGame;;
private final AsteroidGameThread mGame;;
private int mCount = 0;
/**
@@ -38,7 +38,7 @@ public class Shot extends MovingSpaceObject {
* @author ricky barrette
* @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;
mY = y;
mAngle = angle;

View File

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