1

I'm trying to make a game in Eclipse that runs on Windows 8 using type keys. Whenever I run the app using extends Applet I just get a blank screen. I can change the color of the background but none of my images are showing. I can even play with the app and use my arrow keys to get the returns but I can't see anything on the screen. I'm not sure if I should extend Applet or JApplet. Here's my code

package kiloboltgame;

import java.applet.Applet;
import java.awt.*;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics;
import java.net.URL;

public class StartingClass extends Applet implements Runnable, KeyListener {

    private Robot robot;    
    private Image image, currentSprite, character, characterDown, characterJumped, background;
    private Graphics second;
    private URL base;
    private static Background bg1, bg2; 

    @Override
    public void init() {

        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Q-Bot Alpha");
        try {
            base = getDocumentBase();
        } catch (Exception e){
            //TODO: handle exception
        }

        character = getImage(base, "data/character.png");
        characterDown = getImage(base, "data/down.png");
        characterJumped = getImage(base, "data/jumped.png");
        currentSprite = character;
        background = getImage(base, "data/background.png");
    }

    @Override
    public void start() {
        bg1 = new Background(0,0);
        bg2 = new Background(2160, 0);
        robot = new Robot();

        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void stop() {
        //super.stop();
    }

    @Override
    public void destroy() {
        //super.destroy();
    }

    @Override
    public void run() {
        while (true) {
            robot.update();
            if (robot.isJumped()){
                currentSprite = characterJumped;
            } else if (robot.isJumped() == false && robot.isDucked() == false){
                currentSprite = character;
            }
            bg1.update();
            bg2.update();
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void update(Graphics g){
        if (image == null){
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }

        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);
    }

    @Override
    public void paint(Graphics g){
        g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
        g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
        g.drawImage(currentSprite, robot.getCenterX()-61, robot.getCenterY()-63, this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Move Up");
            break;

        case KeyEvent.VK_DOWN:
            currentSprite = characterDown;
            if (robot.isJumped() == false){
                robot.setDucked(true);
                robot.setSpeedX(0);
            }
            break;

        case KeyEvent.VK_LEFT:
            robot.moveLeft();
            robot.setMovingLeft(true);
            break;

        case KeyEvent.VK_RIGHT:
            robot.moveRight();
            robot.setMovingRight(true);
            break;

        case KeyEvent.VK_SPACE:
            robot.jump();
            break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Stop moving up");
            break;

        case KeyEvent.VK_DOWN:
            currentSprite = character;
            break;

        case KeyEvent.VK_LEFT:
            robot.stopLeft();
            break;

        case KeyEvent.VK_RIGHT:
            robot.stopRight();
            break;

        case KeyEvent.VK_SPACE:
            //System.out.println("Stop jumping");
            break;
        }
    }

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

    }

    public static Background getBg1(){
        return bg1;
    }

    public static Background getBg2(){
        return bg2;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Could be problem in the images file path. Have you given the correct relative path ? Do you have all those images ? Did you get any errors on your console ? – Jay Mar 07 '14 at 17:23
  • No errors in the console. I can interact with the app and all the paths are correct. – user3393681 Mar 07 '14 at 20:10
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Mar 08 '14 at 03:31
  • *"No errors in the console"* That's not surprising, since the code catches and ignores errors. Pull your head out of the sand! Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..` – Andrew Thompson Mar 08 '14 at 03:32
  • 1
    For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). – Andrew Thompson Mar 08 '14 at 03:33
  • Even if i convert the project to Swing or JApplet I'm still getting a blank screen. I'll try MCTaRE next – user3393681 Mar 08 '14 at 05:27

0 Answers0