0

学習目的で小さなテキストアドベンチャー(コンソール)をプログラムしました。今度はいくつかの画像でスパイスを効かせたいのですが、画像を更新することができません。私は何が間違っているのですか?誰かが私を助けてくれますか?

メインクラスはGameMaster.javaで、画像を表示するために使用するクラスはDrawRoom.javaです。

関連するコード:


GameMaster.java

class GameMaster
{   

public static void main(String args[])
{

    // Doing some stuff here, like building rooms, etc...

    // Here I start using images
    DrawRoom drawRoom = new DrawRoom();
    Thread myThread = new Thread(drawRoom); 
    myThread.start(); // The first image appears as expected.

    // Then in a while loop, I get user input from the console and process it.
    // According to which room the user is in, I want to draw the corresponding
    //image.

    drawRoom.changeImage("Images/SOME-OTHER-IMAGE.JPG");
    // This however, does not change the shown image!   
}
}

DrawRoom.java

public class DrawRoom extends JPanel implements Runnable{

Image image;
JFrame frame;

    public DrawRoom(){

        this.image = Toolkit.getDefaultToolkit().getImage("Images/GAME-START.JPG"); 
        this.frame = new JFrame("The Current Image");
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.setSize(640,510);
    }

    public void paintComponent(Graphics g){

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

    public static void main(String arg[]){
        // Left empty.
    }

    public void run(){

        DrawRoom panel = new DrawRoom();
        this.frame.setContentPane(panel);
        this.frame.setVisible(true);
    }

    public void changeImage(String whichImage){

        this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
        this.frame.revalidate();
        this.frame.repaint();
    }
}

私は初心者で、特にグラフィックとスレッドは初めてです。助けていただければ幸いです!

4

2 に答える 2

3

それ自体のrepaint()メソッドを呼び出す必要があります。DrawRoom

public void changeImage(String whichImage){

    this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
    this.repaint(); // not this.frame.repaint()!

}

ちなみに、メソッドSystem.out.println(whichImage)内で古き良きものを使用changeImageして、コードによって適切に呼び出されているかどうかを確認してください。

編集:あなたは新しいDrawRoominsiderun()メソッドを構築し、それをフレームに追加しましたcontentPane-それをしないでください!パネルをフレームのパネルのコンストラクターに追加するだけです。

public DrawRoom(){

    this.image = Toolkit.getDefaultToolkit().getImage("Images/GAME-START.JPG"); 
    this.frame = new JFrame("The Current Image");
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.frame.setSize(640,510);

    this.frame.setContentPane(this);
    this.frame.setVisible(true);

}

...

public void run(){

    // do not need to create any DrawRoom instances!

}

public void changeImage(String whichImage){

    this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
    this.repaint();

}

それがすべて今であることを願っています。

于 2012-09-07T23:23:34.913 に答える
2

次のようなソースから始めることをお勧めします。

これは、メインスレッドに画像の表示されたフレームとパネルを作成させてから、画像の変更を循環させることです。この例では、前後に入れ替える画像が2つだけあります。

また、これをテストするときは、画像が正しいフォルダにあることと、画像ファイルへのパスが正しいことを確認してください。空白のフレームしか表示されない場合は、ファイルが適切な場所にないことが問題でした。

私は複数のスレッドを使用していませんが、ここにスレッドとランナブルを構築するためのリソースがあります。

メインクラスは次のようになります。

import javax.swing.*;

public class SimpleThreeTierMain {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        // Doing some stuff here, like building rooms, etc...

        // Here I start using images
        DrawRoom drawRoom = new DrawRoom();
        JFrame  frame;

        frame = new JFrame("The Current Image");
        frame.setContentPane(drawRoom);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640,510);
        frame.setVisible(true);

        // Then in a while loop, I get user input from the console and process it.
        // According to which room the user is in, I want to draw the corresponding
        //image.

         long  lTime = 2050;
        int   iChange = 0;
        try {
            while (true) {
                Thread.sleep (lTime);
                if (iChange == 1)
                    drawRoom.changeImage("0112091252a.jpg");
                else
                    drawRoom.changeImage("0112091251.jpg");
                iChange = 1 - iChange;
            }
        } catch (InterruptedException iex) {}
    }
}

応接室のクラスは次のようになります。

import javax.swing.*;
import java.awt.*;

public class DrawRoom extends JPanel {

    Image image;

        public DrawRoom() {
            this.image = Toolkit.getDefaultToolkit().getImage("0112091251.jpg"); 
        }

        public void paintComponent(Graphics g){
            g.drawImage(image,0,0,640,480, this);
        }

        public void changeImage(String whichImage){
            this.image = Toolkit.getDefaultToolkit().getImage(whichImage);
            this.repaint();
        }
    }
于 2012-09-08T00:51:11.110 に答える