user3393681
user3393681

Reputation: 31

Eclipse ADT Applet blank screen

I'm trying to show some images on my applet and have them move around but whenever I run my project through the applet all I get is a blank/black screen. I can change the color of the background, but I can't see any of my images. Also, the applet isn't initialized when I try to start the applet. Here's the code:

package test;

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

public class MCTaRE extends Applet implements Runnable, KeyListener {

    private Character character;
    private Image image, robot;
    private Graphics graphics;
    private URL base;

    @Override
    public void init() {
        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Test");

        robot = getImage(base, "data/character.png");
    }

    @Override
    public void start() {
        Thread thread = new Thread(this);
        thread.start();

        character = new Character();
    }

    @Override
    public void stop() {

    }

    @Override
    public void run() {
        while (true) {
            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());
            graphics = image.getGraphics();
        }

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

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

    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(robot, character.getInitX() - 61,
                character.getInitY() - 63, this);
    }

    public void KeyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_SPACE:
            System.out.println("Space");
            break;
        }
    }

    public void KeyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

    }

}

package test;

public class Character {
    private int initX = 100;
    private int initY;

    private int speedX = 0;

    public void update(){
        //if (key listener is at the top){
                moveLeft();
    //}
                //else {
                    stop();
                //}

        initY = 200;
    }

    public int getInitX() {
        return initX;
    }

    public int getInitY() {
        return initY;
    }

    public int getSpeedX() {
        return speedX;
    }

    public void setInitX(int initX) {
        this.initX = initX;
    }

    public void setInitY(int initY) {
        this.initY = initY;
    }

    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }

    public void moveLeft(){
        speedX = -5;
    }

    public void stop(){
        speedX = 0;
    }
}

Any help would be greatly appreciated I need to make this work. I can try to use an AVD but I think that would complicate things even more. And a JApplet isn't an option.

Upvotes: 2

Views: 526

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209072

"but I can't see any of my images."

You have a few options loading the images.

I like to use getClass().getResource("/path/to/image") as it load from the class path, which is used from embedded resources.

try {
    robot = ImageIO.read(getClass().getResource("/data/character.png"));
} catch (IOException ex) {
    ex. printStackTrace();
}

You could do the same thing using ImageIcon.getImage where you can avoid the try/catch but I prefer the former, so you can get exception if path is incorrect

robot = new ImageIcon(getClass().getResource("/data/character.png")).getImage();

For Applet, you can use getCodeBase() as seen here

try {
    URL url = new URL(getCodeBase(), "data/character.png");
    robot = ImageIO.read(url);
} catch (IOException e) {
    e.printStackTrace();
}

NOTE: All three options will work with your code (just tested all), given your file structure is as follows, with data in the src

ProjectRoot
         src
             data
                 character.png
             test
                 MCTaRE.java

Side Note

  • You are not calling super.paint in your paint method, which will leave you with paint artifacts. So call it

    @Override
    public void paint(Graphics g) {
        super.paint(g);
    
  • Why code an Applet in the first place. If it must be an Applet, why AWT Applet and not Swing JApplet. If this is a class assignment, have your professor read Why CS teachers should stop teaching Java applets

  • If you want to ditch the zero (AWT) and get with the hero (Swing), read more at Creating GUI with Swing

Upvotes: 1

Related Questions