0

素早く切り替わる 2 つの画像を使用して、口を開閉するオブジェクトを作成したいと考えています。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);
4

2 に答える 2

3

Swing のすべてのアニメーションは、 Event Dispatching Threadを考慮する必要があります。

EDT をブロックする可能性のあるアクション (ループや I/O など) を EDT のコンテンツ内で実行しないでください。これにより、EDT が (特に) ペイント要求を処理できなくなります。

JPanelちらつきをなくすのに役立つなど、常にダブル バッファをサポートできる表面を使用する必要があります。

以下は、 aを使用しjavax.swing.Timerて 2 つの画像を切り替えます...

ここに画像の説明を入力ここに画像の説明を入力

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

    }

}
于 2012-12-03T03:19:38.973 に答える
0

毎回アイコンを作成しないでください。起動時に 2 つのイメージを作成し、実行時に切り替えるだけです。

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

G.drawImage(image, x, y, 20,20,null);
于 2012-12-03T01:57:56.680 に答える