编程语言
首页 > 编程语言> > Java实现动态雪花、心形图案、炫动字体

Java实现动态雪花、心形图案、炫动字体

作者:互联网

Java实现桃花心形、动态雪花特效、炫动字体
在这里插入图片描述

import java.awt.Color;
import java.awt.Graphics; 
import java.awt.Font;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Random;

public class u{
    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)
    {
        double x, y, r;
		Image OffScreen = createImage(500, 500);
		Graphics drawOffScreen = OffScreen.getGraphics();
 
		for (int i = 0; i < 90; i++) {
			for (int j = 0; j < 90; j++) {
				r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * 18;
				x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i)+ 500 / 2-100;
				y = -r * Math.sin(Math.PI / 45 * j) + 500 / 4-50;

				drawOffScreen.setColor(Color.PINK);
				drawOffScreen.fillOval((int) x, (int) y, 2, 2);
			}
			// 生成图片
			g.drawImage(OffScreen, 0, 0, this);
		}
    }
    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("颖",125,185);
        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);
        heardpaint(g);
        snowspaint(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);
    }
}

标签:Java,setColor,int,心形,炫动,Color,drawString,new,Math
来源: https://blog.csdn.net/qq_45633812/article/details/115272646