标签背景方块显示上的Java Gif? [英] Java Gif on Label Background Squares Showing?

查看:42
本文介绍了标签背景方块显示上的Java Gif?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序上使用.gif,并且效果不佳..当我添加.gif时,它显示了带有.gif移动的标签..但旁边有正方形:s 金达·阿克沃德...

I am trying to use .gif on my app, and its not going good.. when I add the .gif it shows the label with my .gif moving.. but there are squares beside it :s Kinda akward...

这是我的.gif: http://sadpanda.us/images/872436-4CD65DA.gif

This is my .gif: http://sadpanda.us/images/872436-4CD65DA.gif

这是我的代码:

public class TestGif {

    private JFrame frame;


    public TestGif(){
        this.frame = new JFrame("teste");
        frame.setSize(200,200);
        ImageIcon icon = new ImageIcon("dark.gif");
        JLabel label = new JLabel(icon);
        icon.setImageObserver(label);
        frame.getContentPane().add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String[] args) throws Exception {
        new TestGif();
    }
}

非常感谢,对不起我的英语不好!

Thanks alot in advance, sorry my bad english!

推荐答案

使用Riven提供的解决方案,网址为

Using solution from Riven at http://riven8192.blogspot.com/2010/02/image-java-animated-gifs.html

ImageUtil.java:

public class ImageUtil
{
    public static BufferedImage convertRGBAToGIF(BufferedImage src, int transColor)
    {
        BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
        Graphics g = dst.getGraphics();
        g.setColor(new Color(transColor));
        g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
        {
            IndexColorModel indexedModel = (IndexColorModel) dst.getColorModel();
            WritableRaster raster = dst.getRaster();
            int sample = raster.getSample(0, 0, 0);
            int size = indexedModel.getMapSize();
            byte[] rr = new byte[size];
            byte[] gg = new byte[size];
            byte[] bb = new byte[size];
            indexedModel.getReds(rr);
            indexedModel.getGreens(gg);
            indexedModel.getBlues(bb);
            IndexColorModel newModel = new IndexColorModel(8, size, rr, gg, bb, sample);
            dst = new BufferedImage(newModel, raster, dst.isAlphaPremultiplied(), null);
        }
        dst.createGraphics().drawImage(src, 0, 0, null);
        return dst;
    }

    public static void saveAnimatedGIF(OutputStream out, List<GifFrame> frames, int loopCount) throws Exception
    {
        ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();

        ImageOutputStream ios = ImageIO.createImageOutputStream(out);
        iw.setOutput(ios);
        iw.prepareWriteSequence(null);

        int p = 0;
        for (GifFrame frame : frames)
        {
            ImageWriteParam iwp = iw.getDefaultWriteParam();
            IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(frame.img), iwp);
            ImageUtil.configureGIFFrame(metadata, String.valueOf(frame.delay / 10L), p++, frame.disposalMethod, loopCount);
            IIOImage ii = new IIOImage(frame.img, null, metadata);
            iw.writeToSequence(ii, null);
        }

        iw.endWriteSequence();
        ios.close();
    }

    private static void configureGIFFrame(IIOMetadata meta, String delayTime, int imageIndex, String disposalMethod, int loopCount)
    {
        String metaFormat = meta.getNativeMetadataFormatName();

        if (!"javax_imageio_gif_image_1.0".equals(metaFormat))
        {
            throw new IllegalArgumentException("Unfamiliar gif metadata format: " + metaFormat);
        }

        Node root = meta.getAsTree(metaFormat);

        Node child = root.getFirstChild();
        while (child != null)
        {
            if ("GraphicControlExtension".equals(child.getNodeName()))
                break;
            child = child.getNextSibling();
        }

        IIOMetadataNode gce = (IIOMetadataNode) child;
        gce.setAttribute("userDelay", "FALSE");
        gce.setAttribute("delayTime", delayTime);
        gce.setAttribute("disposalMethod", disposalMethod);

        if (imageIndex == 0)
        {
            IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
            IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
            ae.setAttribute("applicationID", "NETSCAPE");
            ae.setAttribute("authenticationCode", "2.0");
            byte[] uo = new byte[] { 0x1, (byte) (loopCount & 0xFF), (byte) ((loopCount >> 8) & 0xFF) };
            ae.setUserObject(uo);
            aes.appendChild(ae);
            root.appendChild(aes);
        }

        try
        {
            meta.setFromTree(metaFormat, root);
        }
        catch (IIOInvalidTreeException e)
        {
            throw new Error(e);
        }
    }
}

TestGif.java:

public class TestGif {

    private JFrame frame;
    private URL url = null;

    public ArrayList<BufferedImage> getFrames(URL gif) throws IOException{
        ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
        ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
        URLConnection urlconnection = gif.openConnection();
        ir.setInput(ImageIO.createImageInputStream(urlconnection.getInputStream()), false);
        for(int i = 0; i < ir.getNumImages(true); i++){
            ImageReadParam param = ir.getDefaultReadParam();
            frames.add(ir.read(i, param));
        }
        return frames;
    }

    public TestGif() throws FileNotFoundException {
        frame = new JFrame("teste");
        try {
            url = new URL("http://sadpanda.us/images/872436-4CD65DA.gif");
        } catch (MalformedURLException ex) {
            Logger.getLogger(TestGif.class.getName()).log(Level.SEVERE, null, ex);
        }

        List<GifFrame> gifFrames = new ArrayList<GifFrame>();
        ArrayList<BufferedImage> images = null;
        try {
            images = getFrames(url);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for(BufferedImage image: images)
        {
            int transparantColor = image.getRGB(0, 0); // purple
            BufferedImage gif = ImageUtil.convertRGBAToGIF(image, transparantColor);
            long delay = 100; // every frame takes 100ms
            String disposal = GifFrame.RESTORE_TO_BGCOLOR; // make transparent pixels not 'shine through'
            gifFrames.add(new GifFrame(gif, delay, disposal));
        }

        OutputStream outputStream = new FileOutputStream("C:\\Documents and Settings\\DEVELOPER\\Desktop\\test.gif");

        int loopCount = 0; // loop indefinitely
        try {
            ImageUtil.saveAnimatedGIF(outputStream , gifFrames, loopCount);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ImageIcon icon = null;
        try {
            icon = new ImageIcon(new File("C:\\Documents and Settings\\DEVELOPER\\Desktop\\test.gif").toURI().toURL());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JLabel label = new JLabel(icon);
        icon.setImageObserver(label);
        frame.getContentPane().add(label);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                try {
                    new TestGif();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

结果:

http://sadpanda.us/images/874900-EFU9R4J.gif

该解决方案并未如此优化,因为我必须创建一个临时的动画GIF文件,该文件会删除主图像后面烦人的彩色方块,然后将其作为已标记的动画图标加载到Swing GUI中.

The solution is not so optimized since I have to create a temporary animated GIF file which removes the annoying colorful square behind the main image and later load it as a labeled animated icon in a Swing GUI.

这篇关于标签背景方块显示上的Java Gif?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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