Java實(shí)現(xiàn)浪漫流星表白的示例代碼
介紹
本文實(shí)現(xiàn)的功能有:
1、播放音樂
2、自定義流星數(shù)量、飛行速度、光暈大小、流星大小
3、自定義表白話語
運(yùn)用到的知識(shí)點(diǎn)有:
GUI:java實(shí)現(xiàn)窗體、Swing。其實(shí)JAVA Swing的GUI目前企業(yè)中已經(jīng)不用了,主要是一些學(xué)校和培訓(xùn)機(jī)構(gòu)用來教導(dǎo)學(xué)生寫一些游戲、小項(xiàng)目,練練手的。
多線程:讓cpu同一時(shí)間處理多個(gè)任務(wù)(本文中涉及到音樂、文字緩慢出現(xiàn)、流星線條移動(dòng))
效果圖:
音樂類(其實(shí)也可以不用音樂,有些人并不喜歡):
核心代碼
import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class MusicThread extends Thread{ @Override public void run() { //播放音樂 System.out.println("開始播放"); //表示音樂文件 File f = new File("nv.mp3"); //第三方j(luò)ar包 Player類 try { Player p = new Player(new FileInputStream(f));//參數(shù):文件輸入流對(duì)象 // p.play(); } catch (FileNotFoundException | JavaLayerException e) { e.printStackTrace(); } } }
實(shí)現(xiàn)類:
import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Random; import javax.swing.*; public class MeteorFly extends JFrame { int AppletWidth, AppletHeight; final int MAX = 6; // (~)流星的個(gè)數(shù) final int SLEEP = 2; // 流星飛行的速度(數(shù)值越大,速度越慢) final int COLORLV = 1; // (~)色階(可改變光暈大?。? final int SIZE = 3 ; // (~)流星大小 private MyPanel panel; public MeteorFly() { panel = new MyPanel(); this.setTitle("LOVE"); this.getContentPane().add(panel); this.setSize(AppletWidth, AppletHeight); // 創(chuàng)建窗體 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new Thread(){ @Override public void run() { //聲明一個(gè)File對(duì)象 File mp3 = new File("nv.mp3"); //創(chuàng)建一個(gè)輸入流 FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(mp3); //創(chuàng)建一個(gè)緩沖流 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //創(chuàng)建播放器對(duì)象,把文件的緩沖流傳入進(jìn)去 Player player = new Player(fileInputStream); //調(diào)用播放方法進(jìn)行播放 player.play(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JavaLayerException e) { e.printStackTrace(); } } }.start(); new MeteorFly(); } class MyPanel extends JPanel implements Runnable { Meteor p[]; BufferedImage OffScreen; Graphics drawOffScreen; Thread pThread; Font drawFont = new Font("Arial",0,28); public MyPanel() { //setBackground(Color.black); //窗體初始化 AppletWidth = Toolkit.getDefaultToolkit().getScreenSize().width; AppletHeight = Toolkit.getDefaultToolkit().getScreenSize().height-200; p = new Meteor[MAX]; for (int i = 0; i < MAX; i++) { p[i] = new Meteor(); } OffScreen = new BufferedImage(AppletWidth, AppletHeight, BufferedImage.TYPE_INT_BGR); drawOffScreen = OffScreen.getGraphics(); pThread = new Thread(this); pThread.start(); new Thread(){ @Override public void run() { try { sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } str1 = "流星雨是世間寶藏,而你是我的人間理想"; while(true){ try { sleep(150); } catch (InterruptedException e) { e.printStackTrace(); } pos++; if (pos > str1.length() - 1) { pos = str1.length() - 1; break; } } } }.start(); } int pos = 0; String str1 = " "; @Override public void paintComponent(Graphics g) { super.paintComponents(g); g.drawImage(OffScreen, 0, 0, this); g.setColor(Color.pink); g.setFont(new Font("宋體", Font.BOLD, 50)); g.drawString(str1.substring(0,pos+1),260,700); } @Override public void run() { while (true) { for (int i = 0; i < MAX; i++) { drawOffScreen.setColor(p[i].color); // RGB顏色 drawOffScreen.fillOval(p[i].x, p[i].y, SIZE, SIZE); p[i].x += p[i].mx; p[i].y += p[i].my; int x = p[i].x; int y = p[i].y; int R = p[i].color.getRed(); // 提取顏色 int G = p[i].color.getGreen(); int B = p[i].color.getBlue(); while (true) { if (R ==0 && G ==0 && B ==0 ) { break; } R -= COLORLV; // 尾部顏色淡化 if (R <0 ) { R =0 ; } G -= COLORLV; if (G <0 ) { G =0 ; } B -= COLORLV; if (B < 0) { B =0 ; } Color color = new Color(R, G, B); x -= p[i].mx; // 覆蓋尾部 y -= p[i].my; drawOffScreen.setColor(color); drawOffScreen.fillOval(x, y, SIZE, SIZE); } if (x > AppletWidth || y > AppletHeight) { // 流星飛出窗口,重置流星 p[i].reset(); } } repaint(); try { Thread.sleep(SLEEP); } catch (InterruptedException e) { } } } } class Meteor { // 流星類 int x, y; // 流星的位置 int mx, my; // 下落速度 Color color; // 流星顏色 Random r = new Random(); public Meteor() { reset(); } public void reset() { int rand = (int) (Math.random() *100 ); //隨機(jī)生成流星出現(xiàn)位置 if (rand >35 ) { x = (int) (Math.random() *600 ); y = 0; } else { y = (int) (Math.random() * 150); x =0 ; } mx = r.nextInt(2)+2; //隨機(jī)生成下落速度和角度 my = 1; color = new Color( // 隨機(jī)顏色 (new Double(Math.random() *128 )).intValue() +128 , (new Double(Math.random() *128 )).intValue() +128 , (new Double(Math.random() * 128)).intValue() + 128); } } }
這里的Player類需要自己導(dǎo)包,包我放在這個(gè)鏈接里了:
鏈接 提取碼: v22q
注意事項(xiàng)
導(dǎo)包過程中可能有些人會(huì)出現(xiàn)這種問題:
實(shí)際應(yīng)該是可以打開的:
如何導(dǎo)包:
先要將包復(fù)制粘貼到項(xiàng)目包下
然后進(jìn)入:File –> Project Structure
然后依次點(diǎn)擊Libraries、+號(hào)、Java
找到你要導(dǎo)的文件的位置,然后一直點(diǎn)OK就行了。
以上就是Java實(shí)現(xiàn)浪漫流星表白的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java流星的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決IDEA中下載free maven plugin插件無效的問題
這篇文章主要介紹了解決IDEA中下載free maven plugin插件無效的問題,本文通過圖文并茂的形式給大家分享解決方案,供大家參考,需要的朋友可以參考下2020-11-11詳解Java?POI?excel自定義設(shè)置單元格格式
這篇文章主要介紹了Java?POI?excel設(shè)置單元格格式,自定義設(shè)置,設(shè)置單元格格式:來源_formats,更多數(shù)據(jù)類型從formats里面發(fā)現(xiàn),需要的朋友可以參考下2024-01-01java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于java利用Future實(shí)現(xiàn)多線程執(zhí)行與結(jié)果聚合的相關(guān)資料,Future模式的核心,去除了主函數(shù)的等待時(shí)間,并使得原本需要等待的時(shí)間段可以用于處理其他業(yè)務(wù)邏輯,需要的朋友可以參考下2021-12-12解決Spring配置文件中bean的property屬性中的name出錯(cuò)問題
這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java8處理集合的優(yōu)雅姿勢(shì)之Stream
這篇文章主要給大家介紹了關(guān)于Java8優(yōu)雅處理集合之Stream的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03