GhostRavenstorm
GhostRavenstorm

Reputation: 754

Adding KeyEventListeners to JFrame from External Jar?

I have a class project here where I must make a working Asteroids game. Right now I'm stuck on getting key events to even be recognized. I've read and watched tutorials and had those working, but implementing the same thing in this environment isn't helping. I'm hoping maybe someone can guide me in the right direction.

This here is the main class that implements KeyListener with key action methods which makes my Ship object and sets up a "sandbox". "Sandbox" being the Jframe that displays the objects. "Sandbox" is imported from an external jar I received in class as well as other things from "blobfx.jar".

package Asteroid;

import java.util.Random;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import blobfx.SandBox;
import blobfx.SandBoxMode;



public class AsteroidMain implements KeyListener{
//           ^^^^^^^^^^^^ currently has error: "The type AsteroidMain must implement the inherited abstract method KeyListener.keyPressed(KeyEvent)"

private static Random random = new Random();

public static void main(String[] args) {


    SandBox sb = new SandBox();
    sb.setFrameRate(66);
    // makes window for objects at frame rate of 15


    Ship shipThing = new Ship();
    sb.addBlob( shipThing );
    // makes ship object then adds it to the window

    SandBox.simulate(sb);
    // draws the objects on screen

}

@Override
public void keyPressed(KeyEvent e, Ship shipThing) {
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ currently has error: "The method keyPressed(KeyEvent, Ship) of type AsteroidMain must override or implement a supertype method"
    if(e.getKeyCode() == 38){
        shipThing.moveForward(); 
        System.out.println("KEY PRESSED");
    }

}

@Override
public void keyReleased(KeyEvent e) {

}


@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
}

And here is my Ship class that I want to get moving forward when the UP key is pressed.

package Asteroid;

import java.lang.Math;

import blobfx.PolyBlob;


public class Ship extends PolyBlob{

private double angle = 0.0;
private final double delta = 0.15;
private final double speed = 5.0;


public Ship( ){

    super(0,0,0);

    int x[] = {10, -10, -5, -10};
    int y[] = { 0,  -7,  0,   7};
    setPolygon( x, y );
    // sets vertices that draws the ship polygon

    setLoc( 200, 200 );

}

public void moveForward(){

    System.out.println( "MOVING FORWARD");

    setLoc( getLoc().x + (int) Math.round(speed * Math.cos(angle)), 
            getLoc().y + (int) Math.round(speed * Math.sin(angle))
            );
}

public void turnLeft(){

}

public void turnRight(){

}


}

Upvotes: 0

Views: 213

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

Start by taking a look at the JavaDocs for KeyListener

You are violating the contractual requirements of the interface by not implementing keyPressed(KeyEvent)

This...

@Override
public void keyPressed(KeyEvent e, Ship shipThing) {

Should have given you a compiler error, telling you that you weren't overriding a method and it should be

@Override
public void keyPressed(KeyEvent e) {

You can't just "invent" parameters, the interface describes the expected requirements that must be meet by any implementation

Having said that, I would strongly discourage the use of KeyListener in favor of the Key Bindings API, see How to Use Key Bindings for more details

Upvotes: 1

Related Questions