我为学习目的编写了一个小型文本冒险(控制台)。现在我想用一些图像来增加它的趣味性,但我无法让图像刷新。我究竟做错了什么?有人可以帮我吗?
主类是GameMaster.java,我用来展示图片的类是DrawRoom.java
相关代码:
游戏大师.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!
}
}
画室.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();
}
}
我是新手,尤其是图形和线程新手。帮助将不胜感激!