Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageIcon not rendering for animation

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();
}

}
like image 515
user1142285 Avatar asked Nov 24 '25 17:11

user1142285


2 Answers

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);
    }
});
like image 165
Nahuel Fouilleul Avatar answered Nov 27 '25 07:11

Nahuel Fouilleul


  • 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

like image 31
mKorbel Avatar answered Nov 27 '25 07:11

mKorbel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!