使用JAVA代码实现跳动爱心(脱单节程序员必备哦)

news2024/9/21 4:25:53

520!!!表白日,你脱单了吗?你跟对象彻夜不归了吗?

如果没有说明,你的诚心不够,来给对象一个代码表白吧!

话不多说,先上效果图:

实现代码如下:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;

class Cardioid extends JFrame {

    //定义窗口大小
    private static final int WIDTH = 900;
    private static final int HEIGHT = 800;

    //获取屏幕大小
    private static final int WINDOW_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    private static final int WINDOW_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    //构造函数
    public Cardioid() {
        //设置窗口标题
        super("♥爱心");
        //设置背景色
        this.setBackground(Color.BLACK);
        //设置窗口位置
        this.setLocation((WINDOW_WIDTH - WIDTH) / 2, (WINDOW_HEIGHT - HEIGHT) / 2);
        //设置窗口大小
        this.setSize(WIDTH, HEIGHT);
        //设置窗口布局
        this.setLayout(getLayout());
        //设置窗口可见
        this.setVisible(true);
        //设置窗口的默认关闭方式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //图形函数
    public void paint(Graphics g) {
        //横纵坐标及半径
        double x, y, r;
        //绘制图形
        double z = 0.0;
        double size=10;
        int jj=0;
        while (true) {
            Image image = this.createImage(WIDTH, HEIGHT);
            Graphics pic = image.getGraphics();
            if (jj%2==0){
                size=14.5;
            }else {
                size=15;
            }
            for (int ii = 30; ii > 0; ii--) {
                Color color = new Color(255, 175, (int) (20 * Math.random()) + 220);
                for (int i = 1; i < 400; i++) {
                    int px = (int) (Math.random() * 10);
                    int py = (int) (Math.random() * 10);
                    x = 16 * (Math.sin(z) * Math.sin(z) * Math.sin(z)) * (size) + Math.pow((-1), px) * Math.random() * ii * Math.sqrt(ii) + WIDTH / 2;
                    y = -(13 * Math.cos(z) - 5 * Math.cos(2 * z) - 2 * Math.cos(3 * z) - Math.cos(4 * z)) * (size) + Math.pow((-1), py) * Math.random() * ii * Math.sqrt(ii) + HEIGHT * 1 / 3;
                    z += (Math.PI / 2.0) / 80;
                    pic.setColor(color);
                    pic.fillOval((int) x, (int) y, 2, 2);
                }
                if (ii < 3) {
                    pic.setFont(new Font("楷体", Font.BOLD, 40));//设置字体
                    pic.setColor(Color.pink);//字体颜色
                    //可自定义字体哦
                    pic.drawString("Love You", WIDTH / 2 - 100, 240);//绘制字符串
                    pic.drawString("爱你一万年!", WIDTH / 2, 280);//绘制字符串
                    g.drawImage(image, 0, 0, this);
                }
            }
            jj++;
            if (jj>100){
                break;
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new Cardioid();
    }

}




html使用红心

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    html,
    body {
      height: 100%;
      padding: 0;
      margin: 0;
      background: #000;
    }
    canvas {
      position: absolute;
      width: 100%;
      height: 100%;
    }
	/* 字体的设置 */
	#name {
	        position: absolute;
	        top: 50%;
	        left: 50%;
	        transform: translate(-50%, -50%);
	        margin-top: -20px;
	        font-size: 46px;
	        color: #ea80b0;
	      }
    .aa {
      position: fixed;
      left: 50%;
      bottom: 10px;
      color: #ccc;
    }
  </style>
  <body>
    <canvas id="pinkboard"></canvas>
 <!-- 在下面加名字 -->
     <div id="name" style="color: pink;">爱你一万年</div> 
 
    <script>
      /*
       * Settings
       */
      var settings = {
        particles: {
          length: 500, // maximum amount of particles
          duration: 2, // particle duration in sec
          velocity: 100, // particle velocity in pixels/sec
          effect: -0.75, // play with this for a nice effect
          size: 30 // particle size in pixels
        }
      };
 
      /*
       * RequestAnimationFrame polyfill by Erik M?ller
       */
      (function () {
        var b = 0;
        var c = ["ms", "moz", "webkit", "o"];
        for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) {
          window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"];
          window.cancelAnimationFrame =
            window[c[a] + "CancelAnimationFrame"] ||
            window[c[a] + "CancelRequestAnimationFrame"];
        }
        if (!window.requestAnimationFrame) {
          window.requestAnimationFrame = function (h, e) {
            var d = new Date().getTime();
            var f = Math.max(0, 16 - (d - b));
            var g = window.setTimeout(function () {
              h(d + f);
            }, f);
            b = d + f;
            return g;
          };
        }
        if (!window.cancelAnimationFrame) {
          window.cancelAnimationFrame = function (d) {
            clearTimeout(d);
          };
        }
      })();
 
      /*
       * Point class
       */
      var Point = (function () {
        function Point(x, y) {
          this.x = typeof x !== "undefined" ? x : 0;
          this.y = typeof y !== "undefined" ? y : 0;
        }
        Point.prototype.clone = function () {
          return new Point(this.x, this.y);
        };
        Point.prototype.length = function (length) {
          if (typeof length == "undefined")
            return Math.sqrt(this.x * this.x + this.y * this.y);
          this.normalize();
          this.x *= length;
          this.y *= length;
          return this;
        };
        Point.prototype.normalize = function () {
          var length = this.length();
          this.x /= length;
          this.y /= length;
          return this;
        };
        return Point;
      })();
 
      /*
       * Particle class
       */
      var Particle = (function () {
        function Particle() {
          this.position = new Point();
          this.velocity = new Point();
          this.acceleration = new Point();
          this.age = 0;
        }
        Particle.prototype.initialize = function (x, y, dx, dy) {
          this.position.x = x;
          this.position.y = y;
          this.velocity.x = dx;
          this.velocity.y = dy;
          this.acceleration.x = dx * settings.particles.effect;
          this.acceleration.y = dy * settings.particles.effect;
          this.age = 0;
        };
        Particle.prototype.update = function (deltaTime) {
          this.position.x += this.velocity.x * deltaTime;
          this.position.y += this.velocity.y * deltaTime;
          this.velocity.x += this.acceleration.x * deltaTime;
          this.velocity.y += this.acceleration.y * deltaTime;
          this.age += deltaTime;
        };
        Particle.prototype.draw = function (context, image) {
          function ease(t) {
            return --t * t * t + 1;
          }
          var size = image.width * ease(this.age / settings.particles.duration);
          context.globalAlpha = 1 - this.age / settings.particles.duration;
          context.drawImage(
            image,
            this.position.x - size / 2,
            this.position.y - size / 2,
            size,
            size
          );
        };
        return Particle;
      })();
 
      /*
       * ParticlePool class
       */
      var ParticlePool = (function () {
        var particles,
          firstActive = 0,
          firstFree = 0,
          duration = settings.particles.duration;
 
        function ParticlePool(length) {
          // create and populate particle pool
          particles = new Array(length);
          for (var i = 0; i < particles.length; i++)
            particles[i] = new Particle();
        }
        ParticlePool.prototype.add = function (x, y, dx, dy) {
          particles[firstFree].initialize(x, y, dx, dy);
 
          // handle circular queue
          firstFree++;
          if (firstFree == particles.length) firstFree = 0;
          if (firstActive == firstFree) firstActive++;
          if (firstActive == particles.length) firstActive = 0;
        };
        ParticlePool.prototype.update = function (deltaTime) {
          var i;
 
          // update active particles
          if (firstActive < firstFree) {
            for (i = firstActive; i < firstFree; i++)
              particles[i].update(deltaTime);
          }
          if (firstFree < firstActive) {
            for (i = firstActive; i < particles.length; i++)
              particles[i].update(deltaTime);
            for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
          }
 
          // remove inactive particles
          while (
            particles[firstActive].age >= duration &&
            firstActive != firstFree
          ) {
            firstActive++;
            if (firstActive == particles.length) firstActive = 0;
          }
        };
        ParticlePool.prototype.draw = function (context, image) {
          // draw active particles
          if (firstActive < firstFree) {
            for (i = firstActive; i < firstFree; i++)
              particles[i].draw(context, image);
          }
          if (firstFree < firstActive) {
            for (i = firstActive; i < particles.length; i++)
              particles[i].draw(context, image);
            for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
          }
        };
        return ParticlePool;
      })();
 
      /*
       * Putting it all together
       */
      (function (canvas) {
        var context = canvas.getContext("2d"),
          particles = new ParticlePool(settings.particles.length),
          particleRate =
            settings.particles.length / settings.particles.duration, // particles/sec
          time;
 
        // get point on heart with -PI <= t <= PI
        function pointOnHeart(t) {
          return new Point(
            160 * Math.pow(Math.sin(t), 3),
            130 * Math.cos(t) -
              50 * Math.cos(2 * t) -
              20 * Math.cos(3 * t) -
              10 * Math.cos(4 * t) +
              25
          );
        }
 
        // creating the particle image using a dummy canvas
        var image = (function () {
          var canvas = document.createElement("canvas"),
            context = canvas.getContext("2d");
          canvas.width = settings.particles.size;
          canvas.height = settings.particles.size;
          // helper function to create the path
          function to(t) {
            var point = pointOnHeart(t);
            point.x =
              settings.particles.size / 2 +
              (point.x * settings.particles.size) / 350;
            point.y =
              settings.particles.size / 2 -
              (point.y * settings.particles.size) / 350;
            return point;
          }
          // create the path
          context.beginPath();
          var t = -Math.PI;
          var point = to(t);
          context.moveTo(point.x, point.y);
          while (t < Math.PI) {
            t += 0.01; // baby steps!
            point = to(t);
            context.lineTo(point.x, point.y);
          }
          context.closePath();
          // create the fill
          context.fillStyle = "#ea80b0";
          context.fill();
          // create the image
          var image = new Image();
          image.src = canvas.toDataURL();
          return image;
        })();
 
        // render that thing!
        function render() {
          // next animation frame
          requestAnimationFrame(render);
 
          // update time
          var newTime = new Date().getTime() / 1000,
            deltaTime = newTime - (time || newTime);
          time = newTime;
 
          // clear canvas
          context.clearRect(0, 0, canvas.width, canvas.height);
 
          // create new particles
          var amount = particleRate * deltaTime;
          for (var i = 0; i < amount; i++) {
            var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
            var dir = pos.clone().length(settings.particles.velocity);
            particles.add(
              canvas.width / 2 + pos.x,
              canvas.height / 2 - pos.y,
              dir.x,
              -dir.y
            );
          }
 
          // update and draw particles
          particles.update(deltaTime);
          particles.draw(context, image);
        }
 
        // handle (re-)sizing of the canvas
        function onResize() {
          canvas.width = canvas.clientWidth;
          canvas.height = canvas.clientHeight;
        }
        window.onresize = onResize;
 
        // delay rendering bootstrap
        setTimeout(function () {
          onResize();
          render();
        }, 10);
      })(document.getElementById("pinkboard"));
    </script>
  </body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/547850.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

kicad学习笔记6:kicad启动及其grid参数设置和修改

1。kicad启动&#xff1a; single_top.cpp 启动函数&#xff1a; 1。 IMPLEMENT_APP( APP_SINGLE_TOP )2。 PGM_SINGLE_TOP::OnPgmInit()3。 PGM_BASE::InitPgm2。kicad参数 grid参数定义&#xff1a; struct GRID_SETTINGS {bool axes_enabled;std::vector<wxString&…

华为OD机试真题 Java 实现【天然蓄水池】【2023Q1 200分】

一、题目描述 公元2919年&#xff0c;人类终于发现了一颗宜居星球——X星。现想在X星一片连绵起伏的山脉间建一个天然蓄水库&#xff0c;如何选取水库边界&#xff0c;使蓄水量最大&#xff1f; 要求&#xff1a; 山脉用正整数数组s表示&#xff0c;每个元素代表山脉的高度。…

基于springboot人事管理系统

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SpringBoot 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 前言 基于springboot人事管…

vscode录音及语音实时转写插件开发并在工作区生成本地mp3文件 踩坑日记!

前言 最近接到一个需求&#xff0c;实现录音功能并生成mp3文件到本地工作区&#xff0c;一开始考虑到的是在vscode主体代码里面开发&#xff0c;但这可不是一个小的工作量。时间紧&#xff0c;任务重&#xff01;市面上实现录音功能的案例其实很多&#xff0c;一些功能代码是可…

【敬伟ps教程】修复工具

文章目录 模糊工具锐化工具涂抹工具减淡、加深工具海绵工具仿制图章工具图案图章工具修复画笔工具污点修复画笔工具修补工具内容感知移动工具红眼工具 模糊工具 模糊工具的主要功能就是把图像变的模糊。虽然是把图像变模糊&#xff0c;但是模糊工具应用也是很广泛的。使用模糊工…

小黑子—Java从入门到入土过程:第十一章 - 网络编程、反射及动态代理

Java零基础入门11.0 网络编程1. 初识网络编程2. 网络编程三要素3.IP三要素3.1 IPV4的细节3.1.1特殊的IP地址3.1.2 常用的CMD命令 3.2 InetAddress 的使用3.3 端口号3.4 协议3.4.1 UDP协议3.4.1 - I UDP 发送数据3.4.1 - II UDP 接收数据3.4.1 - III UDP 练习&#xff08;聊天室…

C++实现日期类(超详细)

个人主页&#xff1a;平行线也会相交&#x1f4aa; 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 平行线也会相交 原创 收录于专栏【C之路】&#x1f48c; 本专栏旨在记录C的学习路线&#xff0c;望对大家有所帮助&#x1f647;‍ 希望我们一起努力、成长&…

六级备考28天|CET-6|听力第二讲|长对话满分技巧|听写技巧|2022年6月考题|14:30~16:00

目录 1. 听力策略 2. 第一二讲笔记 3. 听力原文复现 (5)第五小题 (6)第六小题 (7)第七小题 (8)第八小题 扩展业务 expand business 4. 重点词汇 1. 听力策略 2. 第一二讲笔记 3. 听力原文复现 (5)第五小题 our guest is Molly Sundas, a university stud…

【ZYNQ】ZYNQ7000 定时器及私有定时器驱动示例

简介 每个 Cortex-A9 处理器都有自己独立的 32 位私有定时器和 32 位看门狗定时器。这两个处理器共享一个 64 位的全局定时器。这些计时器的频率为 CPU 频率&#xff08;CPU_3x2x&#xff09;的 1/2。 在系统级别上&#xff0c;有一个 24 位看门狗定时器和两个 16 位三重定时…

据不可靠消息,ST的新一代机皇正式命名为STM32V8系列,搭载Cortex-M85内核

根据以往的传统单片机命名方式&#xff1a; C0, L0, G0, F0 > Cortex-M0内核 F1, L1 > Corterx-M3内核 F2, F3 > Corterx-M3/M4 F4&#xff0c;G4&#xff0c;L4, L4 > Cortex-M4内核 L5&#xff0c;U5, H5 > Cor…

数据结构—排序算法(插入排序及选择排序)

目录 1、排序的概念 2、常见的排序算法 3、直接插入排序&#xff08;插入排序&#xff09; 3.1 直接插入排序基本思想 3.2 直接插入排序实现 4、 希尔排序( 缩小增量排序 )&#xff08;插入排序&#xff09; 4.1 基本思想 4.2 希尔排序实现 4.3 希尔排序的特性总结 5、…

【Draw.io】让Draw.io导出的SVG格式图片包含自定义属性信息

前景提要 Draw.io重度用户一枚 这个Draw.io是一个极其好用的跨平台流程图绘制软件。 它保存的文件格式可以输出成SVG格式. 这个是基本功能了&#xff0c;没啥好说的 导出之后得到画的图片的SVG代码 SVG代码&#xff0c;也没啥好说的&#xff0c;一种矢量图片格式。 但是&…

改造Springboot+vue项目

准备 搜索一个项目&#xff0c;并成功运行 毕设时&#xff0c;我们可以从网上搜索一个项目&#xff08;包括前端的页面、后台的处理、数据库等有一下简单的模板样式&#xff09;并能成功的将项目在自己的电脑上跑起来。毕设项目可以用搜索的模板&#xff08;不用从头开始写&a…

【数据结构】_2.包装类与泛型

目录 1. 包装类 1.1 基本数据类型和对应的包装类 1.2 &#xff08;自动&#xff09;装箱和&#xff08;自动&#xff09;拆箱 1.2.1 装箱与拆箱 1.2.2 自动装箱与自动拆箱 1.3 valueOf()方法 2. 泛型类 2.1 泛型类与Object类 2.2 泛型类语法 2.3 泛型类示例 2.4 裸类…

怎么把照片缩小到200k?图片指定大小压缩怎么弄?

平时在给账号设置头像时&#xff0c;都会遇到图片过大无法上传的情况&#xff0c;这时候我们可以通过图片压缩指定大小工具来将图片压缩到200kb以下&#xff0c;这样就可以顺利设置头像了&#xff0c;下面一起来看一下图片指定大小压缩&#xff08;https://www.yasuotu.com/ima…

机器学习笔记 - windows基于TensorRT的UNet推理部署

一、TensorRT简介 NVIDIA TensorRT是一个用于高性能深度学习推理的平台。TensorRT适用于使用CUDA平台的所有NVIDIA GPU。所以如果需要基于TensorRT部署,至少需要一个NVIDIA显卡,算力5.0以上,比Maxwell更新的架构,可以参考下表。 CUDA GPUs - Compute Capability | NVIDIA …

电动车头盔检测数据集

现在正慢慢整理自己有关电动车头盔检测的项目内容&#xff0c;会逐渐将这些资源进行发布&#xff0c;供大家参考和使用【部分资源有偿提供&#xff0c;毕竟花费了很多心血】。 这篇文章主要是发布相关数据集的。网上关于工厂环境下的头盔数据集有很多&#xff0c;而且多种多样…

PFCdocumentation_Coupling PFC and FLAC3D

目录 Coupling PFC and FLAC3D 1D Structural Element Coupling Scheme Wall-Zone Coupling Scheme 2D 3D Ball-Zone Coupling Scheme Commands FISH Functions Coupling PFC and FLAC3D 在 FLAC3D 图形用户界面中&#xff0c;可以通过“工具 ‣ 加载 PFC”菜单项加载 …

计算机组成与结构易错题

计算机组成与结构 海明校验码是在n个数据位之外增设k个校验位&#xff0c;从而形成一个kn位的新的码字&#xff0c;使新的码字的码距比较均匀地拉大。n与k的关系是&#xff08;A&#xff09;。 A、2k-1≥nk B、2n-1≤nk C、nk D、n-1≤k 知识&#xff1a; 确定要传输的信息&…

普通专科生,拿什么拯救自己的未来我想成为一名网络安全专业人员,需要做什么?

前 言 写这篇文章的初衷是很多朋友都想了解如何入门/转行网络安全&#xff0c;实现自己的“黑客梦”。文章的宗旨是&#xff1a; 1.指出一些自学的误区 2.提供客观可行的学习表 3.推荐我认为适合小白学习的资源.大佬绕道哈&#xff01; 我的经历&#xff1a; 我19年毕业&…