In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize romantic meteor confession in Java". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve romantic meteor confession in Java"!
Introduction
The functions realized in this paper are:
1. Play music
2. Customize the number of meteors, flight speed, halo size, meteor size
3. Custom confession discourse.
The knowledge points used are:
GUI:java implements form, Swing. In fact, JAVA Swing's GUI is no longer used in enterprises, mainly used by some schools and training institutions to teach students to write some games, small projects and practice.
Multithreading: let cpu handle multiple tasks at the same time (this article involves music, slow text appearance, meteor lines moving)
Effect picture:
Music (actually, you can do without music, some people don't like it):
The core code 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 () {/ / play music System.out.println ("start playing"); / / represents the music file File f = new File ("nv.mp3") / / third-party jar package Player class try {Player p = new Player (new FileInputStream (f)); / / Parameter: file input stream object / / p.play ();} catch (FileNotFoundException | JavaLayerException e) {e.printStackTrace ();}
Implementation class:
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; / / (~) the number of meteors final int SLEEP = 2; / / the speed of meteors flying (the higher the number, the slower the speed) final int COLORLV = 1 / / (~) color levels (halo size can be changed) final int SIZE = 3; / / (~) meteor size private MyPanel panel; public MeteorFly () {panel = new MyPanel (); this.setTitle ("LOVE"); this.getContentPane () .add (panel); this.setSize (AppletWidth, AppletHeight); / / create form this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) This.setVisible (true);} public static void main (String [] args) {new Thread () {@ Override public void run () {/ / declare a File object File mp3 = new File ("nv.mp3"); / / create an input stream FileInputStream fileInputStream = null Try {fileInputStream = new FileInputStream (mp3); / / create a buffered stream BufferedInputStream bufferedInputStream = new BufferedInputStream (fileInputStream); / / create a player object and pass the buffered stream of the file into Player player = new Player (fileInputStream) / / call the playback method to play 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", 0getScreenSize 28); public MyPanel () {/ / setBackground (Color.black); / / form initialization 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 ("Song style", Font.BOLD, 50); g.drawString (str1.substring (0Jing posthum1), 260700);} @ 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 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 ); //随机生成流星出现位置 if (rand >35) {x = (int) (Math.random () * 600); y = 0;} else {y = (int) (Math.random () * 150); x = 0;} mx = r.nextInt (2) + 2 / / randomly generate falling velocity and angle my = 1 Color = new Color (/ / Random Color (new Double (Math.random () * 128)). IntValue () + 128, (new Double (Math.random () * 128). IntValue () + 128, (new Double (Math.random () * 128)) .intValue () + 128) }}}
The Player class here needs to guide the package itself. I put the package in this link:
Link extraction code: v22q
Matters needing attention
Some people may have this problem during the package guide process:
It should actually be open:
How to guide a package:
First, copy and paste the package under the project package.
Then enter: File-> Project Structure
Then click the Libraries, the + sign, and the Java
Find the location of the file you want to import, and then click OK all the time.
At this point, I believe you have a deeper understanding of "how to achieve romantic meteor confession in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.