Got the menus working, now the KeyListener needs to be fixed.
This commit is contained in:
parent
2cc5153497
commit
a511027e16
@ -1,13 +1,7 @@
|
|||||||
package spaceshipsim;
|
package spaceshipsim;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import java.awt.geom.*;
|
|
||||||
import java.awt.image.*;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import spaceshipsim.entities.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Nicolás A. Ortega
|
* @author Nicolás A. Ortega
|
||||||
@ -16,38 +10,24 @@ import spaceshipsim.entities.*;
|
|||||||
* @year 2014
|
* @year 2014
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SpaceShipSim extends JFrame implements Runnable, KeyListener {
|
public class SpaceShipSim {
|
||||||
private final String version = "v0.2";
|
private JFrame frame;
|
||||||
|
private SimPanel panel;
|
||||||
|
|
||||||
// Menu items
|
// Menu items
|
||||||
JMenuBar menuBar;
|
JMenuBar menuBar;
|
||||||
JMenu simulationMenu;
|
JMenu simulationMenu;
|
||||||
JMenuItem exitMenuItem;
|
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:
|
// Constructor:
|
||||||
public SpaceShipSim() {
|
public SpaceShipSim() {
|
||||||
super("Space Ship Simulator");
|
frame = new JFrame("SpaceShipSim");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setSize(800, 600);
|
panel = new SimPanel();
|
||||||
|
frame.add(panel);
|
||||||
menuSetup();
|
menuSetup();
|
||||||
setVisible(true);
|
frame.setSize(800, 600);
|
||||||
|
frame.setVisible(true);
|
||||||
backbuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
|
|
||||||
g2d = backbuffer.createGraphics();
|
|
||||||
|
|
||||||
ship = new Ship(400, 300);
|
|
||||||
addKeyListener(this);
|
|
||||||
|
|
||||||
start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: There are no errors in compilation or while running, but the menu bar does not appear
|
// 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() {
|
exitMenuItem.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent ae) {
|
public void actionPerformed(ActionEvent ae) {
|
||||||
stop();
|
panel.stop();
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
simulationMenu.add(exitMenuItem);
|
||||||
menuBar.add(simulationMenu);
|
menuBar.add(simulationMenu);
|
||||||
this.setJMenuBar(menuBar);
|
frame.setJMenuBar(menuBar);
|
||||||
}
|
}
|
||||||
// !!!FIXME-END!!!
|
// !!!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(); }
|
public static void main(String[] args) { new SpaceShipSim(); }
|
||||||
}
|
}
|
@ -27,6 +27,7 @@ public class Ship extends BaseVectorShape {
|
|||||||
public Ship(double nx, double ny) {
|
public Ship(double nx, double ny) {
|
||||||
setX(nx);
|
setX(nx);
|
||||||
setY(ny);
|
setY(ny);
|
||||||
|
setVelY(-0.5);
|
||||||
setShape(new Polygon(shipx, shipy, shipx.length));
|
setShape(new Polygon(shipx, shipy, shipx.length));
|
||||||
setAlive(true);
|
setAlive(true);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user