3

I have tried to look at other topics with similar question like mine, and most of those solutions appear to point to fixing the classpath for images... so, I tried those by changing the classpath to absolute and using class get resource, but it still won't render the images. I have a suspicion that it has to do with the main method. I don't completely understand how that method works since I copied the source code somewhere online. I am using the Eclipse editor, and I already had put the image files alongside the Flap class file.

package wing;

import java.awt.*;
import javax.swing.*;

public class Flap extends JComponent implements Runnable {
Image[] images = new Image[2];
int frame = 0;

public void paint(Graphics g) {
    Image image = images[frame];
    if (image != null) {
        // Draw the current image
        int x = 0;
        int y = 0;
        g.drawImage(image, x, y, this);
    }
}

public void run() {
    // Load the array of images
    images[0] = new ImageIcon(this.getClass().getResource("/Wing/src/wing/wing1.png"));
    images[1] = new ImageIcon(this.getClass().getResource("/Wing/src/wing/wing2.png"));

    // Display each image for 1 second
    int delay = 10000;    // 1 second

    try {
        while (true) {
            // Move to the next image
            frame = (frame+1)%images.length;

            // Causes the paint() method to be called
            repaint();

            // Wait
            Thread.sleep(delay);
        }
    } catch (Exception e) {
    }
}

public static void main(String[] args) {
    Flap app = new Flap();

    // Display the animation in a frame
    JFrame frame = new JFrame();
    frame.getContentPane().add(app);
    frame.setSize(800, 700);
    frame.setVisible(true);

    (new Thread(app)).start();
}

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1142285
  • 119
  • 2
  • 10
  • 1
    Please, do have a look at this [answer](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659) of mine. – nIcE cOw Jun 20 '12 at 01:19

3 Answers3

2
  • if isn't there any another JComponent(s) added to the public class Flap extends JComponent implements Runnable {

    1. put Image as Icon to the JLabel

    2. use Swing Timer instead of Runnable#Thread (required basic knowledge about Java and Threads too)

  • if there is/are another JComponent(s) added to the public class Flap extends JComponent implements Runnable {

    1. don't use paint() use paintComponent() for Swing JComponents

    2. use Swing Timer instead of Runnable#Thread (required basic knowledge about Java and Threads too)

  • in both cases load image as local variable, don't reload images forever

  • in both cases you have invoke Swing GUI from InitialThread

mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

The resource name "/Wing/src/wing/wing1.png" looks suspicious: it means to locate a resource in the "/Wing/src/wing/" directory, which is most likely not where the resource actually is. Try "/wing/wing1.png" (similarly for the others)

The reason is that the src folder contains the source, which will be converted to classes. So "src/wing/Flap.java" will have the class path "/wing/Flap.class"; similarly for resources (depending on how you are packaging them).

Also, make sure the resource is indeed where you expect it to be (e.g. next to the Flap.class file in the output directory), otherwise the class loader will not find it.

Attila
  • 28,265
  • 3
  • 46
  • 55
  • 1
    @user1142285 - is the png file sitting next to the compiled Flap.class file (not just next to the Flap.java in the source directory) after compilation? – Attila Jun 19 '12 at 15:45
  • Oh, I haven't tried it until you said it. I made a jar out of the Flap file and then put it in a new folder along with png. Unfortunately, it doesn't work. Thanks, though. – user1142285 Jun 19 '12 at 15:56
  • @user1142285 - If you make a jar, the resource needs to be _in_ the jar, not next to it. Make sure you package the .png file with the classes into the jar – Attila Jun 19 '12 at 15:59
2

ImageIcon is not an Image :

images[0] = new ImageIcon(this.getClass().getResource("/Wing/src/wing/wing1.png")).getImage();

The application never ends, in main :

frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36