0

I want to create an animation that looks like what is shown in this link: Link.

To be more precise I want my trees to behave like a cloud in the link. I did animation with the trees, but it doesn't work how I want. Below, I have posted my code. Please, tell me how to create it?

package project;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Trees extends JPanel implements ActionListener {
    Timer timer = new Timer(10, this);
    BufferedImage img1;
    BufferedImage img2;
    int x = 0, velX = 2;
    int y = 0, velY = 2;

    public Trees(BufferedImage[] images) {
        img1 = images[0];
        img2 = images[1];
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img1, x, 100, this);
        g.drawImage(img2, 300, y, this);

        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(x < 0 || x > 450)
            velX = -velX;

        if(y < 0 || y > 450)
            velY = -velY;

        x = x + velX;
        y = y + velY;
        repaint();
    }

    public static void main(String[] args) throws IOException {
        String[] ids = {"mt1", "mt2"};
        BufferedImage[] images = new BufferedImage[ids.length];
        for(int j = 0; j < images.length; j++) {
            String path = "images\\" + ids[j] + ".png";
            images[j] = ImageIO.read(new File(path));
        }
        Trees test = new Trees(images);
        JFrame f = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.BLUE);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(test);
        f.setSize(600,600);
        f.setVisible(true);
    }   
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
dvlad
  • 21
  • 3
  • What does your current code do? Does it draw at all? Note that you should not start the Timer within paintComponent, and that method should be responsible for painting and painting only. – Hovercraft Full Of Eels Apr 04 '20 at 18:14
  • 2
    *"I want my trees to behave like a cloud in the link"* Do you mean travelling at different speeds, like some are close and others are far away? BTW: 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. [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 Apr 04 '20 at 18:44
  • 1
    Check out: https://stackoverflow.com/questions/16138363/infinite-background-for-game for a couple of example to give you some ideas. One example is provided by @AndrewThompson. The examples use the keyboard to generate the animation. You would need to modify that to use a Swing Timer. – camickr Apr 04 '20 at 18:50
  • 2
    Hand-drawn animation in the past used layers to move different objects at different speeds. You can use a similar technique in Swing. Create several images, and draw them onto the same drawing panel (JPanel). By moving objects in the different layers different distances, you can create the illusion of depth. 70-year-old Disney cartoons used as many as 7 layers in their animation. – Gilbert Le Blanc Apr 04 '20 at 23:18
  • Can't you simply paint the GIF in the `JPanel`? Are you asked to implement this as an animation, or can you use the image directly? Weird question, I know, but it is not granted yet which of the two you mean to do. Also looking at your code, suggests you can just paint the image itself. – gthanop Apr 05 '20 at 16:41

0 Answers0