代码之家  ›  专栏  ›  技术社区  ›  Cyphereion

paint()不能绘制gif,但png可以吗?

  •  0
  • Cyphereion  · 技术社区  · 7 年前

    我正在使用JLabel试图在上面绘制一个动画gif图像。我可以使用构造函数 new JLabel(new ImageIcon(FML.class.getResource("giphy.gif"))); (使用java 1.8)

    Like this one 但在这种情况下,这似乎不起作用。

    我应该注意到,课堂上几乎没有其他事情发生,这是一个简单的JPanel。

        import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.GridBagLayout;
    import java.awt.Image;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FML {
    
        public static void main(String[] args) {
            new FML();
        }
    
        public FML() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.getContentPane().setLayout(new FlowLayout());
                    frame.getContentPane().add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private ImageIcon animatedGif;
    
            public TestPane() {
                setLayout(new GridBagLayout());
                JButton btn = new JButton(new ImageIcon(FML.class.getResource("FmDxW.png")));
                btn.setSize(50, 50);
                btn.setRolloverEnabled(true);
                animatedGif = new ImageIcon(FML.class.getResource("giphy.gif"));
                btn.setRolloverIcon(animatedGif);
                add(btn);
    
                btn.addMouseListener(new MouseAdapter() {
    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        animatedGif.getImage().flush();
                    }
    
                });
    
                //I need this
                final ImageIcon image = new ImageIcon(FML.class.getResource("giphy.gif"));
    
                //To render over this
                final ImageIcon image2 = new ImageIcon(FML.class.getResource("fmDxW.png"));
    
    
                //I'm not understanding why I can add the gif into the constructor but the paint method fails.
                JLabel label = new JLabel(image) {
                    @Override
                    public void paint(Graphics g) {
                        super.paint(g);
                        g.drawImage(image2.getImage(), 64, 64, this);
                    }
                };
                //setSize because I want to be 100% sure that it's loading the correct size.
                //Removing it doesn't affect the problem at hand.
                label.setSize(64, 64);
                add(label);
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   camickr    7 年前
    g.drawImage(image2.getImage(), 64, 64, this);
    

    下面显示的两种方法都可以很好地处理PNG,但不能处理GIF。

    再说一遍,你为什么要这样做。为什么要尝试覆盖绘制方法来绘制第二个图像?

    相反,你应该做以下事情:

    1. 在paintComponent()方法中绘制两个图像
    2. OverlayLayout
    3. 您甚至可以将布局的布局管理器设置为GridBagLayout之类的,然后当您将JLabel添加到标签时,它将自动居中。

    //设置大小,因为我想百分之百地确保它加载的大小正确。

    你说你知道布局管理器是怎么工作的。如果你这样做了,那么你就会知道这个语句不会起任何作用,因为布局管理器会覆盖你设置的任何值,所以使用这样的代码是错误的,这表明你基本上缺乏理解,不应该包含在 MCVE 因为MCVE的目的是尽可能地简化问题。