我试图插入一个gif作为我的应用程序的背景。我剪切了所有帧并将其重命名为f1 / f2 / f3 / f4 / f5 / f6 / .....我会使用计时器来更改帧,使其看起来像动画。
总共有42帧,所以f42.png是最后一帧。代码似乎很好,但没有结果。有什么帮助吗?
全局变量:
private String backgroundFile;
public JPanel backgroundPanel, areaImage;
private BufferedImage background;
private javax.swing.Timer timerBackground;
初始化Timer的构造函数:
public Game()
{
entryWindow();
this.setLayout(null);
timerBackground = new javax.swing.Timer(100,this);
timerBackground.stop();
}
动画方法代码:
private void backgroundAnimation()
{
backgroundFile = "f"+backgroundNum+".png";
try{
background=ImageIO.read(new File(backgroundFile));
}
catch(IOException e)
{
}
backgroundPanel = new JPanel()
{
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, 1100,800,null);
}
};
backgroundPanel.setBackground(Color.BLACK);
backgroundPanel.setBounds(0, 0, 1100, 800);
if (backgroundNum>42)backgroundNum++;
else backgroundNum=1;
add(backgroundPanel);
backgroundPanel.setVisible(true);
}
计时器的动作侦听器:
if (ae.getSource() == timerBackground)
{
backgroundAnimation();
}
答案 0 :(得分:2)
为了显示JPanel,您需要将其添加到具有BorderLayout的JFrame之类的东西,然后您需要显示JFrame。 JFrame是一个应用程序窗口,JPanel只能在Window上添加和绘制,如果没有它可以绘制的东西(如app Window)就无法查看。除此之外,每次动画更改时都不需要创建新的JPanel,只需为当前图像创建一个setter,在分配图像后调用repaint(),ImagePanel
可能是这样的:< / p>
public class ImagePanel extends JPanel {
private volatile BufferedImage image;
public void showImage(BufferedImage image) {
this.image=image;
repaint();
}
public void paintComponent(Graphics g) {
g.drawImage(image, 0,0,getWidth(),getHeight(),null);
}
}
在应用程序启动时将它添加到JFrame中,最好也将JFrame的LayoutManager设置为BorderLayout,因为没有它你的面板将具有大小(0,0),因为你没有设置它,它可能是原因之一为什么你没有看到它(你看不到大小为0像素的东西,可以吗?)。
然后在您的计时器中,只需使用要显示的图片调用ImagePanel
方法public void showImage(BufferedImage image)
即可。如果这不能解决您的问题,请发布您的整个代码。没有我只是猜测,但这些都是常见的问题,所以你很有可能从中获得一些东西。
答案 1 :(得分:1)
我在这里可以看到一些问题
1.假设您的Game
课程正在延长JFrame
,您需要将JPanel添加到ContentPane
的{{1}}。使用其中一种方法JFrame
或setContentPane(backgroundPanel);
您没有使用getContentPane().add(backgroundPanel)
。因此,要么使用LayoutManager
,要么使用LayoutManager
方法明确设置'JFrame'和'JPanel'的大小。我建议使用setBounds()
。
此问题的LayoutManager
或任何JPanel
不会自动刷新。更改图片后,您需要在JPanel上调用Component
。
每次更改图片时都不需要创建新的repaint()
。只需延长JPanel
并覆盖JPanel
,就像您所做的那样。使用paintComponent()
更改单个实例的图像,并在每次更改时调用Timer
。
完整的示例,您正在看到的帽子输出将有助于更好地理解问题并为您提供解决方案。请参阅How to create a Minimal, Complete, and Verifiable example
答案 2 :(得分:0)
这里有很多问题,但首先让我回答你的问题:
您正在创建新的JPanel
并在每次运行时将其添加到Game
。这是错误的,因为你向Game
添加了无限的面板
同样在你的if / else你有错误的条件。当它大于42时你增加迭代器。你可能意味着小于42。
我将如何做到这一点:
public class BackgroundPanel extends JPanel {
private int currImage = 0;
private BufferedImage[] backgroundImages;
public BackgroundPanel() {
int numberOfImages = 42;
backgroundImages = new BufferedImage[42];
for(int i = 1; i <= numberOfImages; i++) {
String backgroundFile = "f" + i + ".png";
backgroundImages[i] = ImageIO.read(new File(backgroundFile));
}
}
public void nextImage() {
/*if(currImage <= 42) currImage++;
else currImage = 1;*/
if(currImage++ > 42) currImage = 1;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImages[currImage], 0, 0, getWidth(), getHeight(), null);
}
}
您需要将此面板ONCE添加到您的游戏&#34;:
//Somewhere in your Game
private BackgroundPanel backgroundPanel;
...
...
public Game() {
entryWindow();
this.setLayout(null);
backgroundPanel = new backgroundPanel();
backgroundPanel.setSize(getWidth(), getHeight());
add(backgroundPanel);
timerBackground = new javax.swing.Timer(100,this);
timerBackground.stop();
}
你的计时器:
if (ae.getSource() == timerBackground) {
backgroundPanel.nextImage();
}
答案 3 :(得分:-2)
将背景放在JLabel上更容易。它只需要3行代码,工作正常! :)希望它对任何有同样问题的人都有帮助:)
您只需要复制此代码,使用任何类型的Java支持的图片/视频/ ....更改名称(我将所有图片放在名为&#34的文件夹中;图像&#34;)将后缀.gif更改为您的文件格式),最后是大小。祝好运! :)
public JLabel backgroundGIF;
backgroundGIF = new JLabel(new ImageIcon(getClass().getResource("Images/background.gif")));
backgroundGIF.setBounds(0,0,1100,800);
add(backgroundGIF);