Maksat Yalkabov
Maksat Yalkabov

Reputation: 558

How can I move image slowly?

I want to move Images slowly.But drawImage() method only takes int values.Is there any method to move pictures slowly.I want to make ground move to the left a little bit slowly.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Board extends JPanel {

private Image ground;
private Image city;
private int x = 0;


public Board() {
    ground = Toolkit.getDefaultToolkit().getImage("Resources\\ground2.png");
    city = Toolkit.getDefaultToolkit().getImage("Resources\\background2.png");
}

public void paint(Graphics g){
    super.paint(g);
    g.drawImage(ground, x--, 500, 600, 200, this);
    g.drawImage(ground, x + 600, 500, 600, 200, this);
    repaint();
    g.drawImage(city, 0, 0, 600, 500, this);

    if(x == -600){
        x = 0;
    }
}
}

Upvotes: 1

Views: 1186

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

drawImage() method only takes int values.Is there any method to move pictures slowly.

Sure. Use an AffineTransform translate instance. They can work with double values. The resulting image drawing will then be 'dithered' along the edges to show what appears to be 'sub pixel accuracy' rendering.

Upvotes: 1

Stefano Buora
Stefano Buora

Reputation: 1062

I believe that the rounding to int of your x isn't the problem. The real one is that you need to move using a time based animation instead of a frame based animation. You can use an approach based on float ( double is too much for your purpose ) or with integer doing some easy steps. Add a member:

private long startTime = 0;

remove the int x as member.

And then change your code in your draw routine using something like:

public void paint(Graphics g){
    super.paint(g);
    long delta;
    if ( startTime == 0 ) {
        startTime = System.currentTimeMillis();
        delta = 0;
    } else {
        long now = System.currentTimeMillis();
        delta = now - startTime;
    }
    //using startTime instead of lastTime increase very slow speed accuracy
    const long speed = 30; //pixel/sec
    //threshold with your image width
    int x = (int)((( speed * delta ) / 1000l)%600l);
    //

    //--- your draw code ---

    //
}

Et voila!

Upvotes: 0

alex2410
alex2410

Reputation: 10994

You can use Swing Timer for changing your x variable and repainting. Add next code to your Board constructor:

Timer t = new Timer(100, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
         x--;
         repaint();
    }
});
t.start();

Also do custom paintings in paintComponent() method instead of paint():

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(ground, x, 500, 600, 200, this);
    g.drawImage(ground, x + 600, 500, 600, 200, this);
    g.drawImage(city, 0, 0, 600, 500, this);

    if (x == -600) {
        x = 0;
    }
}

And don't call repaint() inside paint() or paintComponent() method.

Upvotes: 2

Related Questions