飘雪花~

**注意事项:
1)这是java项目 你用maven下载这个jlayer-1.0.1.jar
然后buildpath到环境中
2)那些图片你新建一个image和music文件夹即可在这里插入代码片

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);//位置
this.setSize(Width, Height);//大小,大小为屏幕分辨率
this.setUndecorated(true);//隐藏最大化最小化那一栏,一时想不起叫什么了
this.setAlwaysOnTop(true);//窗口始终在最顶上
this.setDefaultCloseOperation(0);//防止按Alt+F4关闭
hua hua=new hua();
this.add(hua);
AWTUtilities.setWindowOpaque(this, false);//实现透明窗口建议使用jre1.7版本
}
}

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;//每秒帧数
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++){//调用所有雪花的run方法
list.get(i).run();
}
repaint();//画雪
long b=System.currentTimeMillis();
long c=b-a;
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);//随机大小
sdx=(float) (Math.random()*0.7+0.3);//x轴移动速度
sdy=(float) (Math.random()*0.4+0.3);//y轴移动速度
//雪花出现的位置只能在屏幕的上右方
double gailv=(double)(Snow.Width+Snow.Height)/Snow.Width-1;
if(Math.random()<gailv){//雪花在屏幕上方
y=-h;
x=(int)(Math.random()Snow.Width);
}
else{//雪花在屏幕右方
x=Snow.Width;
y=(int)(Math.random()Snow.Height);
}
hua.list.add(this);//添加进集合
}
public void run(){//雪花的移动
//如果雪花超出屏幕范围的话就从集合中删掉
if(x+w<0||y>Snow.Height||x+w<0||y>Snow.Height){
hua.list.remove(this);
}
y+=sdy;
x-=sdx;
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值