Java实现雪花+心型图案+炫动字体
先来动图一张看看效果。(❤ ω ❤)O(∩_∩)O怎么样。
虽然现在不是情人节(+_+)?。没关系,这又有何妨,这依然不妨碍我这颗向往爱情的心(❤ ω ❤)。
虽然还不完美,但我依旧得到您的点赞与鼓励。您的支持是我最大的动力O(∩_∩)O。
一个更好的版本
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Random;
public class Snow{
public static void main(String[] args)
{
JFrame frame=new JFrame();
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setBackground(Color.BLACK);
mypanel p=new mypanel();
frame.add(p);
frame.setVisible(true);
Thread t=new Thread(p);
t.start();
}
}
class mypanel extends JPanel implements Runnable{
private static final long serialVersionUID=1L;
Font font=new Font("font",Font.BOLD,50);
String colors[]={"red","yellow","blue"};
Random random=new Random();
int[] snowx=new int [600];
int[] snowy=new int[600];
int[] snowy1=new int[600];
double heardx[] = new double[360];
double heardy[] = new double[360];
int heardxx[] = new int[360];
int heardyy[] = new int[360];
int k = 0;
public void setvalue() {
// 360度为一个周期,设置数组中的纸
for (double t = 1; t <= 360; t++) {
// 令x=cost,则y-x^(2/3)=sint
// 所以:y=sint+(cost)^(2/3)
heardx[k] = Math.cos(Math.toRadians(t));
heardy[k] = -(Math.sin(Math.toRadians(t)) + Math.cbrt(Math.pow(Math.cos(Math.toRadians(t)), 2.0)));
// 使x、y的坐标轴与图形对齐,看起来美观一点
heardxx[k] = (int) (heardx[k] * 100 + 200);
heardyy[k] = (int) (heardy[k] * 100 + 245);
k++;
}
}
public mypanel()
{
setvalue();
for(int i=0;i<600;i++)
{
snowx[i]=this.random(800);
snowy[i]=this.random(600);
}
}
public void snowspaint(Graphics g)
{
g.setColor(Color.WHITE);
for(int i=0;i<600;i++)
{
g.drawString("*",snowx[i],snowy[i]);
for(int j=-1;j<snowy1[i];j++)
{
g.drawString("*",snowx[i],600-j*3);
}
}
}
public void heardpaint(Graphics g)
{
g.setColor(Color.red);
Polygon p=new Polygon();
p.npoints=360;
p.xpoints=heardxx;
p.ypoints=heardyy;
g.fillPolygon(p);
}
public void moonpaint(Graphics g)
{
g.setColor(Color.BLACK);
g.fillOval(-170, -170, 1100, 1100);
//g.clearRect(0, 0, 800, 600);
g.setColor(Color.YELLOW);
g.fillOval(600, 70, 130, 130);
g.setColor(Color.BLACK);
g.fillOval(550, 50, 130, 130);
}
public int[] getColor()
{
int k=random.nextInt(5);
int a[][]={{250,0,0},{0,0,250},{250,0,250},{47,56,92},{0,250,0}};
return a[k];
}
public void fontpaint(Graphics g)
{
int a[]=getColor();
g.setFont(font);
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("再",350,100);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("颖",175,220);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("大",450,100);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("的",350,250);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("雪",450,250);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("也",100,370);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("掩",250,370);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("盖",400,370);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("不",550,370);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("了",700,370);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("我",200,500);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("的",400,500);
a=getColor();
g.setColor(new Color(a[0],a[1],a[2]));
g.drawString("心",600,500);
}
public void paint(Graphics g)
{
moonpaint(g);
snowspaint(g);
heardpaint(g);
fontpaint(g);
}
public void run()
{
while(true){
for(int i=0;i<snowy.length;i++)
{
if(snowy[i]<=600)
{
snowy[i]++;
}
else {
snowy1[i]++;
snowy[i]=0;
}
}
repaint();
try{
Thread.sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public int random(int num)
{
return (int)(Math.random()*num);
}
}
您的支持是我的最大动力O(∩_∩)O。