Java在对象的背景图象。找不到我的公共变量

时间:2022-10-23 21:02:11

What i want is that everytime a cell is considered "Dead", this image

我想要的是每次细胞被认为是“死亡”时,这个图像

http://preview.turbosquid.com/Preview/Content_2009_07_25__02_34_32/dead%20cell%201.jpg8c11d904-1879-4bd9-b31c-439bcbb83646Larger.jpg

shows on the background of the object.

显示在对象的背景上。

I have a class called "Game" in the package p_game with the following code

我在包p_game中有一个名为“Game”的类,代码如下

public class Game{
    public Image bg_image;
    public Game(){
           //Here is code that creates a 17*17 table of cells with the status 'Dead' 
        this.Cellules= new p_cell.Cellule[17][17];  
        for (int i=1; i<17; i++){
           for (int j=1; j<17; j++){
               Cellules[i][j]=new p_cell.Cellule(i,j,"Dead");
           }
        }
           //Here is code for the URL and Image
           URL url;
           try {
               url = new URL("http://preview.turbosquid.com/Preview/Content_2009_07_25__02_34_32/dead%20cell%201.jpg8c11d904-1879-4bd9-b31c-439bcbb83646Larger.jpg");
               bg_image = Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
           } catch (MalformedURLException e) {
            e.printStackTrace();
           }
    }
­}

I am trying to use bg_image in my class Cell (in the package p_cell) but it says the variable doesn't exist. What am i missing here?

我试图在我的类Cell中使用bg_image(在包p_cell中),但它说变量不存在。我在这里缺少什么?

public class Cell{
    public void paintComponent(Graphics g){

        g.drawImage(bg_image, 0, 0);

    } 
}

Error given: bg_image cannot be resolved to a variable

给出错误:bg_image无法解析为变量

1 个解决方案

#1


0  

Every variable belongs to a class or class instance.
To use bg_image you must instantiate Game class like this: Game game = new Game();, and then use it with instance reference: game.bg_image.
If you don't want to have instance of Game class, you should make bg_image static and use it like this: Game.bg_image.
Additional reading:
about static variables: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
about instance variables: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

每个变量都属于一个类或类实例。要使用bg_image,你必须像这样实例化Game类:Game game = new Game();,然后将它与实例引用一起使用:game.bg_image。如果你不想拥有Game类的实例,你应该使bg_image静态并像这样使用它:Game.bg_image。补充阅读:关于静态变量:http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html关于实例变量:http://docs.oracle.com/javase/tutorial/java/javaOO/ variables.html

#1


0  

Every variable belongs to a class or class instance.
To use bg_image you must instantiate Game class like this: Game game = new Game();, and then use it with instance reference: game.bg_image.
If you don't want to have instance of Game class, you should make bg_image static and use it like this: Game.bg_image.
Additional reading:
about static variables: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
about instance variables: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

每个变量都属于一个类或类实例。要使用bg_image,你必须像这样实例化Game类:Game game = new Game();,然后将它与实例引用一起使用:game.bg_image。如果你不想拥有Game类的实例,你应该使bg_image静态并像这样使用它:Game.bg_image。补充阅读:关于静态变量:http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html关于实例变量:http://docs.oracle.com/javase/tutorial/java/javaOO/ variables.html