snowflakes~

**Notes:
1) This is a java project, you use maven to download this jlayer-1.0.1.jar
and then buildpath to the environment
2) You can create a new image and music folder for those pictures在这里插入代码片

public class Snow extends JDialog{//主窗口
private static final long serialVersionUID = -6073107021198223228L;
//获取屏幕分辨率
public final static int Width = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
public final static int Height = (int) java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
public final static String property = System.getProperty(“user.dir”);
public static void main(String[] args) {
new Snow().setVisible(true);
try {
System.out.println(“你好~”+property);
new Player(new BufferedInputStream(new FileInputStream(new File(property+File.separator+“music”+File.separator+“成都.mp3”)))).play();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JavaLayerException e) { e.printStackTrace(); } } public Snow(){ this.setLocation(0,0);//position this.setSize(Width, Height);//size , the size is the screen resolution this.setUndecorated(true);//Hide the maximized and minimized column, I can't remember what it is called this.setAlwaysOnTop(true);//The window is always on top this.setDefaultCloseOperation( 0);//Prevent pressing Alt+F4 to close hua hua=new hua(); this.add(hua); AWTUtilities.setWindowOpaque(this, false); //It is recommended to use jre1.7 version to realize transparent window } }













class hua extends JPanel implements Runnable{//画布
private static final long serialVersionUID = -2986761287590314088L;
public static ArrayListlist=new ArrayList();
private BufferedImage img;
public final static String property = System.getProperty(“user.dir”);
public hua(){
try {
img=ImageIO.read(new File(property+File.separator+“image”+File.separator+“orange.jpg”));
} catch (IOException e) {
e.printStackTrace();
}//加载雪花图片
this.setOpaque(false);//设为透明的
new Thread(this).start();//启动线程
}
@Override
public void paint(Graphics g) {
super.paint(g);
for(int i=0;i<list.size();i++){
xue x=list.get(i);
g.drawImage(img, (int)x.x, (int)x.y, x.w, x.h, null);
}
}

@Override
public void run() { int fps=150;//number of frames per second int time=1000/fps; int ii=0; while(true){ long a=System.currentTimeMillis(); if(ii>3 ){ new xue(); ii=0; } ii++; for(int i=0;i<list.size();i++){//Call the run method list.get(i).run() of all snowflakes ; } repaint();//paint snow long b=System.currentTimeMillis(); long c=ba; if(time-c>0) try {Thread.sleep(time-c);} catch (InterruptedException e) { e.printStackTrace();} } } }




















class xue{//雪0
public int w,h;
public float x,y,sdx,sdy;
public xue(){ w=h=(int)(Math.random()*20+10);//random Size sdx=(float) (Math.random()*0.7+0.3);//x-axis moving speed sdy=(float) (Math.random()*0.4+0.3);//y-axis moving speed //snowflake It can only appear on the upper right side of the screen double gailv=(double)(Snow.Width+Snow.Height)/Snow.Width-1; if(Math.random()<gailv){//Snowflakes are on the top of the screen y=-h; x=(int)(Math.random() Snow.Width); } else{//Snowflakes are on the right side of the screen x=Snow.Width; y=(int)(Math.random() Snow. Height); } hua.list.add(this);//Add to the collection } public void run(){//Snowflake movement //If the snowflake exceeds the screen range, delete it from the collection

















if(x+w<0||y>Snow.Height||x+w<0||y>Snow.Height){
hua.list.remove(this);
}
y+=sdy;
x-=sdx;
}
}

Guess you like

Origin blog.csdn.net/qq_40077806/article/details/89888666