广告位联系
返回顶部
分享到

利用Java实现带GUI的气泡诗词特效

java 来源:互联网 作者:佚名 发布时间:2022-08-21 18:54:44 人浏览
摘要

实现效果 实现第个气泡中心显示一个字,在框中随意运动,用空格键按下运行停止,再按下左键运动继续。用鼠标左键按下选中的圆变为填充的,再次按下又变为不填充的。参考:ht

实现效果

实现第个气泡中心显示一个字,在框中随意运动,用空格键按下运行停止,再按下左键运动继续。用鼠标左键按下选中的圆变为填充的,再次按下又变为不填充的。参考:https://github.com/liuyubobobo/

示例代码

AlgoVisualizer.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

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)

        {

            //绘制当前数据

            frame.render(circles);

            AlgoVisHelper.pause(20);

            //更新数据

            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 = "三月七日沙湖道中遇雨。雨具先去,同行皆狼狈,余独不觉。已而遂晴,故作此词 \n" +

                "莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕? 一蓑烟雨任平生。\n" +

                "料峭春风吹酒醒,微冷,山头斜照却相迎。回首向来萧瑟处,归去,也无风雨也无晴。";

  

        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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

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");

        }

    }

  

}


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/moonlightpeng/article/details/126451030
相关文章
  • SpringBoot自定义错误处理逻辑介绍

    SpringBoot自定义错误处理逻辑介绍
    1. 自定义错误页面 将自定义错误页面放在 templates 的 error 文件夹下,SpringBoot 精确匹配错误信息,使用 4xx.html 或者 5xx.html 页面可以打印错误
  • Java实现手写一个线程池的代码

    Java实现手写一个线程池的代码
    线程池技术想必大家都不陌生把,相信在平时的工作中没有少用,而且这也是面试频率非常高的一个知识点,那么大家知道它的实现原理和
  • Java实现断点续传功能的代码

    Java实现断点续传功能的代码
    题目实现:网络资源的断点续传功能。 二、解题思路 获取要下载的资源网址 显示网络资源的大小 上次读取到的字节位置以及未读取的字节
  • 你可知HashMap为什么是线程不安全的
    HashMap 的线程不安全 HashMap 的线程不安全主要体现在下面两个方面 在 jdk 1.7 中,当并发执行扩容操作时会造成环形链和数据丢失的情况 在
  • ArrayList的动态扩容机制的介绍

    ArrayList的动态扩容机制的介绍
    对于 ArrayList 的动态扩容机制想必大家都听说过,之前的文章中也谈到过,不过由于时间久远,早已忘却。 所以利用这篇文章做做笔记,加
  • JVM基础之字节码的增强技术介绍

    JVM基础之字节码的增强技术介绍
    字节码增强技术 在上文中,着重介绍了字节码的结构,这为我们了解字节码增强技术的实现打下了基础。字节码增强技术就是一类对现有字
  • Java中的字节码增强技术

    Java中的字节码增强技术
    1.字节码增强技术 字节码增强技术就是一类对现有字节码进行修改或者动态生成全新字节码文件的技术。 参考地址 2.常见技术 技术分类 类
  • Redis BloomFilter布隆过滤器原理与实现

    Redis BloomFilter布隆过滤器原理与实现
    Bloom Filter 概念 布隆过滤器(英语:Bloom Filter)是1970年由一个叫布隆的小伙子提出的。它实际上是一个很长的二进制向量和一系列随机映射
  • Java C++算法题解leetcode801使序列递增的最小交换次

    Java C++算法题解leetcode801使序列递增的最小交换次
    题目要求 思路:状态机DP 实现一:状态机 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int minSwap(int[] nums1, int[] nums2) { int n
  • Mybatis结果集映射与生命周期介绍

    Mybatis结果集映射与生命周期介绍
    一、ResultMap结果集映射 1、设计思想 对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了 2、resultMap的应用场
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计