对象上的Java背景图像。Can';找不到我的公共变量
我想要的是,每当一个细胞被视为“死亡”,这个图像 显示在对象的背景上 我在包p_游戏中有一个名为“Game”的类,代码如下对象上的Java背景图像。Can';找不到我的公共变量,java,image,swing,paintcomponent,Java,Image,Swing,Paintcomponent,我想要的是,每当一个细胞被视为“死亡”,这个图像 显示在对象的背景上 我在包p_游戏中有一个名为“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.Ce
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();
}
}
}
给出错误:bg_图像无法解析为变量每个变量都属于一个类或类实例。
要使用bg_图像,您必须像这样实例化
Game
类:Game Game=new Game()
,然后将其与实例引用一起使用:game.bg\u image
如果您不想拥有
Game
类的实例,您应该将bg\u image
设置为静态,并像这样使用:Game.bg\u image
附加阅读:
关于静态变量:
关于实例变量:
bg\u image
是Game
中的一个实例字段,而不是单元格中的一个实例字段。但是,难道不是公共的吗?它是公共的,但不是单元格
的实例。如果你必须访问它,请使用Game.bg_image
我将bg_image设置为静态,并通过Game.bg_image访问它,一切都像一个魔咒;非常感谢。
public class Cell{
public void paintComponent(Graphics g){
g.drawImage(bg_image, 0, 0);
}
}