Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

java - How do I display images from an array?

How can I fix it so that the images appear? I'm using NetBeans 11.2 Java Gradle I am trying to make images of a car go forward. The JPanel pops up but no images are shown! I've been at it for a long time trying to figure this out. No error message shows up.

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

public class Main extends JPanel implements ActionListener {
    private final Timer animate;
    private final ImageIcon ImageArray[];
    private int delay = 50, FrameCount = 200, Currentframe = 0;

    public Main() {
        ImageArray = new ImageIcon[FrameCount];
        for (int i = 1; i < ImageArray.length; i++) {
            ImageArray[i] = new ImageIcon(getClass().getResource("Cartest (" + i + ").jpg"));
        }
        animate = new Timer(delay, this);
        animate.start();
    }

    public void paintConponent(Graphics g) {
        super.paintComponent(g);
        if (Currentframe >= ImageArray.length) {
            Currentframe = 0;
        }
        Currentframe++;
        ImageArray[Currentframe].paintIcon(this, g, 0, 0);
    }

    public void actionPerformed (ActionEvent e) {
        repaint();
    }

    public static void main (String args[]) {
        JFrame f= new JFrame("hello");
        Main s = new Main();
        f.add(s);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400,400);
    }
}
question from:https://stackoverflow.com/questions/65626349/how-do-i-display-images-from-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As others pointed out, you made a typo in the paintComponent method's name. The second problem is you'll get a NullpointerException due to line currentFrame = 0; (after the first iteration) as you started filling the imageArray from index 1. There's nothing wrong with the getResource part of your code, as it would've have thrown an exception on that line if no image was to be found.

Advice: Use the Override annotation when overriding a superclass' method. The compiler will start complaining about the annotation's incorrect use in case you made a typo.

This answer isn't necessarily indicative of how to do things. But it does make your code work:

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

public class Main extends JPanel implements ActionListener {

    private final Timer animate;
    private final ImageIcon imageArray[];
    private int delay = 50, frameCount = 200, currentFrame = 0;

    public Main() {

        imageArray = new ImageIcon[frameCount];


        for (int i = 1; i < imageArray.length; i++) {

            imageArray[i] = new ImageIcon(getClass().getResource("Cartest (" + i + ").jpg"));

        }

        animate = new Timer(delay, this);
        animate.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        currentFrame++;
        if (currentFrame == imageArray.length) {
            currentFrame = 1;
        }

        imageArray[currentFrame].paintIcon(this, g, 0, 0);
    }

    public static void main (String args[]) {
        JFrame f= new JFrame("hello");
        Main s = new Main();
        f.add(s);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400,400);
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...