JLabel中的Java旋转图标

时间:2015-11-16 20:00:58

标签: java image rotation icons jlabel

您好我在尝试在JLabel内旋转图像时遇到问题。我从StackOverflow获取此代码,并且我试图稍微更改它,以便在Tab中旋转的图像在JLabel中旋转。

公共类ProgressTabbedPane {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("RotatingIcon"); 
            JTabbedPane tabbedPane = new JTabbedPane();
            JLabel lable = new JLabel();



            tabbedPane.addTab("Searching", new RotatingIcon(new ImageIcon("disk.png"), tabbedPane, 10), new JLabel( /*new ImageIcon( "resources/images/rotatingIcon.gif" )*/));               
            frame.getContentPane().add(tabbedPane);                
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //frame.setUndecorated(true);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

        }
    });
}

private static class RotatingIcon implements Icon {

    private final Icon delegateIcon;
    private double angleInDegrees = 90;
    final private Timer rotatingTimer;

    private RotatingIcon(Icon icon, final JComponent component, int vrotating) {
        delegateIcon = icon;
        rotatingTimer = new Timer(vrotating, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                angleInDegrees = angleInDegrees + 1;
                if (angleInDegrees == 360) {
                    angleInDegrees = 0;
                }
                component.repaint();

            }
        });
        rotatingTimer.setRepeats(false);
        rotatingTimer.start();

    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        rotatingTimer.stop();
        Graphics2D g2 = (Graphics2D) g.create();
        int cWidth = delegateIcon.getIconWidth() / 2;
        int cHeight = delegateIcon.getIconHeight() / 2;
        Rectangle r = new Rectangle(x, y, delegateIcon.getIconWidth(), delegateIcon.getIconHeight());
        g2.setClip(r);
        AffineTransform original = g2.getTransform();
        AffineTransform at = new AffineTransform();
        at.concatenate(original);
        at.rotate(Math.toRadians(angleInDegrees), x + cWidth, y + cHeight);
        g2.setTransform(at);
        delegateIcon.paintIcon(c, g2, x, y);
        g2.setTransform(original);
        rotatingTimer.start();
    }

    @Override
    public int getIconWidth() {
        return delegateIcon.getIconWidth();
    }

    @Override
    public int getIconHeight() {
        return delegateIcon.getIconHeight();
    }
}

} 这是有效的,图像是旋转的。 但是当我改变它时。

公共类ProgressTabbedPane {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("RotatingIcon"); 
            JTabbedPane tabbedPane = new JTabbedPane();
            JLabel lable = new JLabel();




            lable.setIcon(new RotatingIcon(new ImageIcon(disk.png"), tabbedPane, 10));                
            frame.getContentPane().add(lable);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //frame.setUndecorated(true);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

        }
    });
}

private static class RotatingIcon implements Icon {

    private final Icon delegateIcon;
    private double angleInDegrees = 90;
    final private Timer rotatingTimer;

    private RotatingIcon(Icon icon, final JComponent component, int vrotating) {
        delegateIcon = icon;
        rotatingTimer = new Timer(vrotating, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                angleInDegrees = angleInDegrees + 1;
                if (angleInDegrees == 360) {
                    angleInDegrees = 0;
                }
                component.repaint();

            }
        });
        rotatingTimer.setRepeats(false);
        rotatingTimer.start();

    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        rotatingTimer.stop();
        Graphics2D g2 = (Graphics2D) g.create();
        int cWidth = delegateIcon.getIconWidth() / 2;
        int cHeight = delegateIcon.getIconHeight() / 2;
        Rectangle r = new Rectangle(x, y, delegateIcon.getIconWidth(), delegateIcon.getIconHeight());
        g2.setClip(r);
        AffineTransform original = g2.getTransform();
        AffineTransform at = new AffineTransform();
        at.concatenate(original);
        at.rotate(Math.toRadians(angleInDegrees), x + cWidth, y + cHeight);
        g2.setTransform(at);
        delegateIcon.paintIcon(c, g2, x, y);
        g2.setTransform(original);
        rotatingTimer.start();
    }

    @Override
    public int getIconWidth() {
        return delegateIcon.getIconWidth();
    }

    @Override
    public int getIconHeight() {
        return delegateIcon.getIconHeight();
    }
}

}

这停止了工作,对不起,如果这是一个愚蠢的问题,但我似乎没有找到答案。

谢谢

1 个答案:

答案 0 :(得分:0)

绘画方法只应用于绘画。它不应该停止/启动计时器。所以我猜你需要从绘制方法中删除Timer逻辑,然后将Timer设置为重复,以便生成连续事件。

有关替代方法,请查看Animated Icon

Animated Icon将保存一个图标列表,以便根据计时器顺序显示。当Timer触发循环中的下一个Icon时,显示。您可以将动画配置为连续或指定显示每个图标的周期数。

注意:此解决方案应该更有效,因为它只重新绘制Icon,而不是整个Component。

如果您不想创建动画的所有图标,那么您可以使用Rotated Icon。使用此类,您可以设置Icon的旋转度。然后Timer与类完全分离。然后当Timer代码触发时,你会增加旋转度。

使用AnimatedIcon

的简单示例
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("1", new JPanel());
        tabbedPane.add("2", new JPanel());
        add(tabbedPane);

        AnimatedIcon icon = new AnimatedIcon(tabbedPane, 250, 3);
        ImageIcon duke = new ImageIcon( "copy16.gif" );
        icon.addIcon( duke );

        for (int angle = 30; angle < 360; angle += 30)
        {
            icon.addIcon( new RotatedIcon(duke, angle) );
        }

        tabbedPane.setIconAt(0, icon);
        icon.start();
    }


    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}