图像不会绘制到动画

时间:2012-03-10 19:13:32

标签: java animation awt drawimage

在java applet中加载/显示图像时出现问题。不确定我是否正确加载图像或者我是否错误地访问了图像。这是绘制船舶和背景的代码(它是一个类似小行星的游戏)。背景绘制正确,但船没有。这是我正在处理的主要类方法:

public void paintFrame(Graphics g) {
        Dimension d = size();
        g.fillRect(0, 0, d.width, d.height);
        g.drawImage(ship.getImage(), d.width/2, d.height/2, null);
}

我在课程开头创建了一个船舶类的实例。但是,如果我尝试在方法中实例化ship class(例如“Ship ship = new Ship();)”,则表示从不使用变量“ship”。

以下是整个船类:

public class Ship {
    private int dx;
    private int dy;
    private int x;
    private int y;
    private Image image;    

    public Ship() {
        ImageIcon ii = new ImageIcon("ship1.png");
        image = ii.getImage();
    }

    public Image getImage() {
        return image;
    }
}

如果我按原样运行它,它运行没有错误,但它不显示船。如果我尝试在除顶部之外的任何地方创建船的实例,它会给我一个NullPointerException。

更新

这是我的整个主要课程:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

public class RunGame extends Applet implements Runnable {
    int frame;
    int delay;
    Thread animator;
    Ship ship = new Ship();
    Level level;
    Dimension offDimension;
    Image offImage;
    Graphics offGraphics;

    /**
     * Initialize the applet and compute the delay between frames.
     */
    public void init() {
        String str = getParameter("fps");
        int fps = (str != null) ? Integer.parseInt(str) : 10;
        delay = (fps > 0) ? (1000 / fps) : 100;

    }

    /**
     * Method is called when the applet becomes visible on
     * the screen.
     */
    public void start() {
    animator = new Thread(this);
    animator.start();

    }

    /**
     * This method is called by the thread that was created in
     * the start method. It does the main animation.
     */
    public void run() {
    // Remember the starting time
    long tm = System.currentTimeMillis();
    while (Thread.currentThread() == animator) {
        // Display the next frame of animation.
        repaint();

        // Delay depending on how far we are behind.
        try {
        tm += delay;
        Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
        } catch (InterruptedException e) {
        break;
        }

        // Advance the frame
        frame++;
    }
    }

    /**
     * This method is called when the applet is no longer
     * visible. Set the animator variable to null so that the
     * thread will exit before displaying the next frame.
     */
    public void stop() {
    animator = null;
    offImage = null;
    offGraphics = null;
    }

    /**
     * Update a frame of animation.
     */
    public void update(Graphics g) {
        Dimension d = size();

        // Create the offscreen graphics context
        if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) {
            offDimension = d;
            offImage = createImage(d.width, d.height);
            offGraphics = offImage.getGraphics();
        }

        // Erase the previous image
        offGraphics.setColor(getBackground());
        offGraphics.fillRect(0, 0, d.width, d.height);
        offGraphics.setColor(Color.black);

        // Paint the frame into the image
        paintFrame(offGraphics);

        // Paint the image onto the screen
        g.drawImage(offImage, 0, 0, null);
    }

    /**
     * Paint the previous frame (if any).
     */
    public void paint(Graphics g) {
        if (offImage != null) {
            g.drawImage(offImage, 0, 0, null);
        }
    }

    /**
     * Paint a frame of animation.
     */
    public void paintFrame(Graphics g) {
        Dimension d = size();
        g.fillRect(0, 0, d.width, d.height);
        //g.drawImage(level.getImage(), 0, 0, null);
        g.drawImage(ship.getImage(), 400, 300, null);
    }
}

2 个答案:

答案 0 :(得分:3)

ImageIcon ii = new ImageIcon("ship1.png");  

接受String的{​​{3}}假定字符串代表..

  

..文件名或文件路径。

'小程序和文件不混合。'只有受信任的applet才能加载File对象,即使这样,也只能加载最终用户的文件系统。 File对象无法指回服务器。

小程序通常更适用于由URL形成的路径。 Applet类提供了许多方法来帮助形成该URL,并且ImageIcon构造函数被重载以接受URL。

  

..不确定我是否错误地加载了图片,或者我是否错误地访问了图片。

调试101是在加载后立即显示图像。将图像图标拖放到标签中,然后使用JOptionPane显示标签。

答案 1 :(得分:1)

在没有看到完整代码的情况下,我们无法确定异常情况,但在调用drawImage()时,将“this”指定为最后一个参数而不是null。图像是异步加载的,所以他们需要一些实现ImageObserver的东西(比如你的框架)来告诉他们什么时候设法加载了一些(或者全部加载) - 这样它们就可以重新绘制。