目录
一、效果
二、教程
三、代码
首先我们先看效果,雪花是流动的,从上往下,依次变大,十分浪漫!(效果是动态的)
1、使用IDEA搭建一个项目,项目名称:MyStar(可根据自己的喜好)
具体搭建过程可看博文用IDEA构建一个简单的Java程序范例,这里就不详细说了。
2、MyStar.class
(1)导入包
import java.awt.Color; import java.awt.Component; import javax.swing.JFrame;(2)主函数
在这里我的panel大小设置的是800*600,大家可以根据自己的喜好设置的更大一些。
public static void main(String[] args) { JFrame frame = new JFrame("满天星"); MyStarPanel panel = new MyStarPanel(); frame.add(panel); Thread t = new Thread(panel); t.start(); frame.setSize(800, 600); frame.setVisible(true); frame.setLocationRelativeTo((Component)null); frame.setDefaultCloseOperation(3); frame.setBackground(Color.BLACK); }3、 MyStarPanel.class
(1)导入包
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel;(2)类的继承,接口的实现
JPanel类:面板组件,非顶层容器 public class MyStarPanel extends JPanel implements Runnable {}(3)数据类型的定义
xx,yy:int,为雪花出现的坐标image:BufferedImage,为图片类型fonts:int,为雪花字体的大小 int[] xx = new int[100]; int[] yy = new int[100]; BufferedImage image; int[] fonts = new int[100];(4)画布函数MyStarPanel()
for循环:雪花出现的位置随机,一次出现100个MyStarPanel.class.getResource("shu3.jpg"):获取图片的路径。
用法:类名.class.getResource("")
public MyStarPanel() { for(int i = 0; i < 100; ++i) { this.xx[i] = (int)(Math.random() * 800); this.yy[i] = (int)(Math.random() * 600); } try { this.image = ImageIO.read(MyStarPanel.class.getResource("shu3.jpg")); } catch (IOException var2) { var2.printStackTrace(); } }注:图片是我自己提前下载好的,并且放在 项目 --> target --> classes --> MyStar 下
(5)画笔函数paint()
画笔名称g雪花的颜色设置为白色g.setColor(Color.WHITE);for循环:设置雪花“*”的大小,字体。在这里,我们使雪花从上到下依次增大,使其更真实。 public void paint(Graphics g) { g.drawImage(this.image, 0, 0, 800, 600, (ImageObserver)null); g.setColor(Color.WHITE); for(int i = 0; i < 100; ++i) { Font ft = new Font("微软雅黑", 1, this.fonts[i]); g.setFont(ft); g.drawString("*", this.xx[i], this.yy[i]); } }(6)线程函数run()
for循环:循环100次每次循环,yy[i] +11、判断边界:我们的画布大小为:800*600,超过100,令yy[i]=0
2、设定不同部分的雪花的大小。
在这里,我设置了三部分:
0 - 150:font[i] = 18150 - 500:font[i] = 22500 - 600:font[i] = 32这里大家可以根据自己的喜欢更改。
public void run() { while(true) { for(int i = 0; i < 100; ++i) { int var1 = this.yy[i]++; if (this.yy[i] > 600) { this.yy[i] = 0; } if (this.yy[i] > 0 && this.yy[i] < 150) { this.fonts[i] = 18; } else if (this.yy[i] > 150 && this.yy[i] < 500) { this.fonts[i] = 22; } else { this.fonts[i] = 32; } } try { Thread.sleep(100); } catch (InterruptedException var2) { var2.printStackTrace(); } this.repaint(); } } }1、MyStar.class
package MyStar; import java.awt.Color; import java.awt.Component; import javax.swing.JFrame; public class MyStar { public static void main(String[] args) { JFrame frame = new JFrame("满天星"); MyStarPanel panel = new MyStarPanel(); frame.add(panel); Thread t = new Thread(panel); t.start(); frame.setSize(800, 600); frame.setVisible(true); frame.setLocationRelativeTo((Component)null); frame.setDefaultCloseOperation(3); frame.setBackground(Color.BLACK); } }2、MyStarPanel.class
package MyStar; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel; public class MyStarPanel extends JPanel implements Runnable { int[] xx = new int[100]; int[] yy = new int[100]; BufferedImage image; int[] fonts = new int[100]; public MyStarPanel() { for(int i = 0; i < 100; ++i) { this.xx[i] = (int)(Math.random() * 800); this.yy[i] = (int)(Math.random() * 600); } try { this.image = ImageIO.read(MyStarPanel.class.getResource("shu3.jpg")); } catch (IOException var2) { var2.printStackTrace(); } } public void paint(Graphics g) { g.drawImage(this.image, 0, 0, 800, 600, (ImageObserver)null); g.setColor(Color.WHITE); for(int i = 0; i < 100; ++i) { Font ft = new Font("微软雅黑", 1, this.fonts[i]); g.setFont(ft); g.drawString("*", this.xx[i], this.yy[i]); } } public void run() { while(true) { for(int i = 0; i < 100; ++i) { this.yy[i]++; if (this.yy[i] > 600) { this.yy[i] = 0; } if (this.yy[i] > 0 && this.yy[i] < 150) { this.fonts[i] = 18; } else if (this.yy[i] > 150 && this.yy[i] < 500) { this.fonts[i] = 22; } else { this.fonts[i] = 32; } } try { Thread.sleep(100); } catch (InterruptedException var2) { var2.printStackTrace(); } this.repaint(); } } }