本文目录导读:

控制台烟花效果 (ASCII Art)
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class ConsoleFireworks {
public static void main(String[] args) throws InterruptedException {
Random random = new Random();
String[] colors = {"\033[31m", "\033[33m", "\033[32m", "\033[34m", "\033[35m", "\033[36m"}; // 红黄绿蓝紫青
String reset = "\033[0m";
System.out.println("\n🎆 Java 庆祝烟花 🎆\n");
for (int i = 0; i < 10; i++) {
// 随机选择颜色
String color = colors[random.nextInt(colors.length)];
// 随机生成烟花形状
int height = random.nextInt(5) + 3;
int width = random.nextInt(7) + 5;
// 清屏 (在大多数终端有效)
System.out.print("\033[H\033[2J");
System.out.flush();
// 打印烟花
for (int row = 0; row < height; row++) {
for (int col = 0; col <= width * 2; col++) {
if (row == 0 || row == height - 1 || col == 0 || col == width * 2) {
System.out.print(color + "*" + reset);
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 随机延迟
TimeUnit.MILLISECONDS.sleep(400 + random.nextInt(600));
}
System.out.println("\n🎉 庆祝完成! 🎉");
}
}
Swing 图形界面庆祝动画
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class CelebrationSwing extends JPanel {
private final String[] messages = {
"🎉 恭喜!", "🎊 庆祝!", "🌟 成功!", "🏆 奖杯!", "🎈 气球队!"
};
private final int[] messageX = new int[5];
private final int[] messageY = new int[5];
private final Color[] colors = {Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.ORANGE};
private final Random random = new Random();
public CelebrationSwing() {
// 初始化消息位置
for (int i = 0; i < messages.length; i++) {
messageX[i] = random.nextInt(400);
messageY[i] = random.nextInt(300);
}
// 动画定时器
Timer timer = new Timer(100, e -> {
// 更新位置(向上飘动)
for (int i = 0; i < messages.length; i++) {
messageY[i] -= 2;
if (messageY[i] < -50) {
messageY[i] = 400;
messageX[i] = random.nextInt(400);
}
}
repaint();
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 绘制渐变背景
GradientPaint gradient = new GradientPaint(0, 0, Color.WHITE,
400, 400, Color.LIGHT_GRAY);
g2d.setPaint(gradient);
g2d.fillRect(0, 0, 400, 400);
// 绘制庆祝文字
g2d.setFont(new Font("微软雅黑", Font.BOLD, 20));
for (int i = 0; i < messages.length; i++) {
g2d.setColor(colors[i]);
g2d.drawString(messages[i], messageX[i], messageY[i]);
}
// 绘制中心庆祝文字
g2d.setFont(new Font("微软雅黑", Font.BOLD, 30));
g2d.setColor(Color.RED);
g2d.drawString("🎉 JAVA 庆祝 🎉", 100, 200);
}
public static void main(String[] args) {
JFrame frame = new JFrame("庆祝案例 - Swing");
CelebrationSwing celebration = new CelebrationSwing();
frame.add(celebration);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
纯文本彩带庆祝效果
public class TextCelebration {
public static void main(String[] args) throws InterruptedException {
String[] emojis = {"🎉", "🎊", "⭐", "🎈", "🏆", "🎁", "💖", "🎀"};
String message = "Java庆祝案例";
String border = "=".repeat(40);
System.out.println(border);
System.out.println(" 🎉 庆祝案例 🎉");
System.out.println(border);
System.out.println();
// 彩带动画
for (int frame = 0; frame < 20; frame++) {
System.out.print("\r");
// 生成彩带效果
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
int idx = (frame + i) % emojis.length;
sb.append(emojis[idx]).append(" ");
}
System.out.print(sb.toString());
System.out.print(" " + message + " ");
System.out.print(sb.reverse().toString());
Thread.sleep(200);
}
System.out.println("\n");
System.out.println(border);
System.out.println(" 🎉 庆祝完成! 🎉");
System.out.println(border);
}
}
使用 JavaFX 的烟花效果(需要 JavaFX 支持)
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.Random;
public class JavaFXFireworks extends Application {
private static final int WIDTH = 600;
private static final int HEIGHT = 500;
private final Random random = new Random();
@Override
public void start(Stage stage) {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
// 动画循环
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(100), e -> drawFrame(gc))
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
stage.setScene(new Scene(new StackPane(canvas), WIDTH, HEIGHT));
stage.setTitle("JavaFX 烟花庆祝");
stage.show();
}
private void drawFrame(GraphicsContext gc) {
// 清屏
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制随机烟花
gc.setFill(randomColor());
gc.setStroke(randomColor());
gc.setLineWidth(2);
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
int radius = random.nextInt(30) + 10;
// 绘制烟花
gc.strokeOval(x - radius, y - radius, radius * 2, radius * 2);
gc.fillOval(x - 3, y - 3, 6, 6);
// 绘制庆祝文字
gc.setFill(Color.WHITE);
gc.setFont(javafx.scene.text.Font.font("Arial", 30));
gc.fillText("🎉 庆祝!", 200, 250);
}
private Color randomColor() {
return Color.color(random.nextDouble(), random.nextDouble(), random.nextDouble());
}
public static void main(String[] args) {
launch(args);
}
}
运行说明
- 控制台烟花: 直接运行,需要在支持 ANSI 颜色的终端(如 Git Bash、Linux/Mac 终端)中运行。
- Swing 动画: 直接运行,Java 标准 GUI 库。
- 文本彩带: 直接运行,纯控制台效果。
- JavaFX 烟花: 需要 JavaFX 库支持(Java 11+ 需额外添加模块)。
这些案例展示了 Java 在不同场景下的庆祝效果,可以根据需要选择适合的版本。