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)吹酒醒,微冷,山頭斜照卻相迎。回首向來蕭瑟處,歸去,也無風(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)文章
IDEA連接postgressql數(shù)據(jù)庫操作
這篇文章主要介紹了IDEA連接postgressql數(shù)據(jù)庫操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告
這篇文章主要介紹了如何去掉IntelliJ IDEA中mybatis對應(yīng)的xml文件警告問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
spring-data-jpa使用自定義repository來實現(xiàn)原生sql
這篇文章主要介紹了在spring-data-jpa中使用自定義repository來實現(xiàn)原生sql,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot自定義工具類實現(xiàn)Excel數(shù)據(jù)存入MySQL數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了如何使用EasyExcel讀取Excel內(nèi)數(shù)據(jù)并轉(zhuǎn)換為csv格式數(shù)據(jù),然后實現(xiàn)字符串分割,分割出屬性名和屬性值建表插入MySQL數(shù)據(jù)庫中,感興趣的可以了解下2024-03-03
從零開始使用IDEA創(chuàng)建SpringBoot項目(圖文)
這篇文章主要介紹了從零開始使用IDEA創(chuàng)建SpringBoot項目(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

