Java實現(xiàn)帶GUI的氣泡詩詞效果
之前已經(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)文章
基于@RequestParam與@RequestBody使用對比
這篇文章主要介紹了@RequestParam與@RequestBody的使用對比,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10java中計算字符串長度的方法及u4E00與u9FBB的認識
字符串采用unicode編碼的方式時,計算字符串長度的方法找出UNICODE編碼中的漢字的代表的范圍“\u4E00” 到“\u9FBB”之間感興趣的朋友可以參考本文,或許對你有所幫助2013-01-01Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解
這篇文章主要介紹了Java中過濾器、監(jiān)聽器和攔截器的區(qū)別詳解,有些朋友可能不了解過濾器、監(jiān)聽器和攔截器的區(qū)別,本文就來詳細講一下,相信看完你會有所收獲,需要的朋友可以參考下2024-01-01Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決
這篇文章主要介紹了Java遠程執(zhí)行shell命令出現(xiàn)java: command not found問題及解決方案,具有很好的參考價值,希望對大家有所幫助。2023-07-07springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例
本篇文章主要介紹了springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01