标签中的gif的Java问题

问题描述 投票:0回答:3

在我调整窗口大小之前,单击触发它的按钮后,我尝试放入JPanel的gif没有显示出来。当它出现时,它不适合JPanel并且不是动画。我查看了几篇涉及此问题的帖子,但我不明白如何在我的案例中使用它们。

/*
 * Author: Raymo111
 * Date: 13/04/2018
 * Description: Wishes happy birthday to a special someone
 */

//Imports java GUI classes
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

// Main class with JFrame and ActionListener enabled
public class Happy_Birthday_GUI extends JFrame implements ActionListener {

    // Class variables
    private static JButton startButton = new JButton("CLICK TO START");
    private static JPanel startPanel = new JPanel(), gifPanel = new JPanel();
    private static Color blue = new Color(126, 192, 238), pink = new Color(255, 192, 203);
    private static GridLayout grid1 = new GridLayout(1, 1);

    // Constructor
    public Happy_Birthday_GUI() {

        // Initial screen
        startButton.addActionListener(this);
        startButton.setFont(new Font("Comic Sans MS", Font.PLAIN, 50));
        startPanel.setLayout(grid1);
        startPanel.add(startButton);
        startPanel.setBorder(BorderFactory.createLineBorder(blue, 100));
        startButton.setBackground(pink);
        getContentPane().add(startPanel);

        // Sets title, size, layout (grid 1x1), and location of GUI window (center)
        setTitle("Happy Birthday from Dolphin");
        setSize(840, 840);
        setLayout(grid1);
        setLocationRelativeTo(null);
        setVisible(true);

    }

    // Main method
    public static void main(String[] args) {
        new Happy_Birthday_GUI();
    }

    // Action Performed method
    public void actionPerformed(ActionEvent event) {

        // Proceed to gif and song
        if (startButton == event.getSource()) {
            getContentPane().removeAll();
            BufferedImage dolphin;
            gifPanel.setLayout(grid1);
            gifPanel.setBorder(BorderFactory.createLineBorder(pink, 100));
            try {
                dolphin = ImageIO.read(new File("C:\\Users\\raymo\\Pictures\\dolphin.gif"));
                JLabel gifLabel = new JLabel(new ImageIcon(dolphin));
                gifPanel.add(gifLabel);
            } catch (IOException e) {
                e.printStackTrace();
            }
            getContentPane().add(gifPanel);
        }
    }

}

这是dolphin.gif。它真可爱。

Dolphin.gif


如何在单击开始按钮作为适合JPanel的动画gif后立即显示它?提前致谢。

java swing user-interface jpanel gif
3个回答
1
投票

我点击按钮后试图放入JPanel

从可见GUI添加(或删除)组件时,基本代码为:

panel.add(...);
panel.revalidate();
panel.repaint();

revalidate()需要调用布局管理器,以便为组件指定大小。

没有动画。

使用带有ImageIcon的JLabel来显示图像。 JLabel将为GIF制作动画。

当它出现时,它不适合JPanel和

您可以尝试使用Stretch Icon来填充标签可用的空间。


2
投票

BufferedImage不支持绘制GIF动画,相反,你需要使用Image(或者最好是ImageIcon)。

然后可以将其直接应用于JLabel,它将执行动画操作本身。

适合他JPanel的动画GIF?

好的,这是一个更复杂的问题。一种方法是将Gif转换为所需的大小,但不用说,这非常非常复杂。

一个更简单的解决方案可能是使用AffineTransform并缩放图像以满足组件本身的要求。这将需要一个自定义组件,能够计算比例并绘制图像的每一帧。

幸运的是,JPanel是一个ImageObserver,这意味着它能够绘制gif动画

Stretchy

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ImageIcon image;

        public TestPane() {
            image = new ImageIcon("/Users/swhitehead/Downloads/NbENe.gif");
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            int imageWidth = image.getIconWidth();
            int imageHeight = image.getIconHeight();

            if (imageWidth == 0 || imageHeight == 0) {
                return;
            }
            double widthScale = (double)getWidth() / (double)imageWidth;
            double heightScale = (double)getHeight() / (double)imageHeight;
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(image.getImage(), AffineTransform.getScaleInstance(widthScale, heightScale), this);
            g2d.dispose();
        }

    }

}

0
投票

我最终做了:

gifPanel.add(new TestPane());
getContentPane().add(gifPanel);
revalidate();
repaint();

使用camickr的revalidate和repaint,以及MadProgrammer的TestPane类,它可以很好地使gif动画,正确调整大小并立即显示。

© www.soinside.com 2019 - 2024. All rights reserved.