Java Swing中的动画

huangapple 未分类评论43阅读模式
英文:

Animation in java swing

问题

以下是翻译好的内容:

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);
    }
}
英文:

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 &lt; 0 || x &gt; 450)
		    velX = -velX;
	
	    if(y &lt; 0 || y &gt; 450)
		    velY = -velY;
	
	    x = x + velX;
	    y = y + velY;
	    repaint();
    }

    public static void main(String[] args) throws IOException {
        String[] ids = {&quot;mt1&quot;, &quot;mt2&quot;};
        BufferedImage[] images = new BufferedImage[ids.length];
        for(int j = 0; j &lt; images.length; j++) {
            String path = &quot;images\\&quot; + ids[j] + &quot;.png&quot;;
            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);
    }	
}

huangapple
  • 本文由 发表于 2020年4月5日 02:11:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61032611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定