博客列表 >Java动态雪花屏保

Java动态雪花屏保

亚哥的博客
亚哥的博客原创
2018年09月07日 07:23:56781浏览
package screen;

/*
 * JFrame
 */
import javax.swing.JFrame;

import com.sun.awt.AWTUtilities;

public class ScreenFrame {
   public static void main(String[] args){
       JFrame f=new JFrame();
       ScreenPanel p=new ScreenPanel();
       f.add(p);
       f.setSize(1366, 768);
       f.setUndecorated(true);//去边框
       AWTUtilities.setWindowOpaque(f, false);//透明设置
       f.setLocationRelativeTo(null);//居中
       f.setDefaultCloseOperation(3);
       f.setVisible(true); 
       f.setTitle("屏幕保护");
       f.addMouseListener(p);//注册鼠标监听器
       f.addKeyListener(p);
       f.addMouseMotionListener(p);
       p.move();
   }   
}
package screen;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
//ctrl+d 删除
//ctrl +shift+o导包
//单继承多实现
@SuppressWarnings("serial")
public class ScreenPanel extends JPanel implements MouseListener,KeyListener,MouseMotionListener{
    
    int x=200,y=300;
    //定义一个数组来管理雪花x的坐标
    int[] bx=new int[100];
    //定义一个数组来管理雪花y的坐标
    int[] by=new int[100];
    //定义一个数组来管理小球的路径
    int dir[] =new int [100];
    //初始化:赋值在构造方法中
    //语法:public 类名(){} 没有返回值 方法名和类名一样。
    public ScreenPanel(){
        for(int i=0;i<bx.length;i++){
            bx[i]=(int)(Math.random()*1366);
            by[i]=(int)(Math.random()*768);
            dir[i]=(int)(Math.random()*4)+1;}
        }
    
    public void paint(Graphics g){//画笔 Graphics
        super.paint(g);
        setBackground(Color.WHITE);
        setOpaque(false);//透明设置
        //Font f=new Font("楷体",Font.PLAIN,24);创建字体对象
        //g.setFont(f);设置字体
        g.setFont(new Font("微软雅黑",Font.PLAIN,24));
        g.setColor(Color.RED);
        for(int i=1;i<50;i++)
       {
           g.setColor(Color.WHITE);
           g.setFont(new Font("宋体",Font.PLAIN,25));
           //g.drawString("*",a,b);
           Image img=new ImageIcon("src/2.png").getImage();
            g.drawImage(img,bx[i], by[i], null);   } 
       }
    //自定义方法
    public void move(){
        //匿名内部类
        new Thread(){
        public void run(){
         while(true){
         //改坐标 
         for(int i=1;i<100;i++){
                if (dir[i] == 1) {
                    bx[i] += 10;
                    by[i] += 10;}
              
                if (dir[i] == 2){
                    bx[i] -= 10;
                    by[i] += 10;}
                
                if (dir[i] == 3) {
                    bx[i] -= 10;
                    by[i] -= 10;}
                
                if (dir[i] == 4) {
                    bx[i] += 10;
                    by[i] -= 10;}
                //边界值
                if (bx[i] > 1366) {
                    if (dir[i] == 1) {
                        dir[i] = 2;}
                    if (dir[i] == 4) {
                        dir[i] = 3;}}
                
                if (bx[i]<0) {
                    if (dir[i] == 2) {
                        dir[i] = 1;}
                    if (dir[i] == 3) {
                        dir[i] = 4;}}
                
                if (by[i]>768) {
                    if (dir[i] == 1){
                        dir[i] = 4;}
                    if (dir[i] == 2){
                        dir[i]= 3;}}
                
                if (by[i]<0) {
                    if (dir[i] == 4){
                        dir[i] = 1;}
                    if (dir[i] == 3){
                        dir[i] = 2;}}}
             repaint();
             try{
             //休眠
             Thread.sleep(50);}
             catch(InterruptedException e){
         e.printStackTrace();}} }}.start();}
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO 自动生成的方法存根
        //点击鼠标退出
        System.exit(0);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO 自动生成的方法存根
        
    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO 自动生成的方法存根
        
    }
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO 自动生成的方法存根
        System.exit(0);
    }
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO 自动生成的方法存根
        
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO 自动生成的方法存根
        System.exit(0);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO 自动生成的方法存根
    
    }
    

}


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议