Got the menus working, now the KeyListener needs to be fixed.

This commit is contained in:
Deathsbreed 2014-05-27 17:05:04 -05:00
parent 2cc5153497
commit a511027e16
2 changed files with 13 additions and 99 deletions

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,38 +10,24 @@ import spaceshipsim.entities.*;
* @year 2014
*
*/
public class SpaceShipSim extends JFrame implements Runnable, KeyListener {
private final String version = "v0.2";
public class SpaceShipSim {
private JFrame frame;
private SimPanel panel;
// Menu items
JMenuBar menuBar;
JMenu simulationMenu;
JMenuItem exitMenuItem;
// Graphics/Framework items
private Thread gameloop;
private BufferedImage backbuffer;
private Graphics2D g2d;
private AffineTransform identity = new AffineTransform();
// The Ship
private Ship ship;
// Constructor:
public SpaceShipSim() {
super("Space Ship Simulator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
frame = new JFrame("SpaceShipSim");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new SimPanel();
frame.add(panel);
menuSetup();
setVisible(true);
backbuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
ship = new Ship(400, 300);
addKeyListener(this);
start();
frame.setSize(800, 600);
frame.setVisible(true);
}
// FIXME: There are no errors in compilation or while running, but the menu bar does not appear
@ -59,82 +39,15 @@ public class SpaceShipSim extends JFrame implements Runnable, KeyListener {
exitMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
stop();
panel.stop();
System.exit(0);
}
});
simulationMenu.add(exitMenuItem);
menuBar.add(simulationMenu);
this.setJMenuBar(menuBar);
frame.setJMenuBar(menuBar);
}
// !!!FIXME-END!!!
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
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();
}
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 static void main(String[] args) { new SpaceShipSim(); }
}

View File

@ -27,6 +27,7 @@ public class Ship extends BaseVectorShape {
public Ship(double nx, double ny) {
setX(nx);
setY(ny);
setVelY(-0.5);
setShape(new Polygon(shipx, shipy, shipx.length));
setAlive(true);
}