Java - 无法让JFrame刷新图像

时间:2012-09-07 23:07:43

标签: java image swing refresh embedded-resource

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

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

我是新手,尤其是图形和线程新手。非常感谢帮助!

2 个答案:

答案 0 :(得分: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来检查代码是否正确调用了它。

编辑:您在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();

}

希望现在一切都好。

答案 1 :(得分:2)

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

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

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

我没有使用多个线程,但这里是Constructing Threads and 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();
        }
    }