Java的:叠加/其他gif动画叠加动画GIF [英] Java: Superimpose/overlay animated gif on other animated gif

查看:393
本文介绍了Java的:叠加/其他gif动画叠加动画GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个小游戏。这不是一个行动之一,但一个难题之一,所以表现不是很重要。
现在,我主要的游戏区,一个背景图像。在某些场合,我想对背景图像的部分绘制其他图像。
我的问题是,无论是背景图像,以及叠加一(S)可以GIF动画,与未知的帧数(基本上,用户选择)。

现在,我想要做的基本上是:绘制背景图片,并将它的动画(如果它是一个GIF),然后绘制0-n小相对于背景图像位置它的上面,GIF动画,在像素给出坐标。
此外,应该调整大小,以便图像放大/相应地移动。

如何最终的结果看起来是这样: http://i.stack.imgur.com/PxdDt巴纽(试想它动画)

我已发现到它的解决方案,它的工作原理是设置布局管理器为空,并使用绝对定位的JLabel使用图标已他们的动画,用一个作为背景和另一个添加到它作为前景:

  background.setSize(backgroundIcon.getIconWidth(),backgroundIcon.getIconHeight());
foreground.setSize(foregroundIcon.getIconWidth(),foregroundIcon.getIconHeight());
background.setLocation(0,0);
foreground.setLocation(30,30);
background.setLayout(NULL);
background.add(前景);
frame.setLayout(NULL);
frame.add(背景);

(满code以下)

布局管理器设置为null是,因为我听说,有问题(尤其是因为我会想一些文本添加到图像以后,虽然我使用的背景之一,不是背景标签内的另一个标签本身或某事),和调整大小似乎不能够这样,因为我无法调整的JLabel图像图标(找到了一些解决方案,将分裂成GIF每个层中的一个BufferedImage的并把他们再聚首,通过调整之前他们都它失去帧等之间的和具体的延迟)。另外,由于位置应像素完美,调整大小,即使可能会是有问题的,由于舍入误差。

现在,有没有更好的方法来做到这一点?
我能以某种方式加载GIF动画手动合并它们(甚至强硬,他们可能有不同的层数),所以我不得不画只有一个形象?是否有一个布局管理器,我可以手动设置组件的位置,但它会自动调节它,如果你调整窗口/面板?有没有可能是第三方图形项目,可以做我想做的蝙蝠?

在理想情况下会是这样的东西,如果他们不是动画,就像建立一个合并的图像,然后调整大小和显示一个我会做。

全部code:

 进口java.awt.Dimension中;
进口java.net.MalformedURLException;
进口的java.net.URL;进口javax.swing.Icon;
进口javax.swing.ImageIcon中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;公共final类测试仪扩展的JFrame {
    公共静态无效的主要(字串[] args)抛出MalformedURLException的{
        新测试仪();
    }    私人测试仪()抛出MalformedURLException的{
        的setTitle(测试);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        图标backgroundIcon =新的ImageIcon(新URL(http://s5.favim.com/orig/51/animated-gif-gif-hands-sign-language-Favim.com-542945.gif));
        图标foregroundIcon =新的ImageIcon(新URL(http://i.imgur.com/89HANHg.gif));        JLabel的背景=新的JLabel(backgroundIcon);
        JLabel的前景=新的JLabel(foregroundIcon);        // 丑陋
        background.setSize(backgroundIcon.getIconWidth(),backgroundIcon.getIconHeight());
        foreground.setSize(foregroundIcon.getIconWidth(),foregroundIcon.getIconHeight());
        background.setLocation(0,0);
        foreground.setLocation(30,30);
        background.setLayout(NULL);
        background.add(前景);
        setLayout的(NULL);
        加(背景);        //框架集大小为内容的大小
        的setResizable(假);
        的getContentPane()设定preferredSize(新尺寸(backgroundIcon.getIconWidth(),backgroundIcon.getIconHeight()));
        包();        setLocationRelativeTo(NULL);
        调用setVisible(真);
    }
}


解决方案

这对我的作品。较小的轨道图像居中较大的月相图像中。

 进口java.awt中的*。
进口的javax.swing *。
进口java.net *。公共final类测试仪扩展的JFrame {
    公共静态无效的主要(字串[] args)抛出MalformedURLException的{
        新测试仪();
    }    私人测试仪()抛出MalformedURLException的{
        的setTitle(测试);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        图标backgroundIcon =新的ImageIcon(
                新的URL(http://1point1c.org/gif/moon/moonphases.gif));
        图标foregroundIcon =新的ImageIcon(
                新的URL(http://1point1c.org/gif/thum/plnttm.gif));        JLabel的背景=新的JLabel(backgroundIcon);
        JLabel的前景=新的JLabel(foregroundIcon);        background.setLayout(新的GridBagLayout());
        background.add(前景);
        加(背景);        包();        setLocationByPlatform(真);
        调用setVisible(真);
    }
}

I'm making a small game. It's not an action one, but a puzzle one, so performance is not quite as important. Now, I have the main game area, a background image. In certain occasions, I want to draw other images over parts of the background image. My problem is that both the background image as well as the superimposed one(s) can be animated gifs, with an unknown number of frames (basically, user selected).

Now, what I want to do is basically: Draw the background image and have it animated (if it's a gif), then draw 0-n smaller animated gifs on top of it, position relative to background image, given in pixel coordinates. Additionally, it should be resizeable so that the images zoom/move accordingly.

How end result could look like: http://i.stack.imgur.com/PxdDt.png (just imagine it animated)

I have found a solution to it which works by setting the layout manager to null and using absolutely positioned JLabels with Icons to have them animated, using one as background and the other one added to it as foreground:

background.setSize(backgroundIcon.getIconWidth(), backgroundIcon.getIconHeight());
foreground.setSize(foregroundIcon.getIconWidth(), foregroundIcon.getIconHeight());
background.setLocation(0, 0);
foreground.setLocation(30, 30);
background.setLayout(null);
background.add(foreground);
frame.setLayout(null);
frame.add(background);

(full code below)

Setting the layout manager to null is, as I heard, problematic (esp. since I'd want to add some text to the image later on, though I'd use another Label inside the background one, not the background label itself or something), and resizing seems not to be possible this way as I can't resize the JLabels image icon (found some solutions which splitted the gif into one bufferedImage per layer and resized them all before putting them back together again, through which it loses specific delays between frames and so on). Also, since the position should be pixel perfect, resizing even if possible could be problematic due to rounding errors.

Now, is there a better way to do this? Can I somehow load animated gifs and merge them manually (even tough they may have a different number of layers) so I had to paint only one image? Is there a layout manager where I can set the components positions manually but which automatically scales it if you resize the window/panel? Is there maybe a third-party graphics project that could do what I want off the bat?

Ideally would be something like what I would do if they weren't animations, like building a merged Image and then resizing and displaying that one.

Full code:

import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public final class Tester  extends JFrame {
    public static void main(String[] args) throws MalformedURLException {
        new Tester();
    }

    private Tester() throws MalformedURLException {
        setTitle("Tester");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Icon backgroundIcon = new ImageIcon(new URL("http://s5.favim.com/orig/51/animated-gif-gif-hands-sign-language-Favim.com-542945.gif"));
        Icon foregroundIcon = new ImageIcon(new URL("http://i.imgur.com/89HANHg.gif"));

        JLabel background = new JLabel(backgroundIcon);
        JLabel foreground = new JLabel(foregroundIcon);

        // ugly
        background.setSize(backgroundIcon.getIconWidth(), backgroundIcon.getIconHeight());
        foreground.setSize(foregroundIcon.getIconWidth(), foregroundIcon.getIconHeight());
        background.setLocation(0, 0);
        foreground.setLocation(30, 30);
        background.setLayout(null);
        background.add(foreground);
        setLayout(null);
        add(background);

        // set size of frame to size of content
        setResizable(false);
        getContentPane().setPreferredSize(new Dimension(backgroundIcon.getIconWidth(), backgroundIcon.getIconHeight()));
        pack();

        setLocationRelativeTo(null);
        setVisible(true);
    }
}

解决方案

This works for me. The smaller orbital image is centered inside the larger moon phases image.

import java.awt.*;
import javax.swing.*;
import java.net.*;

public final class Tester  extends JFrame {
    public static void main(String[] args) throws MalformedURLException {
        new Tester();
    }

    private Tester() throws MalformedURLException {
        setTitle("Tester");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Icon backgroundIcon = new ImageIcon(
                new URL("http://1point1c.org/gif/moon/moonphases.gif"));
        Icon foregroundIcon = new ImageIcon(
                new URL("http://1point1c.org/gif/thum/plnttm.gif"));

        JLabel background = new JLabel(backgroundIcon);
        JLabel foreground = new JLabel(foregroundIcon);

        background.setLayout(new GridBagLayout());
        background.add(foreground);
        add(background);

        pack();

        setLocationByPlatform(true);
        setVisible(true);
    }
}

这篇关于Java的:叠加/其他gif动画叠加动画GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆