1

So I'm attempting to rotate an image using animation by having it change images to a new one that has been rotated 22.5 degrees. I'm doing this by having 1 class inheriting from a JFrame and the other class from a JPanel However it is not doing anything. Here is the code..

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.*;

public class LogoAnimatorJPanel extends JPanel implements ActionListener
{
    protected ImageIcon[] images = new ImageIcon[16] ; 
    private int currentImage = 0; 

    private Timer animationTimer; 

    public LogoAnimatorJPanel()
    {
        for ( int count = 0; count < images.length; count++ ){
            images [count] = new ImageIcon("car/yellowCar" + count + ".jpg"); 
        }
        startAnimation();
    }

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

        images[ currentImage ].paintIcon( this, g, 50  , 50 );

        currentImage = ( currentImage + 1 ) % images.length;
    } 

    public void startAnimation()
    {
        animationTimer = new Timer(20, this);
        animationTimer.start();
    } 

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

displayAnimator

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


public class displayAnimator extends JFrame
{
    private LogoAnimatorJPanel fp;        
    public displayAnimator()
    {
        setTitle("car");               
        setBounds(200,200,200,200);        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container cp = getContentPane();   
        cp.setLayout(null);               

        fp = new LogoAnimatorJPanel();             
        fp.setBounds(180, 25, 100, 100);  


        cp.add(fp);   
    }

    public static void main(String[] args) 
    {
        displayAnimator testRun = new displayAnimator();   
        testRun.setVisible(true);          
    }
}

Any Ideas?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"Any Ideas?"* 1) Don't extend `JFrame`. Prefer composition over inheritance. 2) Display the images in a `JLabel` (instead of the `JPanel`) 3) Instead of setting the size/bounds of the frame, set the location and call `pack()` once the first image is loaded. 4) When showing an example involving images, hot-link (Java can load images from an URL) to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 5) Start the GUI on the EDT. 6) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now.. – Andrew Thompson May 04 '16 at 23:57
  • .. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 7) `new ImageIcon("car/yellowCar" + count + ".jpg"); ` Use `ImageIO` to load images. `ImageIcon` will fail silently. 8) Use `@Override` notation for compile time checking of methods that are supposed to overriding a method already defined. 9) `cp.setLayout(null); ` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. .. – Andrew Thompson May 04 '16 at 23:57
  • .. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 05 '16 at 00:01

1 Answers1

3

Here is a working demo. of the things I was discussing above, along with a few more tweaks. Read code carefully for changes, do some research on the new/different method calls, and ask if you don't understand (from that research) why I included them. Note that this class includes a main method.

import java.awt.Image;
import java.awt.event.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class LogoAnimator {

    private int currentImage = 0;
    private JLabel animationDisplayLabel = new JLabel();
    private Timer animationTimer;
    private String prefix = "https://i.stack.imgur.com/";
    private String suffix = ".png";
    private String[] imageNames = {"gJmeJ", "L5DGx", "in9g1", "IucNt", "yoKxT"};
    protected ImageIcon[] images = new ImageIcon[imageNames.length];

    public LogoAnimator() {
        for (int count = 0; count < images.length; count++) {
            try {
                URL url = new URL(prefix + imageNames[count] + suffix);
                Image image = ImageIO.read(url);
                images[count] = new ImageIcon(image);
            } catch (Exception ex) { // TODO! Better exception handling!
                ex.printStackTrace();
            }
        }
        startAnimation();
    }

    public void startAnimation() {
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                currentImage = (currentImage + 1) % images.length;
                animationDisplayLabel.setIcon(images[currentImage]);
            }
        };
        animationDisplayLabel.setIcon(images[0]);
        animationTimer = new Timer(200, listener);
        animationTimer.start();
    }

    public JComponent getAnimationComponent() {
        return animationDisplayLabel;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                LogoAnimator fp = new LogoAnimator();

                JFrame f = new JFrame("car");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                f.add(fp.getAnimationComponent());
                f.pack();
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433