在两个图像之间快速切换? [英] Fast switching between two images?

查看:52
本文介绍了在两个图像之间快速切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个可以快速切换的图像来使物体张开并合上.我尝试了for循环,但它落后于我的游戏.

I want to make an object to open mouth and close it using two images that switch quickly. I tried with a for loop but it lagged my game.

 if(direction == Constant.UP){

        ImageIcon i = new ImageIcon("src\\images\\pacman up.png");
        image = i.getImage();

        ImageIcon i2 = new ImageIcon("src\\images\\pacman left.png");
        image = i2.getImage();

        }
 G.drawImage(image, x, y, 20,20,null);

推荐答案

Swing中的任何动画都需要考虑

Any animation in Swing needs to take into consideration the Event Dispatching Thread.

您切勿在EDT内容内执行任何可能阻止它的操作(例如循环或I/O),因为这将阻止EDT(除其他事项外)处理绘画请求.

You should NEVER perform any action within the content of the EDT that may block it (such as loops or I/O) as this will prevent the EDT from (amongst other things) processing paint requests.

您应该始终使用能够支持双缓冲的表面,例如 JPanel ,因为这将有助于消除闪烁

You should always use a surface capable of supporting double buffer, such as JPanel as this will help eliminate flickering

以下使用 javax.swing.Timer 在两个图像之间切换...

The following uses a javax.swing.Timer to switch between the two images...

public class TestPacMan {

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

    public TestPacMan() {
        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 PacManPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PacManPane extends JPanel {

        private BufferedImage pacOpened;
        private BufferedImage pacClosed;
        private BufferedImage frame;
        private boolean opened = true;

        public PacManPane() {
            try {
                pacOpened = ImageIO.read(new File("PC-Closed.png"));
                pacClosed = ImageIO.read(new File("PC-Opened.png"));
                frame = pacOpened;
            } catch (IOException exp) {
                exp.printStackTrace();
            }

            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    opened = !opened;
                    frame = opened ? pacOpened : pacClosed;
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (frame != null) {
                int x = (getWidth() - frame.getWidth()) / 2;
                int y = (getHeight() - frame.getHeight()) / 2;
                g2d.drawImage(frame, x, y, this);
            }
            g2d.dispose();
        }

    }

}

这篇关于在两个图像之间快速切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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