按住按钮以旋转图像

时间:2012-12-11 04:23:30

标签: java image

我正在尝试为我的强盗机器在Java上创建一个保持按钮,按下它时将保持该图像,其他2个图像将继续旋转。有人知道允许这种情况发生的代码吗?

if (e.getSource()== btnaddcash){
                txtbalance.setForeground(Color.red);
                cash = cash + 100;
                txtbalance.setText(cash + "");

if (e.getSource() == btnpic1){

1 个答案:

答案 0 :(得分:1)

基本概念是,你想在按钮被“武装”时旋转图像(或采取一些动作)。

您可以通过这种方式向按钮添加ChangeListener并监控ButtonModel#isArmed状态。

enter image description here

public class ChangeButton {

    public static void main(String[] args) {
        new ChangeButton();
    }

    public ChangeButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new SlotPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class SlotPaneextends JPanel {

        private SpinPane spinPane;
        private JButton button;

        public SlotPane() {
            spinPane = new SpinPane();
            button = new JButton("Press");
            // I've attached directly to the model, I don't
            // think this is required, simply attaching a change
            // listener to the button achieve the same result.
            button.getModel().addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    spinPane.setSpinning(button.getModel().isArmed());
                }
            });

            setLayout(new BorderLayout());
            add(spinPane);
            add(button, BorderLayout.SOUTH);
        }
    }

    public class SpinPane extends JPanel {

        private BufferedImage mole;
        private Timer timer;
        private float angel = 0;

        public SpinPane() {
            try {
                mole = ImageIO.read(new File("mole.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            timer = new Timer(15, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angel += 15;
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
        }

        @Override
        public Dimension getPreferredSize() {
            return mole == null ? super.getPreferredSize() : new Dimension(mole.getWidth(), mole.getHeight());
        }

        public void setSpinning(boolean spinning) {
            if (spinning) {
                if (!timer.isRunning()) {
                    timer.restart();
                }
            } else {
                timer.stop();
            }
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (mole != null) {

                int x = (getWidth() - mole.getWidth()) / 2;
                int y = (getHeight() - mole.getHeight()) / 2;
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angel), getWidth() / 2, getHeight() / 2));
                g2d.drawImage(mole, x, y, this);
                g2d.dispose();

            }
        }
    }
}