• 大小: 23KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-03
  • 语言: Java
  • 标签: java  雪花  效果  

资源简介

java 实现的飘雪花效果.包含代码和所用到的图片资源。欢迎指正

资源截图

代码片段和文件信息

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 snowList =new ArrayList();
static ArrayList snowremoveList =new ArrayList();
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 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 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 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();
}


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        232  2014-04-20 09:40  Test_Snow\.classpath

     文件        385  2014-04-24 19:15  Test_Snow\.project

     文件        598  2014-04-20 09:33  Test_Snow\.settings\org.eclipse.jdt.core.prefs

     文件       2919  2014-04-24 20:06  Test_Snow\bin\My_Snow\My_Snow$1.class

     文件        799  2014-04-24 20:06  Test_Snow\bin\My_Snow\My_Snow$2.class

     文件       1421  2014-04-24 20:06  Test_Snow\bin\My_Snow\My_Snow.class

     文件        924  2014-04-24 19:39  Test_Snow\bin\My_Snow\SnowAttributes.class

     文件       5615  2014-04-24 20:06  Test_Snow\bin\My_Snow\xuehua.png

     文件       2432  2014-04-24 19:17  Test_Snow\bin\My_Snow$1.class

     文件        756  2014-04-24 19:17  Test_Snow\bin\My_Snow$2.class

     文件       1273  2014-04-24 19:17  Test_Snow\bin\My_Snow.class

     文件       1143  2014-04-24 19:15  Test_Snow\bin\Test01$1.class

     文件        932  2014-04-24 19:15  Test_Snow\bin\Test01.class

     文件        689  2014-04-24 19:15  Test_Snow\bin\Test02$1.class

     文件        933  2014-04-24 19:15  Test_Snow\bin\Test02.class

     文件       3052  2014-04-24 20:06  Test_Snow\src\My_Snow\My_Snow.java

     文件        510  2014-04-24 19:39  Test_Snow\src\My_Snow\SnowAttributes.java

     文件       5615  2014-04-24 20:06  Test_Snow\src\My_Snow\xuehua.png

     目录          0  2014-04-24 20:08  Test_Snow\bin\My_Snow

     目录          0  2014-04-24 19:15  Test_Snow\bin\snow

     目录          0  2014-04-24 20:08  Test_Snow\src\My_Snow

     目录          0  2014-04-24 20:08  Test_Snow\.settings

     目录          0  2014-04-24 20:08  Test_Snow\bin

     目录          0  2014-04-24 20:08  Test_Snow\src

     目录          0  2014-04-24 20:08  Test_Snow

----------- ---------  ---------- -----  ----

                30228                    25


评论

共有 条评论