簡體   English   中英

如何添加更多以不同速度移動的圖像

[英]How to add more images moving at a different speed

我正在嘗試制作一個可以像簡單的賽馬一樣工作和運行的程序,但我無法設置不同的速度。 就我所見,看到當計時器獲得不同的值時,例如:我有兩張圖片,一個有一個計時器 40,另一個是 80,它們以相同的速度移動,但是有 80 的圖片會斬波,或者更確切地說,時鍾更少,而我擁有 40 的時鍾更流暢。

public class AnimatedThing {

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

                JFrame frame = new JFrame("Trke");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(400, 600));
                frame.setLayout(new BorderLayout());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                frame.setLayout(new GridLayout(4, 1));
                frame.setBackground(Color.white);

                enemyRed enemyPink = new enemyPink(); // sub-panel 1
                frame.add(enemyPink);
                frame.setLocationRelativeTo(null);
            }
        });
    }
     public class enemyPink extends JPanel {

        private BufferedImage enemyPink;
        public enemyPink() {
            try {
                enemyPink = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEB\\images\\enemy - pink.png"));
                Timer timer2 = new Timer(20, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        xPos += direction;
                        if (xPos + enemyPink.getWidth() > getWidth()) {
                        } else {
                            repaint();
                        }
                    }

                });
                timer2.setRepeats(true);
                timer2.setCoalesce(true);
                timer2.start();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(enemyPink, xPos, 0, this);
        }

    }

最好先閱讀一下標准文檔,然后查看一些有關如何操作的教程。 代碼很混亂,我無法解決。 我會發一個鏈接: https : //youtu.be/Kmgo00avvEw

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM