Question

I was trying to follow these tutorials http://zetcode.com/tutorials/javagamestutorial/animation/ and none of the three examples on that page seem to be working for me. One of them uses a swing timer, one uses the utility timer, and the last and supposedly most effective and accurate according to the page uses a thread to animate.

I will show you the one using the thread, since it is the way that I think I will be doing thing's when using animation for making games.

ThreadAnimationExample.java (in the tutorial it is called star.java but obviously that wont work)

import java.awt.EventQueue;
import javax.swing.JFrame;

public class ThreadAnimationExample extends JFrame {

public ThreadAnimationExample() {

    add(new Board());

    setTitle("Star");               

    pack();
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {                
            JFrame ex = new ThreadAnimationExample();
            ex.setVisible(true);                
        }
    });
}
}

Board.java (the main class)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Board extends JPanel
    implements Runnable {

private final int B_WIDTH = 350;
private final int B_HEIGHT = 350;
private final int INITIAL_X = -40;
private final int INITIAL_Y = -40;
private final int DELAY = 25;

private Image star;
private Thread animator;
private int x, y;

public Board() {

    loadImage();
    initBoard();
}

private void loadImage() {

    ImageIcon ii = new ImageIcon("star.png");
    star = ii.getImage();
}

private void initBoard() {

    setBackground(Color.BLACK);
    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
    setDoubleBuffered(true);

    x = INITIAL_X;
    y = INITIAL_Y;
}

@Override
public void addNotify() {
    super.addNotify();

    animator = new Thread(this);
    animator.start();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    drawStar(g);
}

private void drawStar(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(star, x, y, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

private void cycle() {

    x += 1;
    y += 1;

    if (y > B_HEIGHT) {

        y = INITIAL_Y;
        x = INITIAL_X;
    }
}

@Override
public void run() {

    long beforeTime, timeDiff, sleep;

    beforeTime = System.currentTimeMillis();

    while (true) {

        cycle();
        repaint();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep < 0) {
            sleep = 2;
        }

        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            System.out.println("Interrupted: " + e.getMessage());
        }

        beforeTime = System.currentTimeMillis();
    }
}
}
Was it helpful?

Solution

If you are using Eclipse you should create a source folder and add that image to the source folder. Then you could use this:

ImageIcon ii = new ImageIcon( getClass().getResource("/imageName.png") );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top