0

I'm trying to make a program that will work and function like simple horse races, but I can't set different speeds. As far as I can see, see that when the timer gets different values, for example: I have two pictures, one has a timer 40 and the other 80, they move at the same speed, but a picture that has 80 goes chopping, or rather has fewer clocks, and this one I have 40 goes more smooth.

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

    }
Mihailo E
  • 1
  • 4
  • 1
    1) What's an `xpos` or an `enemyRed`? After adding import statements and a closing `}` the code still showed compilation errors based on those missing attributes. But rather than try to explain in words, [edit] to add a [mre]. 2) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jul 06 '21 at 10:05
  • 1
    .. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jul 06 '21 at 10:06
  • But AFAIU (which is 'not much') .. `enemyPink extends JPanel` should be more like `AnimatedImages extends JPanel` - the plural form is meant to indicate that the one panel should paint *every* image. As to *"one has a timer 40 and the other 80"* there are two ways I can think to use a single timer for two images: 1) Move them both on every stroke of a 40 ms timer, but move one half the distance of the other. 2) Keep a counter that increments each paint, and move the 'slower' image only when the counter is even. – Andrew Thompson Jul 06 '21 at 10:14

1 Answers1

-1

It is best to read the standard documentation a bit and look at some tutorial on how to do it. The code is very confusing and I can't solve it. I will post a link: https://youtu.be/Kmgo00avvEw