学習目的で小さなテキストアドベンチャー(コンソール)をプログラムしました。今度はいくつかの画像でスパイスを効かせたいのですが、画像を更新することができません。私は何が間違っているのですか?誰かが私を助けてくれますか?
メインクラスは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();
}
}
私は初心者で、特にグラフィックとスレッドは初めてです。助けていただければ幸いです!