java创建一个简单的 "雨" 效果
  pOZa2RQhObig 2023年11月24日 16 0

要创建一个简单的 "雨" 效果,我们可以使用Java的Thread类来模拟雨滴下落的过程。以下是一个简单的例子:

java创建一个简单的 "雨" 效果_java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class RainPanel extends JPanel implements ActionListener {

    private static final int WIDTH = 600;
    private static final int HEIGHT = 400;
    private static final int RAIN_DROPS = 50;
    private static final int SPEED = 3;
    private static final int RANDOM_START_Y = 50;
    private static final int RANDOM_START_X = 50;
    private static final int RANDOM_WIDTH = 5;
    private static final int RANDOM_HEIGHT = 5;

    private BufferedImage rainImage;
    private Timer timer;
    private int[] xPositions;
    private int[] yPositions;

    public RainPanel() {
        // Load the rain image
        try {
            rainImage = new ImageIcon(getClass().getResource("rain.png")).getImage();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Initialize the positions of the rain drops
        xPositions = new int[RAIN_DROPS];
        yPositions = new int[RAIN_DROPS];
        for (int i = 0; i < RAIN_DROPS; i++) {
            xPositions[i] = RANDOM_START_X + (int) (Math.random() * (WIDTH - RANDOM_START_X));
            yPositions[i] = RANDOM_START_Y + (int) (Math.random() * (HEIGHT - RANDOM_START_Y));
        }

        // Set up the timer
        timer = new Timer(SPEED, this);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // Draw the rain drops
        for (int i = 0; i < RAIN_DROPS; i++) {
            Image rainDrop = rainImage.getScaledInstance(RANDOM_WIDTH, RANDOM_HEIGHT, Image.SCALE_SMOOTH);
            g2d.drawImage(rainDrop, xPositions[i], yPositions[i], this);

            // Move the rain drop down
            yPositions[i] += SPEED;
            if (yPositions[i] >= HEIGHT) {
                // Reset the position if the rain drop goes out of the screen
                yPositions[i] = RANDOM_START_Y + (int) (Math.random() * (HEIGHT - RANDOM_START_Y));
                xPositions[i] = RANDOM_START_X + (int) (Math.random() * (WIDTH - RANDOM_START_X));
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public static void main(String[] args) {
        // Create the frame
        JFrame frame = new JFrame("Rain Effect");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Set the content pane to our panel
        RainPanel panel = new RainPanel();
        frame.setContentPane(panel);

        // Set the size and show the frame
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }
}

在这个例子中,我们使用了一个Timer来定期调用actionPerformed方法,从而更新雨滴的位置并重绘组件。paintComponent方法中

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月24日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   53   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   107   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
pOZa2RQhObig