drawImage question

Hi,

I've got the following code:

import java.awt.*;

import java.applet.*;

publicclass MyAppletextends Applet{

Image imageEnemy;

// Initialize

publicvoid init(){

imageEnemy = getImage( getCodeBase(),"myimage.gif");

}

// Paint

publicvoid paint(Graphics g){

// Create instance of Enemy with name "enemy1"

Enemy enemy1 =new Enemy(10, 20, imageEnemy);

// Call the function Print(g) from "enemy1"

enemy1.Print(g);

}

}

class Enemy{

// Variables of class Enemy

Image enemyImage;

int enemyX;

int enemyY;

// Constructor of Enemy

public Enemy(int inputX,int inputY, Image inputImage){

enemyX = inputX;

enemyY = inputY;

enemyImage = inputImage;

}

// Function Print of Enemy (should display the enemy)

void Print(Graphics g){

g.drawImage( enemyImage, enemyX, enemyY,this);

}

}

This code doesn't work.

Somehow g.drawImage cannot be called from the function Print.

Now the following code:

import java.awt.*;

import java.applet.*;

publicclass MyAppletextends Applet{

Image imageEnemy;

// Initialize

publicvoid init(){

imageEnemy = getImage( getCodeBase(),"myimage.gif");

}

// Paint

publicvoid paint(Graphics g){

/*

// Create instance of Enemy with name "enemy1"

Enemy enemy1 = new Enemy(10, 20, imageEnemy);

// Call the function Print(g) from "enemy1"

enemy1.Print(g);

*/

g.drawImage( imageEnemy, 10, 20,this);

}

}

class Enemy{

// Variables of class Enemy

Image enemyImage;

int enemyX;

int enemyY;

// Constructor of Enemy

public Enemy(int inputX,int inputY, Image inputImage){

enemyX = inputX;

enemyY = inputY;

enemyImage = inputImage;

}

// Function Print of Enemy (should display the enemy)

void Print(Graphics g){

//g.drawImage( enemyImage, enemyX, enemyY, this);

}

}

This code works...

Why does one code work and the other doesn't? I guess it has something to do with the "this-keyword", but I can't figure it. I do want the image to be drawn from within the Enemy class. Is this even possible?

Thanks,

betonboor

[4650 byte] By [betonboora] at [2007-11-27 11:21:17]
# 1

When you get errors it is a good idea to include the exact error message in your post.

// Function Print of Enemy (should display the enemy)

void Print(Graphics g) {

g.drawImage( enemyImage, enemyX, enemyY, this);

}

I expect the error is on the drawImae line. The fourth parameter you pass is this, the Enemy object. However the drawImage method expects an ImageObserver object to be passed. Since your Enemy class does not extend the ImageObserver class you have a type mismatch on the parameter.

floundera at 2007-7-29 14:47:47 > top of Java-index,Java Essentials,New To Java...
# 2

Hi,

the exact error message was:

cannot resolve symbol method drawImage(java.awt.Image,int,int,Enemy)

Fix:

class Enemy extends ImageObserver didn't work... It couldn't find ImageObserver. However, using class Enemy extends Applet, did work.

Thanks

betonboora at 2007-7-29 14:47:47 > top of Java-index,Java Essentials,New To Java...
# 3

> Fix:

> class Enemy extends ImageObserver didn't work... It

> couldn't find ImageObserver. However, using class

> Enemy extends Applet, did work.

That's not a solution, but rather a very dirty workaround to a non-existent problem.

"Couldn't find ImageObserver"? Did you import it? You know it's in java.awt.image right? So "import java.awt.*;" wont do.

Besides, if you don't need to observe the image being loaded, why provide an observer, just pass null as the last parameter.

dwga at 2007-7-29 14:47:47 > top of Java-index,Java Essentials,New To Java...