二、創(chuàng)意角度一年一度的圣誕節(jié)來了 讓我們一起動動小手 給平凡而普通的生活 添加一筆色彩吧
看看誰敢說程序員不懂浪漫? 程序員一天能new 1024個對象(GC 此時有話要說)
三、java swing版 效果展示從代碼,項目標簽,linux等多方面 畫一顆圣誕樹,讓圣誕變得花里胡哨!
(播放有音樂)
(基于jdk11)
package view;
public class Main {// 程序入口,運行此處
public static void main(String[] args) { try { new MyFrame();
} catch (Exception e) { e.printStackTrace();
}
}
}
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);
}
}
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;
}
}
}
目錄結構、資源路徑:
音樂推薦wav格式,且開關圖片、星星圖片、音樂可以自由替換 ,替換后注意文件名保持一致 或在代碼里面將相應文件名更改。
靜態(tài)資源、代碼地址:
https://github.com/qiuhuanhen/christmasTree
想想你把這個效果提交到git dev分支之后,同事啟動項目時會不會覺得小驚喜呢
在resources目錄下新建banner.txt文件 文件名必須是這個,這是springboot的機制,根據(jù)名字讀取。(和我們的application.yml處于同級目錄)
將符號文字復制進去,啟動項目即可。同樣的 符號文字也是可以隨意替換的 我們可以在網(wǎng)上找更好看的符號 或者動手能力強的同學可以自己設計
∵∴∵︿︿︿︿︿︿..∴∵∴∵∴∵∴☆∵∴∵
∴∵/ ?。埽堋摺嗦}∵∴∵/\∴∵∴
∵/ \\∵∴∵∴/?。堋摺?╭~~~~~~~~~╮○誕∴-/ . ?。堋?╰~~~~~~~~~╯∵∴/ . ★?。?/ \∴快○-- ̄/.?。埽?| ∩ ∩ ---|∵∴∵/★?。。堋?| ? ? ---|∴樂-/ .?。?\ ﹏ -/∴∵--○ ̄/ ̄ ̄\ ̄○
∵\ --/∴∵!∴∵| ?。摺?∴∵ ̄ ̄ ̄ ̄ ̄ ̄ ̄∵∴∵--∴∵∴╰--╯∵∴
〓〓〓◇◇◇〓〓〓○○○〓〓〓☆☆☆〓〓〓〓
七、 linux shell界面打印版 效果展示我們首先找到張圣誕樹圖片
圖片需要轉換成pnm格式 可以利用在線轉換網(wǎng)站或者工具進行轉換 , 轉換后的文件名字 重命名 例如叫 merry.pnm
將圖片上傳至linux,
centos使用ascillview merry.pnm命令
Ubuntu使用aview merry.pnm命令
注: 使用命令前需要先安裝aa-lib,aview,ImageMagick等環(huán)境 ,
具體教程可以在我博客主頁搜索 : 如何實現(xiàn)將圖片用代碼打印出來
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
名稱欄目:【圣誕快樂】如何用代碼畫一顆圣誕樹?-創(chuàng)新互聯(lián)
新聞來源:http://www.rwnh.cn/article42/csodhc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、App設計、App開發(fā)、移動網(wǎng)站建設、服務器托管、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容