Java implementation of snow scene map-Java project practice learning-involving Jframe/Jpanel etc.

Use Java to write a simple snow scene
[Small project suitable for practice]

The effect is as follows:
Insert picture description here

  • The first step is
    to create a project named Demo_snow with idea:
    Insert picture description here
    create a package under src:
    Insert picture description here

Give the package a good name:
Insert picture description here
create two java files in the package:
Insert picture description here
Insert picture description here
Insert picture description here

  • 2. Step 2:
    Let's prepare the resources we need: prepare
    the picture and put it in the created img folder, just copy and paste it directly
    Insert picture description here

  • 3. The third step
    let's analyze how to use java code to realize this snow scene map:

The code of Myframe.java is as follows:

package cn.edu.demo;
import javax.swing.*;

/**
 * @Description:
 * 步骤:
 * 1.GUI 提供用户图形化界面的  Jframe:窗体类
 * 2.在窗体中加入背景  Jpanel:画板类
 * 3.实现雪花的下落
 */
public class Myframe extends JFrame {
    
    
    //使用构造方法,给对象赋初始值
    public Myframe(){
    
    
        //给窗体设置标题
        this.setTitle("雪景图");
        //给窗体设置宽和高
        this.setSize(1920,1080);
        //居中显示
        this.setLocationRelativeTo(null);
        //默认关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //将画板添加到窗体中
        this.add(new Mypanel());
        //显示
        this.setVisible(true);
    }

    public static void main(String[] args) {
    
    
        new Myframe();//创建窗体对象
    }
}

Mypanel.java code is as follows:

package cn.edu.demo;

import javax.swing.*;
import java.awt.*;

/**
 * @Description:画板类
 * 1.画入背景图片
 * 2.画入雪花
 * 3.让雪花下落
 */
public class Mypanel extends JPanel implements Runnable {
    
    
    int[] x = new int[400];
    int[] y = new int[400];
    public Mypanel(){
    
    
        for(int i = 0; i<x.length; i++){
    
    
            x[i] = (int)(Math.random()*1920);
            y[i] = (int)(Math.random()*1080);
        }
        new Thread(this).start();
    }
    /**
     * 绘制的方法
     * @param g 画笔工具
     */
    @Override
    protected void paintComponent(Graphics g) {
    
    
        super.paintComponent(g);
        //1.画入背景图片
        new ImageIcon("img/s2.png").paintIcon(this,g,0,0);
        //2.画入雪花
        g.setColor(Color.white);
        for(int i = 0; i<x.length; i++){
    
    
            g.drawString("*",x[i],y[i]);
        }

    }

    @Override
    public void run() {
    
    
        while (true){
    
    
            for(int i = 0; i<x.length; i++){
    
    
                y[i]++;
                if(y[i] >= 1080){
    
    
                    y[i] = 0;
                }
            }
            repaint();
            try {
    
    
                Thread.sleep(30);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
                //在命令行打印异常信息在程序中出错的位置及原因
            }
        }
    }
}
  • 4. The fourth step is to
    run our project to see the effect.
    Insert picture description here
    If you don’t like this style, just go here and change the background you want.
 new ImageIcon("img/s2.png").paintIcon(this,g,0,0);

Insert picture description here
5. The fifth step

Although this is a very simple small java project, it also involves some knowledge points, let's learn it

在代码中已经描述了每一个java文件的作用,那我们就了解一下用到的知识点b并扩展一下吧:

1. JFrame form class

JFrame() can create an untitled window.
JFrame(String a) can create a window titled a.

public voidsetBounds(int a,int b,int width,int height)

  • Set the initial position of the window to (a, b), which is a pixels from the left of the screen and b pixels from the top of the screen. The width of the window is width and the height is height.

public void setSize(int width,int height)

  • Set the size of the window.

public void setLocation(int x,int y)

  • Set the position of the window, the default position is (0,0).

    public void setVisible(boolean b) Set whether the window is visible, the window is invisible by default.


public voidsetResizable(boolean b)

  • Set whether the window can be resized, the default is resizeable.

public voiddispose()

  • Cancel the current window and release the resources used by the current window.

publicvoid setExtendedState(int state)

  • Set the extended state of the window, where the parameter state takes the following class constants in the JFrame class:

MAXIMIZED_HORIZ (maximized in horizontal direction),
MAXIMIZED_VERT (maximized in vertical direction),
MAXIMIZED_BOTH ( maximized in both horizontal and vertical directions).


publicvoid setDefaultCloseOperation(int operation)

  • It is used to set how the program will process after clicking the close icon in the upper right corner of the form. The parameter operation takes the following int type static constants in the JFrame class. The program makes different treatments according to the value of the parameter operation:

DO_NOTHING_ON_CLOSE (do nothing),
HIDE_ON_CLOSE (hide the current window),
DISPOSE_ON_CLOSE (hide the current window and release other resources occupied by the window), EXIT_ON_CLOSE (end the application where the window is located).


2.JPanel drawing board class
JPanel component definition panel is actually a container component, used to hold various other lightweight components.
Of course, users can also draw graphics with this panel container.

The construction method of JPanel is as follows:

JPanel(): Create a panel with double buffering and FlowLayout.
JPanel (LayoutManager layout): Create a panel with a specified layout manager.

Common methods of JPanel:

void add(Component): Add component.
void add(Component,int): Add a component to the specified position of the index.
void add(Component,Object): Add components according to the specified layout restrictions.
void add(Component,Object,int): Add components to the specified position according to the specified layout manager restrictions.
void remove(Component): Remove the component.
void remove(int): Remove the component at the specified position.
void removeAll(): Remove all components.
void paintComponent(Graphics): Paint the component. ``
void repaint(): Repaint.
void setPreferredSize(Dimension): Set the component size.
Dimension getPreferredSize(): Get the best size.

This small project is over. See you in the next project, follow me, I will make a collection of projects in the category column, let’s learn together

Guess you like

Origin blog.csdn.net/m0_49095721/article/details/109231632