前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >气球飘飘:用Java Swing创造令人心旷神怡的视觉奇观

气球飘飘:用Java Swing创造令人心旷神怡的视觉奇观

作者头像
命运之光
发布2024-03-20 11:09:35
750
发布2024-03-20 11:09:35
举报

✨博主:命运之光 ✨专栏:Java经典程序设计

前言:这篇博客在手机上打开可能会自动播放视频,视频有音乐,请及时关闭手机音乐哈🙂

✨导语

欢迎来到我的博客!你是否曾经想象过能够创造出一个令人心旷神怡的视觉奇观?在本文中,我将向你展示如何利用Java Swing技术,以及一些简单的代码,让彩色气球在屏幕上飘舞起来。这种视觉效果不仅适合儿童节,也可以为你的应用程序增添一份活力和趣味。让我们一起进入这个有趣的编程世界,为我们的程序注入一些令人愉悦的色彩吧!快来探索如何编写这个令人惊叹的气球飘飘效果,给你的应用程序带来惊喜和乐趣吧!


✨引言

儿童节快乐!在这个特殊的日子里,我想向所有的孩子们致以最诚挚的祝福。作为一位热爱编程和创造的开发者,我决定写下这篇博客,与大家分享一个简单而有趣的项目,让我们一起用Java Swing创造一个令人愉悦的气球飘飘效果。


简单介绍一下Javaswing这项技术简单介绍一下Javaswing这项技术(选读,感兴趣的可以了解一下哈(●'◡'●))

Javaswing是一种基于Java的图形用户界面(GUI)技术,它可以让开发者创建出丰富多彩的桌面应用程序。Javaswing提供了一系列的组件,如按钮、文本框、菜单、表格、树形结构等,以及一些高级功能,如拖放、撤销/重做、打印等。Javaswing的优点是跨平台、可定制、可扩展和易于使用。Javaswing的缺点是性能较低、内存占用较高、学习曲线较陡和与本地系统的集成度较低。本文将简单介绍一下Javaswing的历史、架构和应用范例,希望能给有兴趣的读者提供一些参考。 Javaswing的历史可以追溯到1995年,当时Java刚刚诞生,它的图形用户界面技术是基于AWT(Abstract Window Toolkit)的。AWT是一种与本地系统紧密耦合的技术,它使用了本地系统的组件和事件模型,因此具有很好的性能和集成度,但也导致了跨平台性和可定制性的问题。为了解决这些问题,Sun公司在1997年推出了JFC(Java Foundation Classes),其中包含了一个新的图形用户界面技术——Swing。Swing是一种纯Java的技术,它不依赖于本地系统的组件,而是自己绘制所有的界面元素,因此具有很好的跨平台性和可定制性,但也牺牲了一些性能和集成度。Swing最初是作为一个可选的扩展包提供的,后来在1998年被集成到了Java 2平台中,并成为了Java标准图形用户界面技术。 Javaswing的架构主要包括以下几个方面:组件、模型、事件、布局和外观。组件是指Swing提供的各种图形用户界面元素,如按钮、文本框等,它们都继承自javax.swing.JComponent类,这个类又继承自java.awt.Component类,因此Swing组件也可以使用AWT的一些特性,如容器、图形和字体等。模型是指Swing组件所依赖的数据结构,如按钮的状态、文本框的内容等,它们都实现了javax.swing.plaf.ComponentUI接口,这个接口定义了组件的基本行为和外观。事件是指Swing组件所响应的用户操作或系统通知,如鼠标点击、键盘输入等,它们都继承自java.awt.AWTEvent类,并使用了观察者模式来实现事件监听和分发。布局是指Swing组件在容器中的排列方式,如水平对齐、垂直对齐等,它们都实现了java.awt.LayoutManager接口,并提供了一些常用的布局管理器,如流式布局、网格布局等。外观是指Swing组件的视觉效果,如颜色、字体、图标等,它们都实现了javax.swing.plaf.ComponentUI接口,并提供了一些常用的外观风格,如金属风格、Windows风格等。 Javaswing的应用范例有很多,这里只举几个简单的例子。第一个例子是一个简单的计算器程序,它使用了Swing的按钮、文本框、标签和网格布局等组件,以及事件监听和处理等机制,实现了基本的加减乘除功能。第二个例子是一个简单的文本编辑器程序,它使用了Swing的文本区域、菜单、工具栏和滚动条等组件,以及拖放、撤销/重做和打印等功能,实现了基本的文本编辑功能。第三个例子是一个简单的图片浏览器程序,它使用了Swing的列表、标签和滑块等组件,以及图形和文件操作等技术,实现了基本的图片浏览功能。 以上就是对Javaswing这项技术的简单介绍,希望能对读者有所帮助。如果读者想要深入学习Javaswing,可以参考以下几本书籍:《Java Swing》(O'Reilly出版社),《Core Java Volume II - Advanced Features》(Prentice Hall出版社),《Java GUI程序设计》(清华大学出版社)。


✨程序展示(1)

✨程序展示(2)


✨视频展示(1)

六一儿童节快乐(气球飘飘)

✨视频展示(2)

六一儿童节快乐(气球飘飘2)


🍓源代码(气球飘飘)
代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import javax.swing.*;

public class BalloonAnimation extends JFrame {
    private static final int FRAME_WIDTH = 800;
    private static final int FRAME_HEIGHT = 600;
    private static final int BALLOON_COUNT = 5;
    private static final int BALLOON_SPACING = 50;
    private static final int MAX_DELAY = 1000; // 最大延迟时间(毫秒)
    private static final int MIN_SPEED = 1; // 最小速度
    private static final int MAX_SPEED = 2; // 最大速度

    private List<Point> balloons;
    private List<Color> balloonColors;
    private List<Integer> balloonDelays;
    private List<Integer> balloonSpeeds;

    public BalloonAnimation() {
        setTitle("气球飘飘");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);

        balloons = new ArrayList<>();
        balloonColors = new ArrayList<>();
        balloonDelays = new ArrayList<>();
        balloonSpeeds = new ArrayList<>();
        generateBalloons();

        BalloonPanel balloonPanel = new BalloonPanel();
        add(balloonPanel);

        setVisible(true);

        startAnimation();
    }

    private void generateBalloons() {
        int totalWidth = BALLOON_COUNT * (BalloonPanel.BALLOON_WIDTH + BALLOON_SPACING) - BALLOON_SPACING;
        int startX = (FRAME_WIDTH - totalWidth) / 2;

        Random random = new Random();

        for (int i = 0; i < BALLOON_COUNT; i++) {
            int x = startX + i * (BalloonPanel.BALLOON_WIDTH + BALLOON_SPACING);
            int y = FRAME_HEIGHT;
            balloons.add(new Point(x, y));

            // 生成随机颜色
            int red = random.nextInt(256);
            int green = random.nextInt(256);
            int blue = random.nextInt(256);
            Color color = new Color(red, green, blue);
            balloonColors.add(color);

            // 生成随机延迟时间
            int delay = random.nextInt(MAX_DELAY);
            balloonDelays.add(delay);

            // 生成随机速度
            int speed = random.nextInt(MAX_SPEED - MIN_SPEED + 1) + MIN_SPEED;
            balloonSpeeds.add(speed);
        }
    }

    private void startAnimation() {
        for (int i = 0; i < BALLOON_COUNT; i++) {
            int index = i;
            Timer timer = new Timer(balloonDelays.get(i), e -> {
                Point balloon = balloons.get(index);
                balloon.y -= balloonSpeeds.get(index); // 根据速度移动气球位置
                repaint();

                if (balloon.y <= -BalloonPanel.BALLOON_HEIGHT) {
                    // 当气球到达顶部后,重新设置气球位置到底部
                    balloon.y = FRAME_HEIGHT;
                }

                Timer nextTimer = new Timer(20, evt -> {
                    Point nextBalloon = balloons.get(index);
                    nextBalloon.y -= balloonSpeeds.get(index); // 根据速度移动气球位置
                    repaint();

                    if (nextBalloon.y <= -BalloonPanel.BALLOON_HEIGHT) {
                        // 当气球到达顶部后,重新设置气球位置到底部
                        nextBalloon.y = FRAME_HEIGHT;
                    }
                });
                nextTimer.setRepeats(true);
                nextTimer.start();
            });
            timer.setRepeats(false);
            timer.start();
        }
    }

    private class BalloonPanel extends JPanel {
        private static final int BALLOON_WIDTH = 100;
        private static final int BALLOON_HEIGHT = 150;
        private static final int LINE_LENGTH = 90; // 线段的长度

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (int i = 0; i < balloons.size(); i++) {
                Point balloon = balloons.get(i);
                int x = balloon.x;
                int y = balloon.y;
                Color color = balloonColors.get(i);

                // 绘制气球
                g.setColor(color);
                g.fillOval(x, y, BALLOON_WIDTH, BALLOON_HEIGHT);

                // 绘制垂直线段
                int lineX = x + BALLOON_WIDTH / 2;
                int startY = y + BALLOON_HEIGHT;
                int endY = startY + LINE_LENGTH;
                g.setColor(Color.BLACK);
                g.drawLine(lineX, startY, lineX, endY);

                // 绘制文字
                Font font = new Font("宋体", Font.BOLD, 18);
                g.setFont(font);
                g.setColor(Color.black);
                String text = "六一儿童节快乐!";
                int textWidth = g.getFontMetrics(font).stringWidth(text);
                int textX = x + (BALLOON_WIDTH - textWidth) / 2;
                int textY = endY + 20; // 文字位于线段结束点的下方一定距离
                g.drawString(text, textX, textY);
            }
        }
    }





    public static void main(String[] args) {
        SwingUtilities.invokeLater(BalloonAnimation::new);
    }
}
🍓源代码(气球飘飘2)
代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class BalloonAnimation extends JFrame {
    private static final int FRAME_WIDTH = 800;
    private static final int FRAME_HEIGHT = 600;
    private static final int BALLOON_COUNT = 5;
    private static final int BALLOON_SPACING = 50;
    private static final int MAX_DELAY = 5000; // 最大延迟时间(毫秒)

    private List<Point> balloons;
    private List<Color> balloonColors;
    private List<Integer> balloonDelays;

    public BalloonAnimation() {
        setTitle("气球飘飘");
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);

        balloons = new ArrayList<>();
        balloonColors = new ArrayList<>();
        balloonDelays = new ArrayList<>();
        generateBalloons();

        BalloonPanel balloonPanel = new BalloonPanel();
        add(balloonPanel);

        setVisible(true);

        startAnimation();
    }

    private void generateBalloons() {
        int totalWidth = BALLOON_COUNT * (BalloonPanel.BALLOON_WIDTH + BALLOON_SPACING) - BALLOON_SPACING;
        int startX = (FRAME_WIDTH - totalWidth) / 2;

        Random random = new Random();

        for (int i = 0; i < BALLOON_COUNT; i++) {
            int x = startX + i * (BalloonPanel.BALLOON_WIDTH + BALLOON_SPACING);
            int y = FRAME_HEIGHT;
            balloons.add(new Point(x, y));

            // 生成随机颜色
            int red = random.nextInt(256);
            int green = random.nextInt(256);
            int blue = random.nextInt(256);
            Color color = new Color(red, green, blue);
            balloonColors.add(color);

            // 生成随机延迟时间
            int delay = random.nextInt(MAX_DELAY);
            balloonDelays.add(delay);
        }
    }

    private void startAnimation() {
        for (int i = 0; i < BALLOON_COUNT; i++) {
            int index = i;
            Timer timer = new Timer(balloonDelays.get(i), e -> {
                Point balloon = balloons.get(index);
                balloon.y -= 1;
                repaint();

                if (balloon.y > 0) {
                    Timer nextTimer = new Timer(20, evt -> {
                        Point nextBalloon = balloons.get(index);
                        nextBalloon.y -= 1;
                        repaint();
                    });
                    nextTimer.setRepeats(true);
                    nextTimer.start();
                }
            });
            timer.setRepeats(false);
            timer.start();
        }
    }

    private class BalloonPanel extends JPanel {
        private static final int BALLOON_WIDTH = 100;
        private static final int BALLOON_HEIGHT = 150;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (int i = 0; i < balloons.size(); i++) {
                Point balloon = balloons.get(i);
                int x = balloon.x;
                int y = balloon.y;
                Color color = balloonColors.get(i);

                g.setColor(color);
                g.fillOval(x, y, BALLOON_WIDTH, BALLOON_HEIGHT);

                Font font = new Font("宋体", Font.BOLD, 15);
                g.setFont(font);
                g.setColor(Color.black);
                String text = "六一儿童节快乐!";
                int textWidth = g.getFontMetrics(font).stringWidth(text);
                int textX = x + (BALLOON_WIDTH - textWidth) / 2;
                int textY = y + BALLOON_HEIGHT / 2;
                g.drawString(text, textX, textY);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(BalloonAnimation::new);
    }
}

✨结语

🍓🍓儿童节快乐!通过这篇博客,我们一起探索了使用Java Swing创建气球飘飘效果的乐趣和创造力。我们看到了编程的魔力,如何用几行简单的代码就能营造出令人心旷神怡的视觉奇观🎉🎉🎉🎉

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ✨导语
  • ✨引言
  • ✨简单介绍一下Javaswing这项技术简单介绍一下Javaswing这项技术(选读,感兴趣的可以了解一下哈(●'◡'●))
  • ✨程序展示(1)
  • ✨程序展示(2)
  • ✨视频展示(1)
  • ✨视频展示(2)
    • 🍓源代码(气球飘飘)
      • 🍓源代码(气球飘飘2)
      • ✨结语
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档