当前位置:   article > 正文

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

java爱心代码跳动

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

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

话不多说,先上效果图:

实现代码如下:

  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import javax.swing.JFrame;
  7. class Cardioid extends JFrame {
  8. //定义窗口大小
  9. private static final int WIDTH = 900;
  10. private static final int HEIGHT = 800;
  11. //获取屏幕大小
  12. private static final int WINDOW_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
  13. private static final int WINDOW_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
  14. //构造函数
  15. public Cardioid() {
  16. //设置窗口标题
  17. super("♥爱心");
  18. //设置背景色
  19. this.setBackground(Color.BLACK);
  20. //设置窗口位置
  21. this.setLocation((WINDOW_WIDTH - WIDTH) / 2, (WINDOW_HEIGHT - HEIGHT) / 2);
  22. //设置窗口大小
  23. this.setSize(WIDTH, HEIGHT);
  24. //设置窗口布局
  25. this.setLayout(getLayout());
  26. //设置窗口可见
  27. this.setVisible(true);
  28. //设置窗口的默认关闭方式
  29. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. }
  31. //图形函数
  32. public void paint(Graphics g) {
  33. //横纵坐标及半径
  34. double x, y, r;
  35. //绘制图形
  36. double z = 0.0;
  37. double size=10;
  38. int jj=0;
  39. while (true) {
  40. Image image = this.createImage(WIDTH, HEIGHT);
  41. Graphics pic = image.getGraphics();
  42. if (jj%2==0){
  43. size=14.5;
  44. }else {
  45. size=15;
  46. }
  47. for (int ii = 30; ii > 0; ii--) {
  48. Color color = new Color(255, 175, (int) (20 * Math.random()) + 220);
  49. for (int i = 1; i < 400; i++) {
  50. int px = (int) (Math.random() * 10);
  51. int py = (int) (Math.random() * 10);
  52. x = 16 * (Math.sin(z) * Math.sin(z) * Math.sin(z)) * (size) + Math.pow((-1), px) * Math.random() * ii * Math.sqrt(ii) + WIDTH / 2;
  53. 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;
  54. z += (Math.PI / 2.0) / 80;
  55. pic.setColor(color);
  56. pic.fillOval((int) x, (int) y, 2, 2);
  57. }
  58. if (ii < 3) {
  59. pic.setFont(new Font("楷体", Font.BOLD, 40));//设置字体
  60. pic.setColor(Color.pink);//字体颜色
  61. //可自定义字体哦
  62. pic.drawString("Love You", WIDTH / 2 - 100, 240);//绘制字符串
  63. pic.drawString("爱你一万年!", WIDTH / 2, 280);//绘制字符串
  64. g.drawImage(image, 0, 0, this);
  65. }
  66. }
  67. jj++;
  68. if (jj>100){
  69. break;
  70. }
  71. try {
  72. Thread.sleep(500);
  73. } catch (InterruptedException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. public static void main(String[] args) {
  79. new Cardioid();
  80. }
  81. }

html使用红心

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. </head>
  6. <style>
  7. * {
  8. padding: 0;
  9. margin: 0;
  10. }
  11. html,
  12. body {
  13. height: 100%;
  14. padding: 0;
  15. margin: 0;
  16. background: #000;
  17. }
  18. canvas {
  19. position: absolute;
  20. width: 100%;
  21. height: 100%;
  22. }
  23. /* 字体的设置 */
  24. #name {
  25. position: absolute;
  26. top: 50%;
  27. left: 50%;
  28. transform: translate(-50%, -50%);
  29. margin-top: -20px;
  30. font-size: 46px;
  31. color: #ea80b0;
  32. }
  33. .aa {
  34. position: fixed;
  35. left: 50%;
  36. bottom: 10px;
  37. color: #ccc;
  38. }
  39. </style>
  40. <body>
  41. <canvas id="pinkboard"></canvas>
  42. <!-- 在下面加名字 -->
  43. <div id="name" style="color: pink;">爱你一万年</div>
  44. <script>
  45. /*
  46. * Settings
  47. */
  48. var settings = {
  49. particles: {
  50. length: 500, // maximum amount of particles
  51. duration: 2, // particle duration in sec
  52. velocity: 100, // particle velocity in pixels/sec
  53. effect: -0.75, // play with this for a nice effect
  54. size: 30 // particle size in pixels
  55. }
  56. };
  57. /*
  58. * RequestAnimationFrame polyfill by Erik M?ller
  59. */
  60. (function () {
  61. var b = 0;
  62. var c = ["ms", "moz", "webkit", "o"];
  63. for (var a = 0; a < c.length && !window.requestAnimationFrame; ++a) {
  64. window.requestAnimationFrame = window[c[a] + "RequestAnimationFrame"];
  65. window.cancelAnimationFrame =
  66. window[c[a] + "CancelAnimationFrame"] ||
  67. window[c[a] + "CancelRequestAnimationFrame"];
  68. }
  69. if (!window.requestAnimationFrame) {
  70. window.requestAnimationFrame = function (h, e) {
  71. var d = new Date().getTime();
  72. var f = Math.max(0, 16 - (d - b));
  73. var g = window.setTimeout(function () {
  74. h(d + f);
  75. }, f);
  76. b = d + f;
  77. return g;
  78. };
  79. }
  80. if (!window.cancelAnimationFrame) {
  81. window.cancelAnimationFrame = function (d) {
  82. clearTimeout(d);
  83. };
  84. }
  85. })();
  86. /*
  87. * Point class
  88. */
  89. var Point = (function () {
  90. function Point(x, y) {
  91. this.x = typeof x !== "undefined" ? x : 0;
  92. this.y = typeof y !== "undefined" ? y : 0;
  93. }
  94. Point.prototype.clone = function () {
  95. return new Point(this.x, this.y);
  96. };
  97. Point.prototype.length = function (length) {
  98. if (typeof length == "undefined")
  99. return Math.sqrt(this.x * this.x + this.y * this.y);
  100. this.normalize();
  101. this.x *= length;
  102. this.y *= length;
  103. return this;
  104. };
  105. Point.prototype.normalize = function () {
  106. var length = this.length();
  107. this.x /= length;
  108. this.y /= length;
  109. return this;
  110. };
  111. return Point;
  112. })();
  113. /*
  114. * Particle class
  115. */
  116. var Particle = (function () {
  117. function Particle() {
  118. this.position = new Point();
  119. this.velocity = new Point();
  120. this.acceleration = new Point();
  121. this.age = 0;
  122. }
  123. Particle.prototype.initialize = function (x, y, dx, dy) {
  124. this.position.x = x;
  125. this.position.y = y;
  126. this.velocity.x = dx;
  127. this.velocity.y = dy;
  128. this.acceleration.x = dx * settings.particles.effect;
  129. this.acceleration.y = dy * settings.particles.effect;
  130. this.age = 0;
  131. };
  132. Particle.prototype.update = function (deltaTime) {
  133. this.position.x += this.velocity.x * deltaTime;
  134. this.position.y += this.velocity.y * deltaTime;
  135. this.velocity.x += this.acceleration.x * deltaTime;
  136. this.velocity.y += this.acceleration.y * deltaTime;
  137. this.age += deltaTime;
  138. };
  139. Particle.prototype.draw = function (context, image) {
  140. function ease(t) {
  141. return --t * t * t + 1;
  142. }
  143. var size = image.width * ease(this.age / settings.particles.duration);
  144. context.globalAlpha = 1 - this.age / settings.particles.duration;
  145. context.drawImage(
  146. image,
  147. this.position.x - size / 2,
  148. this.position.y - size / 2,
  149. size,
  150. size
  151. );
  152. };
  153. return Particle;
  154. })();
  155. /*
  156. * ParticlePool class
  157. */
  158. var ParticlePool = (function () {
  159. var particles,
  160. firstActive = 0,
  161. firstFree = 0,
  162. duration = settings.particles.duration;
  163. function ParticlePool(length) {
  164. // create and populate particle pool
  165. particles = new Array(length);
  166. for (var i = 0; i < particles.length; i++)
  167. particles[i] = new Particle();
  168. }
  169. ParticlePool.prototype.add = function (x, y, dx, dy) {
  170. particles[firstFree].initialize(x, y, dx, dy);
  171. // handle circular queue
  172. firstFree++;
  173. if (firstFree == particles.length) firstFree = 0;
  174. if (firstActive == firstFree) firstActive++;
  175. if (firstActive == particles.length) firstActive = 0;
  176. };
  177. ParticlePool.prototype.update = function (deltaTime) {
  178. var i;
  179. // update active particles
  180. if (firstActive < firstFree) {
  181. for (i = firstActive; i < firstFree; i++)
  182. particles[i].update(deltaTime);
  183. }
  184. if (firstFree < firstActive) {
  185. for (i = firstActive; i < particles.length; i++)
  186. particles[i].update(deltaTime);
  187. for (i = 0; i < firstFree; i++) particles[i].update(deltaTime);
  188. }
  189. // remove inactive particles
  190. while (
  191. particles[firstActive].age >= duration &&
  192. firstActive != firstFree
  193. ) {
  194. firstActive++;
  195. if (firstActive == particles.length) firstActive = 0;
  196. }
  197. };
  198. ParticlePool.prototype.draw = function (context, image) {
  199. // draw active particles
  200. if (firstActive < firstFree) {
  201. for (i = firstActive; i < firstFree; i++)
  202. particles[i].draw(context, image);
  203. }
  204. if (firstFree < firstActive) {
  205. for (i = firstActive; i < particles.length; i++)
  206. particles[i].draw(context, image);
  207. for (i = 0; i < firstFree; i++) particles[i].draw(context, image);
  208. }
  209. };
  210. return ParticlePool;
  211. })();
  212. /*
  213. * Putting it all together
  214. */
  215. (function (canvas) {
  216. var context = canvas.getContext("2d"),
  217. particles = new ParticlePool(settings.particles.length),
  218. particleRate =
  219. settings.particles.length / settings.particles.duration, // particles/sec
  220. time;
  221. // get point on heart with -PI <= t <= PI
  222. function pointOnHeart(t) {
  223. return new Point(
  224. 160 * Math.pow(Math.sin(t), 3),
  225. 130 * Math.cos(t) -
  226. 50 * Math.cos(2 * t) -
  227. 20 * Math.cos(3 * t) -
  228. 10 * Math.cos(4 * t) +
  229. 25
  230. );
  231. }
  232. // creating the particle image using a dummy canvas
  233. var image = (function () {
  234. var canvas = document.createElement("canvas"),
  235. context = canvas.getContext("2d");
  236. canvas.width = settings.particles.size;
  237. canvas.height = settings.particles.size;
  238. // helper function to create the path
  239. function to(t) {
  240. var point = pointOnHeart(t);
  241. point.x =
  242. settings.particles.size / 2 +
  243. (point.x * settings.particles.size) / 350;
  244. point.y =
  245. settings.particles.size / 2 -
  246. (point.y * settings.particles.size) / 350;
  247. return point;
  248. }
  249. // create the path
  250. context.beginPath();
  251. var t = -Math.PI;
  252. var point = to(t);
  253. context.moveTo(point.x, point.y);
  254. while (t < Math.PI) {
  255. t += 0.01; // baby steps!
  256. point = to(t);
  257. context.lineTo(point.x, point.y);
  258. }
  259. context.closePath();
  260. // create the fill
  261. context.fillStyle = "#ea80b0";
  262. context.fill();
  263. // create the image
  264. var image = new Image();
  265. image.src = canvas.toDataURL();
  266. return image;
  267. })();
  268. // render that thing!
  269. function render() {
  270. // next animation frame
  271. requestAnimationFrame(render);
  272. // update time
  273. var newTime = new Date().getTime() / 1000,
  274. deltaTime = newTime - (time || newTime);
  275. time = newTime;
  276. // clear canvas
  277. context.clearRect(0, 0, canvas.width, canvas.height);
  278. // create new particles
  279. var amount = particleRate * deltaTime;
  280. for (var i = 0; i < amount; i++) {
  281. var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
  282. var dir = pos.clone().length(settings.particles.velocity);
  283. particles.add(
  284. canvas.width / 2 + pos.x,
  285. canvas.height / 2 - pos.y,
  286. dir.x,
  287. -dir.y
  288. );
  289. }
  290. // update and draw particles
  291. particles.update(deltaTime);
  292. particles.draw(context, image);
  293. }
  294. // handle (re-)sizing of the canvas
  295. function onResize() {
  296. canvas.width = canvas.clientWidth;
  297. canvas.height = canvas.clientHeight;
  298. }
  299. window.onresize = onResize;
  300. // delay rendering bootstrap
  301. setTimeout(function () {
  302. onResize();
  303. render();
  304. }, 10);
  305. })(document.getElementById("pinkboard"));
  306. </script>
  307. </body>
  308. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/959812
推荐阅读
相关标签
  

闽ICP备14008679号