Reputation: 249
Im making a simple pong game and want to put a texture over my paddle but I don't know how to import image into my game. I tried doing in like here in 3rd example but it doesn't work:
Image myImage = getImage(getCodeBase(), "texture.png");
g.drawImage(myImage, 0, 0 , 10, 120, this);
g cannot be resolved
Here's some code:
public void run(){
Image myImage = getImage(getCodeBase(), "texture.png");
g.drawImage(myImage, 0, 0 , 10, 120, this);
GOval ball = makeBall();
add(ball);
GRect paddleLeft = makePaddle();
GRect paddleRight = makePaddle();
add(paddleLeft);
add(paddleRight);
bounce(ball, paddleLeft, paddleRight);
}
public static GRect makePaddle(){
GRect result = new GRect(0,0,WIDTH,HEIGHT);
result.setFilled(true);
result.setColor(Color.BLACK);
return result;
}
the texture.png is for the paddles
EDIT:
I got the texture to load, but I can't make it move with the paddles, I don't know why WIDTH is width of the paddle, getWidth() - window. I guess the code I use to move the paddle should work for the texture but it doesnt
with the image.sendToFront()
player's paddle's texture works, but the AI's doesn't
if(mouseY<getHeight()-HEIGHT){ // Player
paddleLeft.setLocation(WIDTH,mouseY);
image.setLocation(WIDTH,mouseY);
image.sendToFront();
}
else{
paddleLeft.setLocation(WIDTH,getHeight()-HEIGHT);
image.setLocation(WIDTH,getHeight()-HEIGHT);
image.sendToFront();
}
if(ball.getY()<getHeight()-paddleRight.getHeight()){ // AI
paddleRight.setLocation(getWidth()-2*WIDTH,ball.getY());
image2.setLocation(getWidth()-2*WIDTH,ball.getY());
image2.sendToFront();
}
else
paddleRight.setLocation(getWidth()-2*WIDTH,getHeight()-paddleRight.getHeight());
image2.setLocation(getWidth()-2*WIDTH,getHeight()-paddleRight.getHeight());
image2.sendToFront();
Upvotes: 0
Views: 1121
Reputation: 54639
It seems like you are using a library from http://cs.stanford.edu/people/eroberts/jtf/ (based on the GRect
class etc).
And it seems like you should be able to use a GImage as well. So the code could roughly look like
Image myImage = getImage(getCodeBase(), "texture.png");
//g.drawImage(myImage, 0, 0 , 10, 120, this);
GImage image = new GImage(myImage);
add(image);
Upvotes: 1