14 Commits
v0.1 ... v0.2

4 changed files with 221 additions and 90 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Ignore binary files
bin/*
# Just in case some .class files escape:
*.class

View File

@ -0,0 +1,106 @@
package spaceshipsim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import spaceshipsim.entities.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class SimPanel extends JPanel implements Runnable, KeyListener {
// Graphics/Framework items
private Thread gameloop;
//private BufferedImage backbuffer;
private Graphics2D g2d;
//private AffineTransform identity = new AffineTransform();
// The Ship
private Ship ship;
public SimPanel() {
setFocusable(true);
ship = new Ship(400, 300);
addKeyListener(this);
start();
}
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
public void stop() { gameloop = null; }
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 800, 600);
drawInfo();
drawShip();
}
private void drawInfo() {
g2d.translate(0, 10);
g2d.setColor(Color.WHITE);
g2d.drawString("Welcome to SpaceShipSim v0.2", 10, 10);
g2d.drawString("Position: " + (int)ship.getX() + ", " + (int)ship.getY(), 10, 25);
g2d.drawString("Velocity (px/s): " + (int)(ship.getVelX() * 50) + ", " + (int)(ship.getVelY() * 50), 10, 40);
g2d.drawString("Acceleration (px/s/s): " + (int)(ship.getAccelX() * Math.pow(50, 2)) + ", " + (int)(ship.getAccelY() * Math.pow(50, 2)), 10, 55);
g2d.drawString("Move Angle: " + (int)ship.getMoveAngle(), 10, 70);
g2d.drawString("Face Angle: " + (int)(ship.getFaceAngle() - 90), 10, 85);
}
private void drawShip() {
g2d.translate(ship.getX(), ship.getY());
g2d.rotate(Math.toRadians(ship.getFaceAngle()));
g2d.setColor(Color.RED);
g2d.fill(ship.getShape());
}
public void run() {
Thread t = Thread.currentThread();
while(t == gameloop) {
try {
ship.update();
Thread.sleep(20);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
repaint();
}
}
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(true); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(true); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(true); }
}
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(false); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(false); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(false); }
}
public void keyTyped(KeyEvent ke) {}
public void reset() {
ship = new Ship(400, 300);
}
}

View File

@ -1,13 +1,7 @@
package spaceshipsim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
import spaceshipsim.entities.*;
/**
* @author Nicolás A. Ortega
@ -16,100 +10,110 @@ import spaceshipsim.entities.*;
* @year 2014
*
*/
public class SpaceShipSim extends JFrame implements Runnable, KeyListener {
private final String version = "v0.1";
public class SpaceShipSim {
private JFrame frame;
private SimPanel panel;
private Thread gameloop;
private BufferedImage backbuffer;
private Graphics2D g2d;
private AffineTransform identity = new AffineTransform();
// Menu items
JMenuBar menuBar;
private Ship ship;
JMenu simulationMenu;
JMenuItem resetItem;
JMenuItem exitItem;
JMenu helpMenu;
JMenuItem instructionsItem;
JMenuItem licenseItem;
JMenuItem aboutItem;
// Constructor:
public SpaceShipSim() {
super("Space Ship Simulator");
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backbuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
ship = new Ship(400, 300);
addKeyListener(this);
start();
frame = new JFrame("SpaceShipSim");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new SimPanel();
frame.add(panel);
menuSetup();
frame.setSize(800, 600);
frame.setVisible(true);
}
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
public void menuSetup() {
menuBar = new JMenuBar();
simulationMenu = new JMenu("Simulation");
resetItem = new JMenuItem("Reset");
exitItem = new JMenuItem("Exit");
helpMenu = new JMenu("Help");
instructionsItem = new JMenuItem("Instructions");
licenseItem = new JMenuItem("License");
aboutItem = new JMenuItem("About");
public void stop() { gameloop = null; }
public void paint(Graphics g) {
g2d.setTransform(identity);
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, getSize().width, getSize().height);
drawInfo();
drawShip();
g.drawImage(backbuffer, 0, 0, this);
}
private void drawInfo() {
g2d.setPaint(Color.WHITE);
g2d.drawString("Welcome to SpaceShipSim " + version, 10, 40);
g2d.drawString("Position: " + (int)ship.getX() + ", " + (int)ship.getY(), 10, 55);
g2d.drawString("Velocity (px/s): " + (int)(ship.getVelX() * 50) + ", " + (int)(ship.getVelY() * 50), 10, 70);
g2d.drawString("Acceleration (px/s/s): " + (int)(ship.getAccelX() * Math.pow(50, 2)) + ", " + (int)(ship.getAccelY() * Math.pow(50, 2)), 10, 85);
g2d.drawString("Move Angle: " + (int)ship.getMoveAngle(), 10, 100);
g2d.drawString("Face Angle: " + (int)(ship.getFaceAngle() - 90), 10, 115);
}
private void drawShip() {
g2d.setTransform(identity);
g2d.translate(ship.getX(), ship.getY());
g2d.rotate(Math.toRadians(ship.getFaceAngle()));
g2d.setColor(Color.RED);
g2d.fill(ship.getShape());
}
public void run() {
Thread t = Thread.currentThread();
while(t == gameloop) {
try {
ship.update();
Thread.sleep(20);
} catch(InterruptedException ie) {
ie.printStackTrace();
resetItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) { panel.reset(); }
});
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
panel.stop();
System.exit(0);
}
});
repaint();
}
instructionsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("Instructions", "Accelerate: Up Arrow\n" +
"Turn left: Left Arrow\n" +
"Turn right: Right Arrow\n\n");
}
});
licenseItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("License Information",
"SpaceShipSim v0.2, a simulation of a spaceship in a frictionless environment\n" +
"Copyright (C) 2014 Nicolás A. Ortega\n\n" +
"This program is free software: you can redistribute it and/or modify\n" +
"it under the terms of the GNU General Public License as published by\n" +
"the Free Software Foundation, either version 3 of the License, or\n" +
"(at your option) any later version.\n\n" +
"This program is distributed in the hope that it will be useful,\n" +
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
"GNU General Public License for more details.\n\n" +
"You should have received a copy of the GNU General Public License\n" +
"along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
}
});
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("About", "SpaceShipSim v0.2\n" +
"Copyright (C) 2014 Nicolás A. Ortega\n" +
"Contact: nicolas.ortega.froysa@gmail.com\n" +
"Source-code: https://github.com/Deathsbreed/SpaceShipSim\n" +
"Developers: Nicolás Ortega\n\n");
}
});
resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
simulationMenu.add(resetItem);
simulationMenu.addSeparator();
simulationMenu.add(exitItem);
helpMenu.add(instructionsItem);
helpMenu.add(licenseItem);
helpMenu.addSeparator();
helpMenu.add(aboutItem);
menuBar.add(simulationMenu);
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
}
public void keyPressed(KeyEvent ke) {
//keys[ke.getKeyCode()] = true;
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(true); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(true); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(true); }
}
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(false); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(false); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(false); }
}
public void keyTyped(KeyEvent ke) {}
public static void main(String[] args) { new SpaceShipSim(); }
}
}

View File

@ -0,0 +1,16 @@
package spaceshipsim;
import javax.swing.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class Window {
public Window(String title, String infoMessage) {
JOptionPane.showMessageDialog(null, infoMessage, title, JOptionPane.INFORMATION_MESSAGE);
}
}