1

Java アプレットでの画像の読み込み/表示に問題があります。画像を間違ってロードしているのか、それとも間違ってアクセスしているのかわかりません。船と背景を描画するコードは次のとおりです (小惑星のようなゲームです)。背景は正しく描画されますが、船は描画されません。これは、私が扱っているメイン クラス メソッドです。

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 クラスのインスタンスを作成します。ただし、メソッド (「Ship ship = new Ship();」など) で 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);
    }
}
4

2 に答える 2

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

a を受け入れるImageIconコンストラクターString、文字列が ..

..ファイル名またはファイル パス。

「アプレットとファイルは混在しません。」信頼できるアプレットだけがFileオブジェクトをロードでき、その場合でもエンド ユーザーのファイル システムからしかロードできません。Fileオブジェクトがサーバーを指すことはできません。

アプレットは通常、URL から形成されたパスで機能します。このAppletクラスは、その URL を形成するのに役立ついくつかのメソッドを提供し、ImageIconコンストラクターは URL を受け入れるためにオーバーロードされます。

..画像を間違ってロードしているのか、それとも間違ってアクセスしているのかわかりません。

デバッグ 101 は、イメージをロードした直後に表示することです。画像アイコンをラベルにドロップし、 a を使用してラベルJOptionPaneを表示します。

于 2012-03-10T19:37:25.820 に答える
1

完全なコードを見ないと、例外で何が起こっているのかを実際に知ることはできませんが、drawImage() への呼び出しで、null の代わりに最後のパラメーターとして「this」を指定してください。画像は非同期に読み込まれるため、ImageObserver (フレームなど) を実装して、もう少し (またはすべて) 読み込みが完了したことを通知して再描画できるようにする必要があります。

于 2012-03-10T19:27:45.590 に答える