java 实现的飘雪花效果

时间:2022-12-22 08:39:01

//My_Snow 类

package My_Snow;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.awt.AWTUtilities;
public class My_Snow {
 static ArrayList<SnowAttributes> snowList =new ArrayList<SnowAttributes>();
 static ArrayList<SnowAttributes> snowremoveList =new ArrayList<SnowAttributes>();
 public static void main(String[] args) {
  final JFrame jf = new JFrame();
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
  jf.setUndecorated(true);//frame修饰
  AWTUtilities.setWindowOpaque(jf, false);
  
//  final Rectangle bound = jf.getBounds();//放在这里报错
  
  final JPanel jp = new JPanel(){
    int count=30;
    Random ran = new Random(47);
   @Override
   public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);
    Rectangle bound = jf.getBounds();
    if(snowList.size()<30){
      SnowAttributes snow = new SnowAttributes();
      snow.setSnowX(ran.nextInt(bound.width));
      snow.setSnowY(ran.nextInt(bound.height));
      snow.setAngle(ran.nextInt(360));
      snowList.add(snow);
    }
    Iterator<SnowAttributes> i = snowList.iterator();
    int temp =0;
    while(i.hasNext()){
     SnowAttributes snow = i.next();
     snow.setSnowX(snow.getSnowX()+(temp++)/10);
     snow.setSnowY(snow.getSnowY()+(temp++)/5);
     snow.setAngle(snow.getAngle()*57+5);
     if(snow.getSnowX()>bound.width||snow.getSnowY()>bound.height){
      snowremoveList.add(snow);
     }
     if(snow.getAngle()>2*Math.PI){
      snow.setAngle(0);
     }
    }
    Iterator<SnowAttributes> i_remove = snowremoveList.iterator();
    while(i_remove.hasNext()){
     snowList.remove(i_remove.next());
    }
    Image image=null;
    try {
     image = ImageIO.read(new File("src/My_Snow/xuehua.png"));
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    Iterator<SnowAttributes> i2 = snowList.iterator();
    while(i2.hasNext()){
     SnowAttributes snow = i2.next();
     Graphics2D g2d =  (Graphics2D) g;
     g2d.translate(snow.getSnowX(), snow.getSnowY());
     g2d.rotate(snow.getAngle());
     g.drawImage(image, 0, 0, null);
     g2d.rotate(-snow.getAngle());
     g2d.translate(-snow.getSnowX(), -snow.getSnowY());
    }
    
    
    
   }
   
   
  };
  jp.setOpaque(false);
  jf.setContentPane(jp);
  jf.setVisible(true);
  Thread thread = new Thread(){
   public void run() {
    while(true){
     try {
      Thread.sleep(20);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     jp.repaint();
    }
    
   };
  };
  thread.start();
  
  
 }
}
//SnowAttributes类--------------------------------------------------------------------------------
package My_Snow;
public class SnowAttributes {
  private int snowX ;
  private int snowY ;
  private double angle;//弧度
 public int getSnowX() {
  return snowX;
 }
 public void setSnowX(int snowX) {
  this.snowX = snowX;
 }
 public int getSnowY() {
  return snowY;
 }
 public void setSnowY(int snowY) {
  this.snowY = snowY;
 }
 public double getAngle() {
  return angle;
 }
 public void setAngle(double angle) {//angle参数是角度
  this.angle = Math.toRadians(angle);//转为弧度
 }
}
 

效果如下

java 实现的飘雪花效果