Java實現(xiàn)浪漫流星表白的示例代碼
介紹
本文實現(xiàn)的功能有:
1、播放音樂
2、自定義流星數(shù)量、飛行速度、光暈大小、流星大小
3、自定義表白話語
運用到的知識點有:
GUI:java實現(xiàn)窗體、Swing。其實JAVA Swing的GUI目前企業(yè)中已經(jīng)不用了,主要是一些學校和培訓機構用來教導學生寫一些游戲、小項目,練練手的。
多線程:讓cpu同一時間處理多個任務(本文中涉及到音樂、文字緩慢出現(xiàn)、流星線條移動)
效果圖:
音樂類(其實也可以不用音樂,有些人并不喜歡):
核心代碼
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"); //第三方jar包 Player類 try { Player p = new Player(new FileInputStream(f));//參數(shù):文件輸入流對象 // p.play(); } catch (FileNotFoundException | JavaLayerException e) { e.printStackTrace(); } } }
實現(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; // (~)流星的個數(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() { //聲明一個File對象 File mp3 = new File("nv.mp3"); //創(chuàng)建一個輸入流 FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(mp3); //創(chuàng)建一個緩沖流 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //創(chuàng)建播放器對象,把文件的緩沖流傳入進去 Player player = new Player(fileInputStream); //調(diào)用播放方法進行播放 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 ); //隨機生成流星出現(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; //隨機生成下落速度和角度 my = 1; color = new Color( // 隨機顏色 (new Double(Math.random() *128 )).intValue() +128 , (new Double(Math.random() *128 )).intValue() +128 , (new Double(Math.random() * 128)).intValue() + 128); } } }
這里的Player類需要自己導包,包我放在這個鏈接里了:
鏈接 提取碼: v22q
注意事項
導包過程中可能有些人會出現(xiàn)這種問題:
實際應該是可以打開的:
如何導包:
先要將包復制粘貼到項目包下
然后進入:File –> Project Structure
然后依次點擊Libraries、+號、Java
找到你要導的文件的位置,然后一直點OK就行了。
以上就是Java實現(xiàn)浪漫流星表白的示例代碼的詳細內(nèi)容,更多關于Java流星的資料請關注腳本之家其它相關文章!
相關文章
解決IDEA中下載free maven plugin插件無效的問題
這篇文章主要介紹了解決IDEA中下載free maven plugin插件無效的問題,本文通過圖文并茂的形式給大家分享解決方案,供大家參考,需要的朋友可以參考下2020-11-11java利用Future實現(xiàn)多線程執(zhí)行與結果聚合實例代碼
這篇文章主要給大家介紹了關于java利用Future實現(xiàn)多線程執(zhí)行與結果聚合的相關資料,Future模式的核心,去除了主函數(shù)的等待時間,并使得原本需要等待的時間段可以用于處理其他業(yè)務邏輯,需要的朋友可以參考下2021-12-12解決Spring配置文件中bean的property屬性中的name出錯問題
這篇文章主要介紹了解決Spring配置文件中bean的property屬性中的name出錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07