-4

So we are making an Avatar for a game and we have already created the Avatar as an Avatar class file. Our problem is that in our class file Game we are having difficulty declaring Avatar as an instance variable. The code is as follows:

public class Game extends JApplet {
    private static final long serialVersionUID = 1L;
    public static final long WIDTH = 800;
    public static final long HEIGHT = 600;

    // Declare an instance variable to reference your Avatar object

    public @Override void init() {
        // Store an instantiated Avatar into the instance variable
        setSize(new Dimension( WIDTH, HEIGHT));
        getContentPane().setBackground(Color.BLUE); 
    }

    public @Override void paint(Graphics g) { 
        super.paint(g); // Call the JAplet paint method (inheritance)
        // Call the draw method in your Avatar class
    }
}  
Tom
  • 16,842
  • 17
  • 45
  • 54

1 Answers1

0

You could try:

private Avatar avatar;

avatar = new Avatar();

avatar.draw();

Like this:

public class Game extends JApplet {
private static final long serialVersionUID = 1L;
public static final long WIDTH = 800;
public static final long HEIGHT = 600;

// Declare an instance variable to reference your Avatar object
private Avatar avatar;


public @Override void init() {
    // Store an instantiated Avatar into the instance variable
    avatar = new Avatar();

    setSize(new Dimension( WIDTH, HEIGHT));
    getContentPane().setBackground(Color.BLUE); 
}

public @Override void paint(Graphics g) { 
    super.paint(g); // Call the JAplet paint method (inheritance)
    // Call the draw method in your Avatar class

    avatar.draw(); //replace "draw" with method you want to invoke :)
    }
}  
Johan
  • 475
  • 4
  • 17