本文介绍了创建的ImageIcon是另一个的镜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会喜欢知道如果有是创建一个的ImageIcon那是另外的ImageIcon的镜的方法。

I'll like to know if there is a way to create a ImageIcon that is the mirror of another ImageIcon.

搜索在谷歌,我发现如何使用AWT很多图书馆做。

Searching on Google, I found how to do it by using many AWT libraries.

有没有办法与Swing做呢?如果不是这样,我仍然有与AWT方法的烦恼:

Is there a way to do it with Swing ? If not, I'm still having troubles with the AWT method :

我要镜的ImageIcon是一个gif动画(与含有透明色)和AWT方法返回一个非透明(透明颜色变为不透明的黑色)和非gif动画。

The ImageIcon I want to mirror is a animated gif (with contain a transparent color) and the AWT method returns a non-transparent (the transparent color is changed to opaque black) and non-animated gif.

任何想法如何保持动画和透明色?

Any ideas how to keep the animation and the transparent color?

下面是AWT code,我发现(rangerStand是原始的ImageIcon):

Here is the AWT code I found (rangerStand being the original ImageIcon) :

 Image reversed = rangerStand.getImage();
 BufferedImage bufferedImage = new BufferedImage(reversed.getWidth(null), reversed.getHeight(null), BufferedImage.TYPE_INT_RGB);
 Graphics gb = bufferedImage.getGraphics();
 gb.drawImage(reversed, 0, 0, null);
 gb.dispose();

 AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
 tx.translate(-reversed.getWidth(null), 0);
 AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
 bufferedImage = op.filter(bufferedImage, null);
 ImageIcon lol = new ImageIcon(bufferedImage);
 this.sprite.setIcon(lol);

感谢您的阅读。

推荐答案

您可能会想尝试和子类ImageIcon的类和油漆扭转形象。试试这件作品code的,它可能做的伎俩(我还没有与动画透明的gif尝试,但它应该工作)

You might want to try and subclassing the ImageIcon class and paint the image reversed. Try this piece of code, it might do the trick (i haven't tried with animated transparent gif but it should work):

编辑:我稍微改变了code和动画GIF进行了测试。它的工作原理!

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;

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


/**
 * @author Savvas Dalkitsis
 */
public class Test {

    public static void main(String[] args) {
    	JFrame f = new JFrame("Test");
    	JLabel l = new JLabel();
    	ImageIcon io = new MirrorImageIcon("test.gif");
    	l.setIcon(io);
    	f.getContentPane().add(l);
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.pack();
    	f.setVisible(true);
    }

}

@SuppressWarnings("serial")
class MirrorImageIcon extends ImageIcon {

    public MirrorImageIcon(String filename) {
    	super(filename);
    }

    @Override
    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
    	Graphics2D g2 = (Graphics2D)g.create();
    	g2.translate(getIconWidth(), 0);
    	g2.scale(-1, 1);
    	super.paintIcon(c, g2, x, y);
    }

}

这篇关于创建的ImageIcon是另一个的镜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 14:34
查看更多