Java background image on object. Can't find my public variable

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

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?

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

        g.drawImage(bg_image, 0, 0);

    } 
}

Error given: bg_image cannot be resolved to a variable

Answers


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


Need Your Help

Calculate date from user input

javascript jquery datetime

I'm trying to create a sort of Date Calculator widget, where dates can be generated based on user inputs to different fields. Thought being if you didn't know the exact date bbut knew it was so many

NSMutable array containsObject, does it check the contents of the object or the object type?

ios nsmutablearray

I am a little confused to how containsObject works. Does it check to see if it contains an instance of an object type or does it compare the inside of the objects variables etc to see if they match?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.