-1

Im making a basic space invaders game. I got all the resources from the LWJGL .zip file (Im not using LWJGL librarys to create my game, just got the pictures, etc. from it.) Anyway, whenever i press "space" on my keyboard, my KeyListener creates a new bullet that my ship fires. However, i dont know how to draw the bullets image, since my KeyListener doesnt pass a graphics object, and you need one to draw a image. The code thats causing the problem is the "drawImage" method in the "Shot" constructor. Heres my code:

    public class KeyTyped{

    public void keyESC(){
        Screen.isRunning = false;
    }

    public void keyLEFT() {
        Screen.shipPosX -= 10;

    }

    public void keyRIGHT() {
        Screen.shipPosX += 10;

    }
    //This is automatically called from my KeyListener whenever i 
    //press the spacebar
    public void keySPACE(){
        if(!spacePressed){
            ShotHandler.scheduleNewShot();
        }else{
            return;
        }
    }


}

public class ShotHandler {

public static int shotX = Screen.shipPosX;
public static int shotY = Screen.shipPosY + 25;




public static void scheduleNewShot() {
    //All this does is set a boolean to 'false', not allowing you to fire any more shots until a second has passed.
    new ShotScheduler(1);


    new Shot(25);
}

}

public class Shot extends ShotHandler{

public Shot(int par1){

    //This is my own method to draw a image. The first parameter is the graphics object that i need to draw.
    GUI.drawImage(*????????*, "res/spaceinvaders/shot.gif", ShotHandler.shotX, ShotHandler.shotY);

}
            //Dont worry about this, i was just testing something
    for(int i = 0; i <= par1; i++){
        ShotHandler.shotY++;
    }
}

}

Thanks guys! Any help will be appreciated!

(This is a re-post because the last time i posted this, i didnt get a sufficient answer, at least for my skill level)

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
Geforce132
  • 73
  • 7
  • `yourpanel.getGraphics()`? – BackSlash Jul 26 '13 at 23:45
  • I tried that, but it doesnt draw the picture. I have no idea why. – Geforce132 Jul 26 '13 at 23:46
  • 2
    Because, as @JBNizet told you in the previous question, this is **not** the proper way to do it. _This is a re-post because the last time i posted this, i didnt get a sufficient answer, at least for my skill level_ You asked your previous question 1 hour ago. Don't you think you could at least wait a day to let all other users see and answer your question? – BackSlash Jul 26 '13 at 23:48
  • Relevant SO q: [I can't get graphics to draw on my panel/canvas/window...](http://stackoverflow.com/questions/11878676/i-cant-get-graphics-to-draw-on-my-panel-canvas-window-in-my-java-program-swing) – Caffeinated Jul 26 '13 at 23:53
  • 1) A single blank line of white space in source code is *always* enough. 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Jul 27 '13 at 01:12

1 Answers1

3

Start by taking a look at

You may also find Concurrency in Swing of some usefulness.

The basic gist is you should have some kind of model which describes the state of the game play at any moment in time. This model is updated outside the content of the Event Dispatching Thread, normally in some kind of background thread, where the time it takes to perform the updates doesn't effect the ability for Swing to continue running and remain responsive to the user.

You will need to override the JPanel's paintComponent method. In here, you would paint the model state to the screen.

There are a bunch of other techniques which could be discussed, but lets get this started

Examples...

Community
  • 1
  • 1
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329