My image is not showing in java, using ImageIcon

Posted by user1048606 on Game Development See other posts from Game Development or by user1048606
Published on 2011-11-16T23:17:08Z Indexed on 2011/11/17 2:05 UTC
Read the original article Hit count: 289

Filed under:

I'd like to know why my images are now showing up when I use ImageIcon and when I have specified the directory the image is in. All I get is a black blank screen with nothing else on it.

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;

// Class for handling key input
public class Craft {

    private int dx;
    private int dy;
    private int x;
    private int y;
    private Image image;
    private Image image2;

    private ArrayList missiles;
    private final int CRAFT_SIZE = 20;
    private String craft = "C:\\Users\\Jimmy\\Desktop\\Jimmy's Folder\\programs\\craft.png";

    public Craft() {
        ImageIcon ii = new ImageIcon(craft);
        image2 = ii.getImage();
        missiles = new ArrayList();
        x = 40;
        y = 60;
    }

    public void move() {
        x += dx;
        y += dy;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }

    public ArrayList getMissiles() {
        return missiles;
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        // Shooting key  
        if (key == KeyEvent.VK_SPACE) {
            fire();
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -1;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -1;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 1;
        }
    }

    // Handles the missile object firing out of the ship
    public void fire() {
        missiles.add(new Missile(x + CRAFT_SIZE, y + CRAFT_SIZE/2));
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
}

© Game Development or respective owner

Related posts about java