编程知识 cdmana.com

The realization of snowflake falling effect in Java

 

  1. Chapter one : Analysis of program ideas
    1. 1. Thought analysis

The program adopts the idea of object-oriented , Including the main program , Panels and snowflakes .

The main program is used to generate windows , Storage panel, etc .

Panel class is used to store background pictures , Store the generated snowflake object , Time to refresh the position of snow , Form the effect of snowflakes falling .

The snowflake class is used to generate snowflake objects , Provide a way for snowflakes to move .

  1. Chapter two : Program code implementation
    1. 1. preparation

open idea development tool , hold bg.png、bg2.png、bg3.png and snow.png Copy the picture to src Under the table of contents .

establish cn.tx.utils and cn.tx.domain Package structure of .

hold FrameUtils.java copy to utils In bag .

hold XueHua.java and MyJpanel.java copy to domain In bag .

establish cn.tx.main package , establish MyFrame class , Start coding .

  1. 2. Code implementation

Write main program , It's mainly about generating windows 、 Panel and set background image .

package cn.tx.main;
​
import cn.tx.domain.MyJpanel;
import cn.tx.utils.FrameUtils;
​
import javax.swing.*;
​
/**
 *  author : Mr. Sakura 
 */
public class Demo1 {
    
    public static void main(String[] args) {
        //  Create window objects 
        JFrame frame = new JFrame(" I love your ");
        //  Initialize size and display location 
        FrameUtils.init(frame,1000,700);
        //  Create panel objects 
        MyJpanel jpanel = new MyJpanel(frame);
        //  Toggle background picture 
        String [] bgs = {"bg.png","bg2.png","bg3.png"};
        //  Set the background image 
        jpanel.changebg(jpanel,bgs);
        //  add to 
        frame.add(jpanel);
        //  Start thread , Perform animation effects 
        new Thread(jpanel).start();
        //  Start a new thread , Play background music 
        new Thread(new MyMusic()).start();
        //  Show 
        frame.setVisible(true);
    }
​
}

 

  1. 3. You can add background music

hold xxx.mp3 Copy the background music to src Under the table of contents

Import in the project jl1.0.jar( To play music jar package )

hold MusicUtils.java Copy the tool class to cn.tx.utils In bag

Write a class to play music ( It's also a way to use threads )

package cn.tx.main;
​
import cn.tx.utils.MusicUtils;
​
/**
 *  author : Mr. Sakura 
 */
public class MyMusic implements Runnable {
    
    @Override
    public void run() {
        //  Play music 
        MusicUtils.play(MyMusic.class.getResourceAsStream("/qt.mp3"));
    }
​
}

  Modify the main function , Start thread , Add code to play background music

 

package cn.tx.main;
​
import cn.tx.domain.MyJpanel;
import cn.tx.utils.FrameUtils;
​
import javax.swing.*;
​
/**
 *  author : Mr. Sakura 
 */
public class Demo1 {
​
    public static void main(String[] args) {
        //  Create window objects 
        JFrame frame = new JFrame(" Liao sister ");
        //  Initialize size and display location 
        FrameUtils.init(frame,1000,700);
        //  Create panel objects 
        MyJpanel jpanel = new MyJpanel(frame);
        //  add to 
        frame.add(jpanel);
        //  Start thread , Perform animation effects 
        new Thread(jpanel).start();
        //  Start a new thread , Play background music 
        new Thread(new MyMusic()).start();
        //  Show 
        frame.setVisible(true);
    }
​
}

 

Based on my many years of working experience and learning experience , Recorded a new set of Java Focus on video tutorials , If you are learning now Java, At the beginning of learning Java There is no systematic learning course in the process , You can add groups 654631948 Collect the learning materials , Interview questions , Development tools, etc , There are senior people in the group java The teacher answers questions , Every day, there will also be live lectures on the basic part and architecture , You can also add my wechat renlliang2013 In depth communication , As long as I really want to learn Java People welcome .

 

java Basic course :https://ke.qq.com/course/149432?tuin=57912c43


Java Distributed Internet Architecture / Microservices / High performance /springboot/springcloud:

https://ke.qq.com/course/179440?tuin=57912c43

版权声明
本文为[renlianggee]所创,转载请带上原文链接,感谢

Scroll to Top