Complications adding Graphics to KeyListener

I'm trying to add shooting to my game, but I can't change my Player class to abstract or it will not work correctly, therefore I'm restricted to what I can do.

I've tried a million different ways to approach this, none worked. I've searched and searched and watched video tutorials to no avail. This is my last resort, I'm sure somebody can help me.

This is my first applet, but I'm not new to java. This isn't a 2D game. It's just a basic experiment I've been working on.

MainClass.java

/**
 *
 * @author Alexander Benoit
 */
import java.applet.*;
import java.awt.*;

public class MainClass extends Applet implements Runnable {

    private static final long serialVersionUID = 1L;
    Thread thread = new Thread(this);
    boolean running = true;
    Player p;
    Enemy e;
    Image dbImage;
    Graphics dbg;
    int xpos;
    int ypos;
    int mousex;
    int mousey;

    @Override
    public void init() {
        setSize(400, 400);
        p = new Player(this);
        e = new Enemy(this);
    }

    @Override
    public void start() {
        thread.start();
    }

    @Override
    public void destroy() {
        running = false;
    }

    @Override
    public void stop() {
        running = false;
    }


    @Override
    public void run() {
        while (running) {
            repaint();
            p.update(this);
            e.update(this, p);
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                System.out.print("Error occured");
            }
        }

    }

    @Override
    public void update(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paint(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }

    @Override
    public void paint(Graphics g) {
        g.drawString("Game", 180, 20);
        p.paint(g, this);
        e.paint(g, this);
    }
}

Player.java

/**
 *
 * @author Alexander Benoit
 */
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class Player implements KeyListener {

    private int x = 200, y = 200, velX = 0, velY = 0;
    private URL url;
    private Image player;
    private Image playerimg;
    private Image Image;
    public boolean isShooting;


    public Player(MainClass mc) {
        url = mc.getDocumentBase();
        player = mc.getImage(url, "http://i563.photobucket.com/albums/ss75/belhelmiel/player-1.png");
    }

    public void update(MainClass mc) {
        mc.addKeyListener(this);
        x += velX;
        y += velY;
    }

    public void paint(Graphics g, MainClass mc) {
        g.drawImage(player, x, y, mc);
        g.drawString("X:" + x + " Y:" + y, 10, 10);
        //g.drawRect(x, y, 50, 50);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {

            case KeyEvent.VK_SPACE: {
                //This should make the player start shooting
                //I've tried a million different things
                //each one gave me some kind of problem primarily
                //because I can't set this class to abstract
                break;
            }

            case KeyEvent.VK_RIGHT: {
                if (x >= 430) {
                    x = -40;
                }
                velX = 2;
                break;
            }
            case KeyEvent.VK_LEFT: {
                if (x <= -40) {
                    x = 430;
                }
                velX = -2;
                break;
            }
            case KeyEvent.VK_UP: {
                if (y <= -35) {
                    y = 430;
                }
                velY = -2;
                break;
            }
            case KeyEvent.VK_DOWN: {
                if (y >= 400) {
                    y = -30;
                }
                velY = 2;
                break;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_SPACE: {
                //This should make the player stop shooting
                break;
            }

            case KeyEvent.VK_RIGHT: {
                velX = 0;
                break;
            }
            case KeyEvent.VK_LEFT: {
                velX = 0;
                break;
            }
            case KeyEvent.VK_UP: {
                velY = 0;
                break;
            }
            case KeyEvent.VK_DOWN: {
                velY = 0;
                break;
            }
        }
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    void restart() {
        x = 200 - 38 / 2;
        y = 200 - 38 / 2;
    }
}

Enemy.java

import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;

/**
 *
 * @author Alexander Benoit
 */
public class Enemy {

    private int x = 50, y = 50, Speed=3;
    private URL url;
    private Image enemy;

    public Enemy(MainClass mc) {
        url = mc.getDocumentBase();
        enemy = mc.getImage(url, "http://i563.photobucket.com/albums/ss75/belhelmiel/enemy.png");
    }

    public void update(MainClass mc, Player p) {
        x+=Speed;
        if(x<=0){
            Speed=3;
        }else if(x>=mc.getWidth()-33){
            Speed=-3;
        }

        Collision(p);
    }

    public void paint(Graphics g, MainClass mc) {
        g.drawImage(enemy, x, y, mc);
    }

    private void Collision(Player p) {
        int PX = p.getX();
        int PY = p.getY();

        if(PX-38 <= x && PX+38 >= x && PY-38 <= y && PY+38 >= y) {
            p.restart();
            System.out.print("Collision Detected");
        }
    }
}

Sorry if I can't provide you with enough information, I myself am at a lack of it as well because I cannot find anything helpful online after trying to figure this out for a longg time.

Summary: I need to make some sort of bullets fire when the space bar is held down by the player. I know how to use keylistener but I can't use keylistener with the "Graphics g" without setting my Player class to abstract, which I cannot do or my game won't work. I've searched forever trying to figure out how to do this but to no avail. Sorry if this seems kind of odd, it may just be better for me to restart but I've been working on this for hours.

Answers


You can definitely work with keylistener and graphics without setting your player class to abstract. You just need to implement all the methods that are declared in the interfaces. You can learn about abstract classes here and about AWT/Swing painting here.


Need Your Help

log4j JDBCAppender rotate table name

log4j

I have successfully configured an application that uses log4j for it's logging to log into a MySQL database. (Using org.apache.log4j.jdbc.JDBCAppender).

AddObserver UIKeyboardWillShowNotification trouble specifying object

iphone objective-c

I'm trying to receive notification when the keyboard appears but I'm in a situation where I have 2 UITextViews and need to be able to differentiate between them. For some reason, I can't figure out...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.