欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)帶GUI的氣泡詩詞效果

 更新時間:2022年12月21日 08:58:03   作者:天人合一peng  
這篇文章主要為大家介紹了如何利用Java實現(xiàn)帶GUI的氣泡詩詞效果,文中的示例代碼講解詳細,對我們學(xué)習(xí)Java有一定幫助,感興趣的可以了解一下

之前已經(jīng)為大家介紹過利用Java實現(xiàn)帶GUI的氣泡詩詞特效,本文將為大家介紹另一種方法同樣也可以實現(xiàn)氣泡詩詞的效果。下面是示例代碼

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 = "夜飲東坡醒復(fù)醉,歸來仿佛三更。" +
            "家童鼻息已雷鳴。敲門都不應(yīng),倚杖聽江聲。\n" +
            "\n" +
            "長恨此身非我有,何時忘卻營營。" +
            "夜闌風(fēng)靜縠紋平。小舟從此逝,江海寄余生。";
 
    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)
        {
            //繪制當(dāng)前數(shù)據(jù)
            frame.render(circles);
            AlgoVisHelper.pause(20);
            //更新數(shù)據(jù)
            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" +
                "料峭春風(fēng)吹酒醒,微冷,山頭斜照卻相迎?;厥紫騺硎捝帲瑲w去,也無風(fēng)雨也無晴。";
 
        int sceneWidth = 800;
        int sceneHeight = 800;
        int N = 15;
 
//        AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N);
        AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N, poemData);
 
    }
}

到此這篇關(guān)于Java實現(xiàn)帶GUI的氣泡詩詞效果的文章就介紹到這了,更多相關(guān)Java氣泡詩詞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java文件流關(guān)閉和垃圾回收機制

    Java文件流關(guān)閉和垃圾回收機制

    本文是關(guān)于Java IO文件流和垃圾回收問題,一個小的測試程序搞清楚Java IO的問題,希望能幫助有需要的小伙伴
    2016-07-07
  • 一文教你搞定Java Optional類判空操作

    一文教你搞定Java Optional類判空操作

    有時項目組內(nèi)做code review,會充斥著大量的、原始的、丑陋的判空語句。讓整體的代碼顯得十分的臃腫龐大丑陋,那么怎么辦呢?利用Optional這個jdk8中引入的類就可以優(yōu)雅的處理,現(xiàn)在我們來詳細講解下這個類的使用和源碼
    2022-10-10
  • 基于@RequestParam與@RequestBody使用對比

    基于@RequestParam與@RequestBody使用對比

    這篇文章主要介紹了@RequestParam與@RequestBody的使用對比,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring源碼之循環(huán)依賴之三級緩存詳解

    Spring源碼之循環(huán)依賴之三級緩存詳解

    這篇文章主要為大家詳細介紹了Spring源碼之循環(huán)依賴之三級緩存,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Nacos集群搭建過程詳解

    Nacos集群搭建過程詳解

    這篇文章主要為大家介紹了Nacos集群搭建過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • java中計算字符串長度的方法及u4E00與u9FBB的認識

    java中計算字符串長度的方法及u4E00與u9FBB的認識

    字符串采用unicode編碼的方式時,計算字符串長度的方法找出UNICODE編碼中的漢字的代表的范圍“\u4E00” 到“\u9FBB”之間感興趣的朋友可以參考本文,或許對你有所幫助
    2013-01-01
  • Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解

    Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解

    這篇文章主要介紹了Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解,有些朋友可能不了解過濾器、監(jiān)聽器和攔截器的區(qū)別,本文就來詳細講一下,相信看完你會有所收獲,需要的朋友可以參考下
    2024-01-01
  • Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決

    Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決

    這篇文章主要介紹了Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決方案,具有很好的參考價值,希望對大家有所幫助。
    2023-07-07
  • Springmvc國際化自動配置代碼實現(xiàn)

    Springmvc國際化自動配置代碼實現(xiàn)

    這篇文章主要介紹了Springmvc國際化自動配置代碼實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例

    springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例

    本篇文章主要介紹了springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01

最新評論