I'm trying to move the boat on the x-axis (without the keyboard yet). How can I relate the movement/ animation to the boat.png
and not to any other image?
public class Mama extends Applet implements Runnable {
int width, height;
int x = 200;
int y = 200;
int dx = 1;
Image img1, img2, img3, img4;
@Override
public void init(){
setSize(627, 373);
Thread t = new Thread(this);
img1 = getImage(getCodeBase(),"Background.png");
img2 = getImage(getCodeBase(), "boat.png");
img3 = getImage(getCodeBase(), "LeftPalm.png");
img4 = getImage(getCodeBase(), "RightPalm.png");
}
@Override
public void paint(Graphics g){
g.drawImage(img1, 0, 0, this);
g.drawImage(img2, 200, 200, this);
g.drawImage(img3, 40, 100, this);
g.drawImage(img4, 450, 130, this);
}
@Override
public void run() {
while(true){
x += dx;
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
System.out.println("Thread generates an error.");
}
}
}
}
You need to replace
g.drawImage(img2, 200, 200, this);
withg.drawImage(img2, x, y, this);
There are a few things that stand out...
The "Problem"
As has already been stated, you need to supply variable arguments to the image drawing process.
g.drawImage(img2, x, y, this);
, this will allow you define where the image should be painted.While you've implemented
Runnable
, you've actually not started any threads to call it. This means, nothing is actually changing the variables.In you
start
method, you should be calling something likenew Thread(this).start()
.Recommendations
Although you've tagged the question as Swing, you're using AWT components. This isn't recommended (in fact applets are generally discouraged as they are troublesome - IMHO). The other problem, as you are bound to find out shortly, is they are not double buffered, this generally leads to flickering when performing animation, which isn't desirable.
As a side note, it is also discouraged to override the
paint
method of top level containers likeApplet
. Top level containers tend to contain a number additional components, by overriding thepaint
method like this, you destroy this setup. Also, top level containers don't tend to be double buffered either.The example below uses a
JFrame
, but it wouldn't take much to convert it to use aJApplet
(just drop theAnimationPanel
on to it. This is another reason why extending from top level containers is generally discouraged ;)