Java实现带GUI的气泡诗词效果
作者:天人合一peng 发布时间:2022-09-12 18:04:48
标签:Java,气泡,诗词
之前已经为大家介绍过利用Java实现带GUI的气泡诗词特效,本文将为大家介绍另一种方法同样也可以实现气泡诗词的效果。下面是示例代码
import java.awt.*;
import java.awt.event.*;
public class AlgoVisualizer {
private Object data;
private Circle[] circles;
private AlgoFrame frame;
private boolean isAnmiated = true;
String SuShi_Poem = "夜饮东坡醒复醉,归来仿佛三更。" +
"家童鼻息已雷鸣。敲门都不应,倚杖听江声。\n" +
"\n" +
"长恨此身非我有,何时忘却营营。" +
"夜阑风静縠纹平。小舟从此逝,江海寄余生。";
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
circles = new Circle[N];
int R = 50;
for(int i = 0; i < N; i++)
{
int x = (int)(Math.random()*(sceneWidth-2*R)) + R;
int y = (int)(Math.random()*(sceneHeight-2*R)) + R;
int vx = (int)(Math.random()*11) - 5;
int vy = (int)(Math.random()*11) - 5;
circles[i] = new Circle(x, y, R, vx, vy);
}
EventQueue.invokeLater(()->{
frame = new AlgoFrame("Welcome-Java", sceneWidth, sceneHeight);
frame.addKeyListener(new AlgoKeyListener());
frame.addMouseListener(new AlgoMouseListener());
new Thread(()->{run();}).start();
});
}
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, String centerLael){
Circle.showLabel = true;
circles = new Circle[N];
int R = 50;
for(int i = 0; i < N; i++)
{
int x = (int)(Math.random()*(sceneWidth-2*R)) + R;
int y = (int)(Math.random()*(sceneHeight-2*R)) + R;
int vx = (int)(Math.random()*11) - 5;
int vy = (int)(Math.random()*11) - 5;
// circles[i] = new Circle(x, y, R, vx, vy);
circles[i] = new Circle(x, y, R, vx, vy, centerLael.charAt(i) + "");
}
EventQueue.invokeLater(()->{
frame = new AlgoFrame("Welcome-Java", sceneWidth, sceneHeight);
frame.addKeyListener(new AlgoKeyListener());
frame.addMouseListener(new AlgoMouseListener());
new Thread(()->{
run();
}).start();
});
}
private void run(){
while(true)
{
//绘制当前数据
frame.render(circles);
AlgoVisHelper.pause(20);
//更新数据
if(isAnmiated)
{
for(Circle circle:circles)
circle.move(0, 0, frame.getCanvasWidth(), frame.getCanvasHeight());
}
}
}
private class AlgoKeyListener extends KeyAdapter {
@Override
public void keyReleased(KeyEvent event)
{
// 空格 动画
if(event.getKeyChar() == ' ')
{
isAnmiated = !isAnmiated;
}
// +事件加速,跑的更快
if(event.getKeyChar() == '+')
{
// System.out.println("加速++++++");
for(Circle circle:circles)
{
circle.vx *= 2;
circle.vy *= 2;
}
}
// —减速,慢一点
if(event.getKeyChar() == '-')
{
// System.out.println("加速++++++");
for(Circle circle:circles)
{
circle.vx /= 2;
circle.vy /= 2;
if(circle.vx == 0 && circle.vy == 0)
{
System.out.println("practice makes perfect!");
System.out.println(SuShi_Poem);
circle.vx = (int)(Math.random()*11) - 5;
circle.vy = (int)(Math.random()*11) - 5;
}
}
}
}
}
private class AlgoMouseListener extends MouseAdapter{
@Override
public void mousePressed (MouseEvent event)
{
event.translatePoint(0,
// (frame.getBounds().height -frame.getCanvasHeight()));
-(frame.getBounds().height -frame.getCanvasHeight()));
// System.out.println(event.getPoint());
for(Circle circle:circles)
{
if(circle.contain(event.getPoint())){
circle.isFilled = !circle.isFilled;
}
}
}
}
public static void main(String[] args) {
String poemData = "三月七日沙湖道中遇雨。雨具先去,同行皆狼狈,余独不觉。已而遂晴,故作此词 \n" +
"莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕? 一蓑烟雨任平生。\n" +
"料峭春风吹酒醒,微冷,山头斜照却相迎。回首向来萧瑟处,归去,也无风雨也无晴。";
int sceneWidth = 800;
int sceneHeight = 800;
int N = 15;
// AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N);
AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N, poemData);
}
}
来源:https://blog.csdn.net/moonlightpeng/article/details/128385910


猜你喜欢
- 前面讲述了使用POI导出Word文件和读取Excel文件,这两个例子都相对简单,接下来要讲述的使用POI导出Excel文件要复杂得多,内容也
- 这篇文章主要介绍了简单了解Java中的可重入锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 对于导航组件的使用方式不是本文的重点,具体使用可以参考官方文档,导航组件框架是通过f
- package cn.liangjintang.httpproxy;import java.io.BufferedReader;import
- Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0、DBCP、PROXOOL等DB池的优点,同时加入了日志监控,可以很好的
- 1. 概述在 Spring Security 5.2 中增强了 DSL 的功能:允许使用 Lambda 表达式来配置 HTTP securi
- Java面向对象之猜拳游戏,供大家参考,具体内容如下1 要求与电脑进行猜拳并记录分数。2 Computer.java 源代码(电脑自动随机出
- 这篇文章主要介绍了java文件下载代码实例(单文件下载和多文件打包下载),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 前言首先,事务这个概念是数据库层面的,Spring只是基于数据库中的事务进行扩展,以及提供了一些能让程序员更新方便操作事务的方式Spring
- 在android 6.0中google终于给android系统加上了指纹识别的支持,这个功能在iPhone上早就已经实现了,并且在很多厂商的
- 参考链接:狂神的Swagger笔记号称世界上最流行的API框架Restful Api 文档在线自动生成器 => API 文档 与API
- 生产者消费者模式的几种实现方式拿我们生活中的例子来说,工厂生产出来的产品总是要输出到外面使用的,这就是生产与消费的概念。在我们实际的软件开发
- Unity 跑马灯抽奖效果实现代码,供大家参考,具体内容如下这边用到插件是NGUI+Dotween,思路简单说下:先排版,通过移动图片蒙版来
- turbine是聚合服务器发送事件流数据的一个工具,hystrix的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过turbine
- JAVA源码编译由三个过程组成:1、源码编译机制。2、类加载机制3、类执行机制我们这里主要介绍编译和类加载这两种机制。一、源码编译代码编译由
- @RequestBody配合@Valid校验入参参数自定义一个Controllerimport com.example.demo.pojo.
- 前面文章讲解了Android的蓝牙基本用法,本文讲得深入些,探讨下蓝牙方面的隐藏API。用过Android系统设置(Setting)的人都知
- TV 3D卡片无限循环效果,供大家参考,具体内容如下##前言1、需求:实现3个卡片实现无限循环效果:1-2-3-1-2-3-1…,而且要实现
- java list,set,map,数组间的相互转换详解1.list转setSet set = new HashSet( new Array
- 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何自定义一个类似热门标签那样