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