Java实现带GUI的气泡诗词效果

 更新时间:2022年12月21日 08:58:03   作者:天人合一peng  
这篇文章主要为大家介绍了如何利用Java实现带GUI的气泡诗词效果,文中的示例代码讲解详细,对我们学习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);
 
    }
}

到此这篇关于Java实现带GUI的气泡诗词效果的文章就介绍到这了,更多相关Java气泡诗词内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • tk.mybatis扩展通用接口使用详解

    tk.mybatis扩展通用接口使用详解

    这篇文章主要介绍了tk.mybatis扩展通用接口使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • IDEA连接postgressql数据库操作

    IDEA连接postgressql数据库操作

    这篇文章主要介绍了IDEA连接postgressql数据库操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • mybatis注解与xml常用语句汇总

    mybatis注解与xml常用语句汇总

    最近一直在用mybatis,由于需要使用到了动态sql,遇到了一些问题,现在来总结一下,经验教训。下面这篇文章主要给大家总结介绍了mybatis注解与xml常用语句的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-09-09
  • 如何去掉IntelliJ IDEA中mybatis对应的xml文件警告

    如何去掉IntelliJ IDEA中mybatis对应的xml文件警告

    这篇文章主要介绍了如何去掉IntelliJ IDEA中mybatis对应的xml文件警告问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • spring-data-jpa使用自定义repository来实现原生sql

    spring-data-jpa使用自定义repository来实现原生sql

    这篇文章主要介绍了在spring-data-jpa中使用自定义repository来实现原生sql,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java多线程(单例模式,堵塞队列,定时器)详解

    Java多线程(单例模式,堵塞队列,定时器)详解

    这篇文章主要介绍了java多线程的(单例模式,堵塞队列,定时器),具有一定参考价值,加深多线程编程的理解还是很有帮助的,需要的朋友可以参考下
    2021-08-08
  • SpringBoot自定义工具类实现Excel数据存入MySQL数据库

    SpringBoot自定义工具类实现Excel数据存入MySQL数据库

    这篇文章主要为大家详细介绍了如何使用EasyExcel读取Excel内数据并转换为csv格式数据,然后实现字符串分割,分割出属性名和属性值建表插入MySQL数据库中,感兴趣的可以了解下
    2024-03-03
  • 使用maven编译Java项目实例

    使用maven编译Java项目实例

    这篇文章主要介绍了使用maven编译Java项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
    2019-06-06
  • java程序打包成exe与jar的图文教程

    java程序打包成exe与jar的图文教程

    这篇文章主要介绍了java程序打包成exe与jar的图文教程,有需要的朋友可以参考一下
    2014-01-01
  • 从零开始使用IDEA创建SpringBoot项目(图文)

    从零开始使用IDEA创建SpringBoot项目(图文)

    这篇文章主要介绍了从零开始使用IDEA创建SpringBoot项目(图文),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05

最新评论