0

我为学习目的编写了一个小型文本冒险(控制台)。现在我想用一些图像来增加它的趣味性,但我无法让图像刷新。我究竟做错了什么?有人可以帮我吗?

主类是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();
    }
}

我是新手,尤其是图形和线程新手。帮助将不胜感激!

4

2 回答 2

3

您需要调用自身的repaint()方法DrawRoom

public void changeImage(String whichImage){

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

}

顺便说一句,在方法中使用一个好的 ol'System.out.println(whichImage)changeImage检查它是否被您的代码正确调用。

编辑:您构建了一个新的DrawRoom内部run()方法,然后将其添加到框架中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

我建议您从如下所示的源代码开始。

这样做是让主线程为图像创建显示的框架和面板,然后循环更改图像。在这个例子中,我只有两个来回交换的图像。

还要确保您的图像在测试时位于正确的文件夹中,并且您的图像文件路径是正确的。如果您只看到一个空白框架,那么文件不在正确的位置对我来说是个问题。

我没有使用多个线程,但是这里是Constructing Threads 和 Runnables的资源。

主类如下所示:

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 回答