资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

【圣诞快乐】如何用代码画一颗圣诞树?-创新互联

文章目录
    • 一、前言
    • 二、创意角度
    • 三、java swing版 效果展示
    • 四、java swing版 实现步骤&代码
    • 五、springboot项目banner版 效果展示
    • 六、springboot项目banner版 实现步骤
    • 七、 linux shell界面打印版 效果展示
    • 八、 linux shell界面打印版 实现步骤

贵港ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!一、前言

一年一度的圣诞节来了 让我们一起动动小手 给平凡而普通的生活 添加一笔色彩吧
看看谁敢说程序员不懂浪漫? 程序员一天能new 1024个对象(GC 此时有话要说)

二、创意角度

从代码,项目标签,linux等多方面 画一颗圣诞树,让圣诞变得花里胡哨!

三、java swing版 效果展示

(播放有音乐)
在这里插入图片描述

四、java swing版 实现步骤&代码

(基于jdk11)

  1. main方法
package view;

public class Main {// 程序入口,运行此处
   public static void main(String[] args) {   try {   new MyFrame();
       } catch (Exception e) {   e.printStackTrace();
       }
   }
}
  1. JFrame方法
package view;

import javax.swing.*;

public class MyFrame extends JFrame {MyPanel p;

    MyFrame() throws Exception {p = new MyPanel();
        add(p);
        setBounds(400, 200, 800, 800);
        setVisible(true);
        validate();
        setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
    }
}
  1. Jpanel方法(核心实现)
package view;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

public class MyPanel extends JPanel implements ActionListener {// 图片、音乐路径 音乐推荐wav格式 
    static final String MUSIC = "src/resouce/music.wav";
    static final String STAR_SHINE = "src/resouce/STAR_SHINE.png";
    static final String STAR_NOT_SHINE = "src/resouce/STAR_NOT_SHINE.png";
    static final String ON = "src/resouce/ON.png";
    static final String OFF = "src/resouce/OFF.png";

    int x, y;
    JButton onOff;
    Timer time;
    boolean flag;
    boolean color;
    File file = new File(MUSIC);
    URL url = null;
    URI uri = null;
    //   since jdk9 : Clip (jdk9 before : AudioClip)
    Clip clip = null;
    AudioInputStream ais = null;

    MyPanel() throws Exception {setLayout(null);
        ImageIcon icon = new ImageIcon(OFF);
        icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
        onOff = new JButton();
        onOff.addActionListener(this);
        onOff.setIcon(icon);
        onOff.setBorder(null);
        onOff.setContentAreaFilled(false);
        onOff.setBounds(0, 0, 50, 50);
        add(onOff);
        flag = true;
        color = true;
        time = new Timer(300, this);
        time.stop();
        try {uri = file.toURI();
            url = uri.toURL();
        } catch (MalformedURLException e1) {System.out.println(e1);
        }
        clip = AudioSystem.getClip();
        ais = AudioSystem.getAudioInputStream(file);
        clip.open(ais);

    }

    public void paintComponent(Graphics g) {x = 380;
        y = 100;
        if (color) {ImageIcon image1 = new ImageIcon(STAR_NOT_SHINE);
            g.drawImage(image1.getImage(), x - 3, y - 25, 28, 26, null);
        } else {ImageIcon image1 = new ImageIcon(STAR_SHINE);
            g.drawImage(image1.getImage(), x - 3, y - 25, 25, 25, null);
        }
        Color red = new Color(255, 0, 0);
        Color yellow = new Color(255, 241, 0);
        drawTree(1, 4, g);
        if (color) {drawDecoration(x + 22, y - 44, 6, yellow, g);
            drawDecoration(x, y - 22, 8, red, g);
        } else {drawDecoration(x + 22, y - 44, 6, red, g);
            drawDecoration(x, y - 22, 8, yellow, g);
        }
        x = 380 - 2 * 22;
        drawTree(3, 6, g);
        if (color) {drawDecoration(x + 22, y - 44, 10, yellow, g);
            drawDecoration(x, y - 22, 12, red, g);
        } else {drawDecoration(x + 22, y - 44, 10, red, g);
            drawDecoration(x, y - 22, 12, yellow, g);
        }
        x = 380 - 4 * 22;
        drawTree(5, 8, g);
        if (color) {drawDecoration(x + 22, y - 44, 14, yellow, g);
            drawDecoration(x, y - 22, 16, red, g);
        } else {drawDecoration(x + 22, y - 44, 14, red, g);
            drawDecoration(x, y - 22, 16, yellow, g);
        }
        x = 380 - 1 * 22;
        drwaRoot(g);
    }

    void drawTree(int from, int to, Graphics g) {Color c = new Color(9, 124, 37);
        g.setColor(c);
        for (int i = from; i<= to; i++) {for (int j = 0; j< (i * 2 - 1); j++) {g.fillRect(x, y, 20, 20);
                x += 22;
            }
            x = 380 - i * 22;
            y += 22;
        }
    }

    void drawDecoration(int tx, int ty, int num, Color c, Graphics g) {g.setColor(c);
        g.fillRoundRect(tx, ty, 18, 18, 18, 18);
        g.fillRoundRect(tx + num * 22, ty, 18, 18, 18, 18);
    }

    void drwaRoot(Graphics g) {Color c = new Color(131, 78, 0);
        g.setColor(c);
        for (int i = 0; i< 4; i++) {for (int j = 0; j< 3; j++) {g.fillRect(x, y, 20, 20);
                x += 22;
            }
            x = 380 - 1 * 22;
            y += 22;
        }
    }

    public void actionPerformed(ActionEvent e) {if (e.getSource() == onOff) {if (flag) {ImageIcon icon = new ImageIcon(ON);
                icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
                onOff.setIcon(icon);
                try {clip.start();
                } catch (Exception exc) {exc.printStackTrace();
                }
                flag = false;
                clip.setLoopPoints(0, -1);
                time.restart();
            } else {ImageIcon icon = new ImageIcon(OFF);
                icon.setImage(icon.getImage().getScaledInstance(50, 50, 0));
                onOff.setIcon(icon);
                flag = true;
                time.stop();
                clip.stop();
            }
        } else if (e.getSource() == time) {repaint();
            color = !color;
        }
    }
}
  1. 目录结构、资源路径:
    在这里插入图片描述
    音乐推荐wav格式,且开关图片、星星图片、音乐可以自由替换 ,替换后注意文件名保持一致 或在代码里面将相应文件名更改。

  2. 静态资源、代码地址:
    https://github.com/qiuhuanhen/christmasTree

五、springboot项目banner版 效果展示

想想你把这个效果提交到git dev分支之后,同事启动项目时会不会觉得小惊喜呢
在这里插入图片描述

六、springboot项目banner版 实现步骤
  1. 在resources目录下新建banner.txt文件 文件名必须是这个,这是springboot的机制,根据名字读取。(和我们的application.yml处于同级目录)

  2. 将符号文字复制进去,启动项目即可。同样的 符号文字也是可以随意替换的 我们可以在网上找更好看的符号 或者动手能力强的同学可以自己设计

∵∴∵︿︿︿︿︿︿..∴∵∴∵∴∵∴☆∵∴∵
∴∵/     \\∵∴聖∵∴∵/\∴∵∴
∵/       \\∵∴∵∴/ .\∵∴
╭~~~~~~~~~╮○誕∴-/ .  \∵
╰~~~~~~~~~╯∵∴/ .  ★ \
/         \∴快○-- ̄/. \ ̄○
|  ∩   ∩  ---|∵∴∵/★ . \∵
|       ---|∴樂-/    . \
\    ﹏    -/∴∵--○ ̄/ ̄ ̄\ ̄○
∵\       --/∴∵!∴∵|  |∵∴
∴∵ ̄ ̄ ̄ ̄ ̄ ̄ ̄∵∴∵--∴∵∴╰--╯∵∴
〓〓〓◇◇◇〓〓〓○○○〓〓〓☆☆☆〓〓〓〓
七、 linux shell界面打印版 效果展示

在这里插入图片描述

八、 linux shell界面打印版 实现步骤
  1. 我们首先找到张圣诞树图片
    在这里插入图片描述

  2. 图片需要转换成pnm格式 可以利用在线转换网站或者工具进行转换 , 转换后的文件名字 重命名 例如叫 merry.pnm

  3. 将图片上传至linux,

  4. centos使用ascillview merry.pnm命令
    Ubuntu使用aview merry.pnm命令

注: 使用命令前需要先安装aa-lib,aview,ImageMagick等环境 ,
具体教程可以在我博客主页搜索 : 如何实现将图片用代码打印出来

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享名称:【圣诞快乐】如何用代码画一颗圣诞树?-创新互联
浏览地址:http://cdkjz.cn/article/csodhc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220