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

