简体   繁体   English

Java-无法让JFrame刷新图像

[英]Java - Can't get JFrame to refresh image

I've programmed a small text adventure (console) for learning purposes. 我已出于学习目的编写了一个小型文本冒险游戏(控制台)。 Now I want to spice it up with some images, but I can't get the images to refresh. 现在,我想添加一些图像,但我无法刷新图像。 What am I doing wrong? 我究竟做错了什么? Can somebody help me pls? 有人可以帮我吗?

The main class is GameMaster.java and the class I use to show the picture is DrawRoom.java 主要类是GameMaster.java,用于显示图片的类是DrawRoom.java。

The relevant code: 相关代码:


GameMaster.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 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();
    }
}

I'm a newbie, especially new to graphics and threads. 我是新手,尤其是图形和线程新手。 Help would be greatly appreciated! 帮助将不胜感激!

You need to call the repaint() method of the DrawRoom itself: 您需要调用DrawRoom本身的repaint()方法:

public void changeImage(String whichImage){

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

}

By the way, use a good ol' System.out.println(whichImage) inside the changeImage method to check if it is properly called by your code. 顺便说一句,请在changeImage方法中使用良好的System.out.println(whichImage)检查代码是否正确调用了它。

EDIT: you built a new DrawRoom inside run() method, then added it to frame's contentPane - don't do that! 编辑:您在run()方法内构建了一个新的DrawRoom ,然后将其添加到框架的contentPane -请勿这样做! Just add the panel to the frame into panel's constructor: 只需将面板添加到框架的面板构造函数中即可:

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();

}

Hope it's all now. 希望这就是现在。

I suggest you start with source that looks like the following. 我建议您从如下所示的源开始。

What this does is to have the main thread create the displayed frame and panel for the image and to then cycle through changing the images. 这样做是让主线程为图像创建显示的框架和面板,然后循环切换图像。 In this example I have just two images that swap back and forth. 在此示例中,我只有两个图像来回交换。

Also make sure your images are in the right folders when testing this and that your path to the image files is correct. 在测试时,还请确保图像位于正确的文件夹中,并且图像文件的路径正确。 If you see nothing but a blank frame, the files not in the right place was the problem for me. 如果您只看到空白框,则文件位置不正确对我来说是个问题。

I am not using multiple threads however here is a resource for Constructing Threads and Runnables . 我没有使用多个线程,但是这里是构造线程和Runnables的资源。

The main class looks like the following: 主类如下所示:

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) {}
    }
}

The drawing room class looks like the following: 客厅类如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM