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

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

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

實現(xiàn)效果

實現(xiàn)第個氣泡中心顯示一個字,在框中隨意運(yùn)動,用空格鍵按下運(yùn)行停止,再按下左鍵運(yùn)動繼續(xù)。用鼠標(biāo)左鍵按下選中的圓變?yōu)樘畛涞?,再次按下又變?yōu)椴惶畛涞?。參考?a rel="external nofollow" target="_blank">https://github.com/liuyubobobo/

示例代碼

AlgoVisualizer.java

import java.awt.*;
import java.awt.event.*;
 
public class AlgoVisualizer {
    private Object data;
    private Circle[] circles;
    private AlgoFrame frame;
    private boolean isAnmiated = true;
 
    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;
            }
        }
    }
 
    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" +
                "莫聽穿林打葉聲,何妨吟嘯且徐行。竹杖芒鞋輕勝馬,誰怕? 一蓑煙雨任平生。\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);
 
    }
}

AlgoFrame.java

import javax.swing.*;
import java.awt.*;
 
 
public class AlgoFrame extends JFrame {
    private int canvasWidth;
    private int canvasHeight;
    public AlgoFrame(String title, int canvasWidth, int canvasHeight){
 
        super(title);
 
        this.canvasHeight = canvasHeight;
        this.canvasWidth = canvasWidth;
 
 
        AlgoCanvas canvas = new AlgoCanvas();
        setContentPane(canvas);
        pack();
 
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
 
    public AlgoFrame(String title){
        this(title, 1024, 768);
    }
 
 
    public int getCanvasWidth(){return  canvasWidth;}
    public int getCanvasHeight() {return canvasHeight;}
 
    private Circle[] circles;
 
    public void render(Circle[] circles)
    {
        this.circles = circles;
        repaint();
    }
 
    private Object data;
    public void render(Object data)
    {
        this.data = data;
        repaint();
    }
    private class AlgoCanvas extends JPanel{
 
        public AlgoCanvas(){
            super(true);
        }
 
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2d =(Graphics2D)g;
 
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.addRenderingHints(hints);
 
            AlgoVisHelper.setStrokeWidth(g2d, 1);
            AlgoVisHelper.setColor(g2d, Color.RED);
 
            g2d.setFont( new Font("SimSun", Font.BOLD, 16));
 
            for (Circle circle: circles)
            {
                if(!circle.isFilled)
                {
                    AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR() );
                }
                else
                {
                    AlgoVisHelper.fillCircle(g2d, circle.x, circle.y, circle.getR());
                }
 
                if(Circle.showLabel)
                {
//                    AlgoVisHelper.showPoem(g2d,circle.x, circle.y, circle.centerLabel);
                    AlgoVisHelper.drawText(g2d,circle.centerLabel,circle.x, circle.y);
 
                }
            }
 
        }
 
        @Override
        public Dimension getPreferredSize(){
//            System.out.println("Run getPreferredSize()");
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

Circle.java

import java.awt.*;
 
public class Circle {
    public int x, y;
    private int r;
    public int vx, vy;
    public String centerLabel;
    static public boolean showLabel = false;
    public boolean isFilled = false;
 
    public Circle(int x, int y, int r, int vx, int vy)
    {
        this.x = x;
        this.y = y;
        this.r = r;
        this.vx = vx;
        this.vy = vy;
    }
    public Circle(int x, int y, int r, int vx, int vy, String centerLabel)
    {
        this.x = x;
        this.y = y;
        this.r = r;
        this.vx = vx;
        this.vy = vy;
        this.centerLabel = centerLabel;
    }
    public int getR(){return r;}
 
    public void move(int minx, int miny, int maxx, int maxy){
        x += vx;
        y += vy;
 
        checkCollision(minx, miny, maxx, maxy);
    }
 
    private void checkCollision(int minx, int miny, int maxx, int maxy) {
        if (x - r < minx) {
            x = r;
            vx = -vx;
        }
        if (x + r >= maxx) {
            x = maxx - r;
            vx = -vx;
        }
        if (y - r < miny) {
            y = r;
            vy = -vy;
        }
        if (y + r >= maxy) {
            y = maxy - r;
            vy = -vy;
        }
 
    }
 
    public boolean contain(Point p)
    {
        return (x - p.x) * (x - p.x) + (y - p.y)*(y - p.y) <= r*r;
    }
 
}

AlgoVisHelper.java

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
 
public class AlgoVisHelper {
    private AlgoVisHelper(){}
 
    public static void setStrokeWidth(Graphics2D g2d, int w){
        int strokeWidth = w;
        g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    }
 
    public static void strokeCircle(Graphics2D g2d, int x, int y, int r){
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g2d.draw(circle);
    }
 
    public static void fillCircle(Graphics2D g2d, int x, int y, int r){
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g2d.fill(circle);
    }
 
    public static void strokeRectangle(Graphics2D g2d, int x, int y, int w, int h)
    {
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
        g2d.draw(rectangle);
    }
 
    public static void fillRectangle(Graphics2D g2d, int x, int y, int w, int h)
    {
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
        g2d.fill(rectangle);
    }
 
    public static void putImage(Graphics2D g2d, int x, int y, String imageURL){
        ImageIcon icon = new ImageIcon(imageURL);
        Image image = icon.getImage();
        g2d.drawImage(image, x, y, null);
    }
 
    public  static void drawText(Graphics2D g2d, String text, int centerx, int centery)
    {
        if(text == null)
            throw new IllegalArgumentException("Text is null");
 
        FontMetrics metrics = g2d.getFontMetrics();
        int w = metrics.stringWidth(text);
        int h = metrics.getDescent();
        g2d.drawString(text, centerx - w/2, centery + h);
    }
 
 
    public static void showPoem(Graphics2D g2d,  int x, int y, String poem){
 
        g2d.drawString(poem,x, y);
 
//        g2d.drawString("醉", x, y);
    }
    public static void setColor(Graphics2D g2d, Color color){
        g2d.setColor(color);
    }
 
    public static void pause(int t)
    {
        try{
            Thread.sleep(t);
        }
        catch (InterruptedException e){
            System.out.println("Error in sleepping");
        }
    }
 
}

以上就是利用Java實現(xiàn)帶GUI的氣泡詩詞特效的詳細(xì)內(nèi)容,更多關(guān)于Java氣泡詩詞的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Scala遞歸函數(shù)調(diào)用自身

    Scala遞歸函數(shù)調(diào)用自身

    這篇文章主要介紹了Scala遞歸函數(shù),Scala遞歸函數(shù)是一種函數(shù)可以調(diào)用自身的函數(shù),直到滿足某個特定的條件為止。在函數(shù)式編程的語言中,遞歸函數(shù)起著重要的作用,因為它可以用來表示循環(huán)或迭代的邏輯
    2023-04-04
  • IntelliJ?IDEA無公網(wǎng)遠(yuǎn)程Linux服務(wù)器環(huán)境開發(fā)過程(推薦收藏)

    IntelliJ?IDEA無公網(wǎng)遠(yuǎn)程Linux服務(wù)器環(huán)境開發(fā)過程(推薦收藏)

    下面介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實現(xiàn)無公網(wǎng)遠(yuǎn)程連接,然后實現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開發(fā),感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • Java spring事務(wù)及事務(wù)不生效的原因詳解

    Java spring事務(wù)及事務(wù)不生效的原因詳解

    在日常編碼過程中常常涉及到事務(wù),在前兩天看到一篇文章提到了Spring事務(wù),那么在此總結(jié)下在Spring環(huán)境下事務(wù)失效的幾種原因
    2021-09-09
  • java中sdk與jdk的區(qū)別詳細(xì)解析

    java中sdk與jdk的區(qū)別詳細(xì)解析

    以下是對java中sdk與jdk的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下
    2013-08-08
  • SpringBoot實現(xiàn)異步任務(wù)的項目實踐

    SpringBoot實現(xiàn)異步任務(wù)的項目實踐

    本文將使用SpringBoot 去實現(xiàn)異步之間的調(diào)用,提高系統(tǒng)的并發(fā)性能、用戶體驗,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • Spring Cloud Gateway(讀取、修改 Request Body)的操作

    Spring Cloud Gateway(讀取、修改 Request Body)的操作

    這篇文章主要介紹了Spring Cloud Gateway(讀取、修改 Request Body)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java并發(fā)編程中構(gòu)建自定義同步工具

    Java并發(fā)編程中構(gòu)建自定義同步工具

    這篇文章主要介紹了Java并發(fā)編程中構(gòu)建自定義同步工具,本文講解了可阻塞狀態(tài)依賴操作的結(jié)構(gòu)、有界緩存實現(xiàn)基類示例、阻塞實現(xiàn)方式一:拋異常給調(diào)用者、阻塞實現(xiàn)方式二:通過輪詢和休眠、阻塞實現(xiàn)方式三:條件隊列等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Spring Boot中配置文件application.properties使用

    Spring Boot中配置文件application.properties使用

    這篇文章主要介紹了Spring Boot中配置文件application.properties使用及spring boot讀取application.properties文件的方式,需要的朋友參考下吧
    2018-01-01
  • 使用Spring特性實現(xiàn)接口多實現(xiàn)類的動態(tài)調(diào)用方式

    使用Spring特性實現(xiàn)接口多實現(xiàn)類的動態(tài)調(diào)用方式

    這篇文章主要介紹了使用Spring特性實現(xiàn)接口多實現(xiàn)類的動態(tài)調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Fluent Mybatis快速入門詳細(xì)教程

    Fluent Mybatis快速入門詳細(xì)教程

    由于FluentMybatis是基于mybatis上做封裝和擴(kuò)展的,所以這里主要聊聊mybatis處理的方式,以及給出FluentMybatis的解放方案。對Fluent Mybatis入門相關(guān)知識感興趣的朋友一起看看吧
    2021-08-08

最新評論