gpt4 book ai didi

Java标签中gif的问题

转载 作者:行者123 更新时间:2023-12-01 19:55:43 24 4
gpt4 key购买 nike

我尝试放入 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?提前致谢。

最佳答案

BufferedImage 不支持绘制动画 Gif,相反,您需要使用 Image(或者最好使用 ImageIcon) .

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

animated gif that fits he JPanel?

好吧,这是一个更复杂的问题。一种方法是将 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();
}

}

}

关于Java标签中gif的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49826647/

24 4 0