【问题标题】:Attempting to animate multiple images in Java using JFrame & JPanel尝试使用 JFrame 和 JPanel 在 Java 中为多个图像设置动画
【发布时间】:2016-05-04 23:50:25
【问题描述】:

所以我尝试使用动画来旋转图像,方法是将图像更改为已旋转 22.5 度的新图像。我通过让 1 个类继承自 JFrame 和另一个类从 JPanel 继承来做到这一点但是它没有做任何事情。这是代码..

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

有什么想法吗?

【问题讨论】:

  • “有什么想法吗?” 1) 不要扩展JFrame。更喜欢组合而不是继承。 2)在JLabel 中显示图像(而不是JPanel) 3)不是设置框架的大小/边界,而是设置位置并在加载第一张图像后调用pack()。 4) 当显示一个涉及图像的示例时,热链接(Java 可以从 URL 加载图像)到在this Q&A 中看到的图像。 5) 在 EDT 上启动 GUI。 6) 应用程序资源在部署时将成为嵌入式资源,因此明智的做法是像现在一样开始访问它们..
  • .. embedded-resource 必须通过 URL 而不是文件访问。请参阅info. page for embedded resource 了解如何形成 URL。 7) new ImageIcon("car/yellowCar" + count + ".jpg"); 使用ImageIO 加载图像。 ImageIcon 将静默失败。 8) 使用@Override 表示法对应该覆盖已定义方法的方法进行编译时检查。 9) cp.setLayout(null); Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同语言环境中的不同 PLAF。 ..
  • .. 因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。

标签: java image swing animation jpanel


【解决方案1】:

这是一个工作演示。我在上面讨论的事情,以及更多的调整。仔细阅读代码以了解更改,对新的/不同的方法调用进行一些研究,并询问您是否不理解(从该研究中)我为什么包含它们。请注意,此类包含一个main 方法。

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 = "http://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);
    }
}

【讨论】:

猜你喜欢
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多