两幅图像之间快速切换?

时间:2012-12-03 01:51:28

标签: java image

我想让一个物体张开嘴并使用两个快速切换的图像将其关闭。我尝试使用for循环,但它落后于我的游戏。

 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);

2 个答案:

答案 0 :(得分:3)

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

您不应该在EDT的内容中执行任何可能阻止它的操作(例如循环或I / O),因为这会阻止EDT(除其他事项外)处理绘制请求。

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

以下使用javax.swing.Timer在两张图片之间切换......

enter image description here enter image description here

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();
        }

    }

}

答案 1 :(得分:0)

每次都不要创建图标。在启动时创建两个图像,然后切换回来 在运行时。

if(direction == Constant.UP){
    image = open;
}else {
    image = closed;
}

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