当前位置:网站首页>Imitate Tengu eating the moon with Avatar

Imitate Tengu eating the moon with Avatar

2022-07-07 04:04:00 InfoQ

Use the avatar to imitate Tengu eating the moon

Today came interest , Use the avatar to imitate the dog eating the moon , So what is a dog eating the moon ?

The so-called heavenly dog is actually an eclipse of the moon , The ancients didn't know this before , So the moon was eaten by the dog , In fact, it is a natural phenomenon , When the sun 、 The earth 、 When the moon is exactly or almost in the same straight line ( The earth is between the sun and the moon ), The light from the sun to the moon will be partially or completely covered by the earth , Produce an eclipse .

This article mainly uses Jpanel, So what is Jpanel Well ?

JPanel  yes  Java Graphical user interface (GUI) tool kit swing Panel container class in , Included in javax.swing  package , Can be added to JFrame In form .JPanel The default layout manager is FlowLayout, It can be nested and combined by itself , Other components can be included in different sub containers (component), Such as JButton、JTextArea、JTextField  etc. , The function is to combine these controls on the form .

This paper mainly uses JPanel Of paint Method , At first glance, this method means painting .

Code display :

import lombok.extern.slf4j.Slf4j;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

/**
 * @Author xiepanpan
 * @Description // Eat the moon
 * @Date 9:05 2021/9/8
 * @Param
 * @return
 **/
@Slf4j
public class XppMoonPanel extends JPanel{
 int x=700;
 public void startRun(){
 new Thread(){
 public void run(){
 while(true){
 // The abscissa of the avatar keeps increasing
 // So close to the moon and away from , Cause the effect of heavenly dog eating the moon
 for (int i=0;i<1024;i++){
 x++;
 if(x>1024){
 x=0;
 }
 }
 try {
 // Let the thread sleep 10 millisecond
 Thread.sleep(10);
 }
 catch (InterruptedException e) {
 log.info(&quot;&quot;,e);
 }
 // Automatically refresh the screen , call paint() Method
 repaint();
 }
 }
 }
 .start();
 }

 /*
 * @Author xiepanpan
 * @Description // Start drawing
 * @Date 9:01 2021/9/8
 * @Param [g]
 * @return void
 **/
 public void paint(Graphics g){
 super.paint(g);
 // Sky Color
 this.setBackground(Color.BLACK);

 // Love stars
 g.setColor(Color.RED);
 for (int i = 0; i < 20; i++) {
 g.drawString(&quot;&quot;, (int) (Math.random() * 1024), (int) (Math.random() * 768));
 }

 // The moon
 g.setColor(Color.yellow);
 g.fillArc(500, 100, 200, 200, 0, 360);

 ImageIcon icon = new ImageIcon(&quot;D:\\1.png&quot;);
 // Set the avatar to cover the moon
 g.drawImage(icon.getImage(),x,100,200,200,this);
 }
}

Start class :

import javax.swing.JFrame;

/**
 * @Author xiepanpan
 * @Description // start-up
 * @Date 9:06 2021/9/8
 * @Param
 * @return
 **/
public class XppMoonFrame {

 public static void main(String[] args) {
 JFrame frame = new JFrame();
 frame.setSize(1024, 768);
 frame.setLocation(100, 100);
 XppMoonPanel panel = new XppMoonPanel();
 frame.add(panel);
 panel.startRun();
 frame.setVisible(true);
 }
}

Let me briefly describe the general idea , It's very simple :

  • Create a new class inheritance Jpanel  Rewrite its paint Method
  • Draw the sky in the method   The moon and stars   Then load a picture   This picture is used to cover the moon
  • Then constantly change the position of the picture ,, So as to achieve the effect of Tengu eating the moon

Finally, I will show you the renderings :

null
The rendering can be said to be quite beautiful , Come and have a try ~, My favorite friends give me a compliment before I go , Your praise is the biggest driving force for me to move forward ~  Hasaki !

️  Thank you for your

If you think this is helpful for you :

  • Welcome to follow me ️, give the thumbs-up , Comment on , forward
  • Focus on
    Panpan small class
    , Push good articles for you regularly , There are also group chat and irregular lottery activities , You can say what you want , Communicate with the great gods , Learning together .
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062132520914.html