Java GUI气泡诗词02

这篇博客介绍了一个使用JavaGUI创建的动态气泡诗词可视化应用。通过随机分布的圆圈运动,展示了苏轼的诗词,用户可以通过键盘控制气泡的运动速度,鼠标点击可以改变气泡填充状态。同时,代码还支持自定义诗词输入,增强了互动性和艺术感。
摘要由CSDN通过智能技术生成

Java GUI气泡诗词_天人合一peng的博客-CSDN博客

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);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值