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

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

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

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

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ù)醉,歸來(lái)仿佛三更。" +
            "家童鼻息已雷鳴。敲門都不應(yīng),倚杖聽江聲。\n" +
            "\n" +
            "長(zhǎng)恨此身非我有,何時(shí)忘卻營(yíng)營(yíng)。" +
            "夜闌風(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)
        {
            // 空格 動(dòng)畫
            if(event.getKeyChar() == ' ')
            {
                isAnmiated = !isAnmiated;
            }
 
 
//            +事件加速,跑的更快
            if(event.getKeyChar() == '+')
            {
//                System.out.println("加速++++++");
                for(Circle circle:circles)
                {
                    circle.vx *= 2;
                    circle.vy *= 2;
 
                }
 
            }
//    —減速,慢一點(diǎn)
            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 = "三月七日沙湖道中遇雨。雨具先去,同行皆狼狽,余獨(dú)不覺。已而遂晴,故作此詞 \n" +
                "莫聽穿林打葉聲,何妨吟嘯且徐行。竹杖芒鞋輕勝馬,誰(shuí)怕? 一蓑煙雨任平生。\n" +
                "料峭春風(fēng)吹酒醒,微冷,山頭斜照卻相迎?;厥紫騺?lái)蕭瑟處,歸去,也無(wú)風(fēng)雨也無(wú)晴。";
 
        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實(shí)現(xiàn)帶GUI的氣泡詩(shī)詞效果的文章就介紹到這了,更多相關(guān)Java氣泡詩(shī)詞內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論