java Realization of Tik Tok Code Dance Source Code

  • 2021-07-16 02:36:16
  • OfStack

In this paper, we share the source code of java to realize Douyin code dance for your reference. The specific contents are as follows

Client.java


package com.dance;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
/**
 * //gif Picture path, it is recommended to use pixels in 400*400 Within 
 * //gif Image generation path, there is no integration here AnimatedGifEncoder Bag 
 * // So what is generated is 1 Some jpg Pictures, you need to use other tools to make the whole gif
 * // If there are conditions for further improvement, generate it directly 1 A gif Documents 
* @ClassName: Client 
* @Description: TODO( Used here 1 Sentences describe the function of this class ) 
* @author baitp 
* @date 2018 Year 12 Month 27 Day  
*
 */
public class Client {

 public static String input = "2.gif"; 
 public static String output = "D://charGif//"; 
 public static void main(String[] args) {
 EventQueue.invokeLater(new Runnable() {
 
 @Override
 public void run() {
 MainFrame mainFrame = new MainFrame();
 // Create and add a menu bar 
  JMenuBar menuBar = new JMenuBar();
  JMenu menuFile = new JMenu("GIF Picture ");
  menuBar.add(menuFile);
  JMenuItem itemSave = new JMenuItem(" Upload ");
//  itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); 
  menuFile.add(itemSave);
  mainFrame.setJMenuBar(menuBar);
 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 mainFrame.setVisible(true);
 itemSave.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 JFileChooser fd = new JFileChooser();
 //fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 fd.showOpenDialog(null);
 fd.setFileSelectionMode(0);
 File f = fd.getSelectedFile();
 String url =Client.class.getClassLoader().getResource("").getFile()+"image/"+f.getName();
 File outfile = new File(url);
 try {
 FileInputStream fi = new FileInputStream(f);
 FileOutputStream out =new FileOutputStream(outfile);
 // Create a moving tool 
   byte datas[] = new byte[1024*8];
   // Creation length 
   int len = 0;
   // Loop to read data 
   while((len = fi.read(datas))!=-1){
   out.write(datas,0,len);
   }
   Client.input=f.getName();
   //3. Release resources 
   fi.close();
   out.close();
 } catch (Exception e1) {
 e1.printStackTrace();
 }
 }
 });
 }
 });
 }
}

@SuppressWarnings("serial")
class MainFrame extends JFrame {
 private int x;
 private int y;
 public static int WIDTH = 0;
 public static int HEIGHT = 0;
 {
 Toolkit kit = Toolkit.getDefaultToolkit();
 int screen_width = kit.getScreenSize().width;
 int screen_height = kit.getScreenSize().height;
 x = (screen_width-WIDTH)/2;
 y = (screen_height-HEIGHT)/2;
 }
 public MainFrame() {
 setTitle("intlstar code dance 666");
 initBounds();
 createGIF();
 setBounds(x, y, WIDTH, HEIGHT);
 setResizable(false);
 setIconImage(null);
 
 JPanel panel = new GamePanel();
 Container container = getContentPane();
 container.add(panel);
 }
 // Output the character picture to the specified directory 
 public void createGIF() {
 BufferedImage[] charImgs = ImgToCharacter.getCharImgs();
 try {
 for (int i=0; i<charImgs.length; i++) {
 File file = new File(Client.class.getClassLoader().getResource("").getFile()+"chargif/"+i+".jpg");
 ImageIO.write(charImgs[i], "jpg", file);
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 System.out.println(" Picture output is complete! ");
 }
 public void initBounds() {
 ImgToCharacter.readGiF();
 ImgToCharacter.draw();
 BufferedImage[] charImgs = ImgToCharacter.getCharImgs();
 int max_width = 0;
 int max_height = 0;
 for (BufferedImage img : charImgs) {
 if(img.getWidth() > max_width)
 max_width = img.getWidth();
 if(img.getHeight() > max_height)
 max_height = img.getHeight();
 }
 WIDTH = max_width;
 HEIGHT = max_height;
 }
}

GamePanel.java


package com.dance;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GamePanel extends JPanel{

 private int fps = 6;
 private BufferedImage[] charImgs;
 int count = 0;
 private BufferedImage background = null;
 public GamePanel() {
 charImgs = ImgToCharacter.getCharImgs();
 initBackgroudImg();
 Thread t = new MyThread();
 t.start();
 }
 public void initBackgroudImg() {
 background = new BufferedImage(MainFrame.WIDTH, MainFrame.HEIGHT, BufferedImage.TYPE_INT_RGB);
 Graphics gb = background.getGraphics();
 gb.setColor(Color.white);
 gb.fillRect(0, 0, background.getWidth(), background.getHeight());
 gb.dispose();
 }
 @Override
 public void paint(Graphics g) {
 super.paint(g);
 int size = charImgs.length;
 int index = count % size;
 BufferedImage img = charImgs[index];
 int w = img.getWidth();
 int h = img.getHeight();
// int x = (MainFrame.WIDTH - w)/2;
// int y = (MainFrame.HEIGHT - h)/2;
 // White background 
 g.setColor(Color.white);
 g.fillRect(0, 0, MainFrame.WIDTH, MainFrame.HEIGHT);
 g.setColor(Color.black);
 // 3/4 To show the scale, consider that some gif The picture is too large, so it is reduced to 1 Scale to display in window 
 g.drawImage(img, 0, MainFrame.HEIGHT - h,w*3/4, h*3/4, null);
 count++;
 }
 
 class MyThread extends Thread {

 long startTime;
 long endTime;
 long sleepTime;
 long spendTime;
 long period = 1000/fps;
 @Override
 public void run() {
 while(true) {
 startTime = System.currentTimeMillis();
 repaint();
 endTime = System.currentTimeMillis();
 spendTime = endTime - startTime;
 sleepTime = period - spendTime;
 if(sleepTime < 0)
 sleepTime = 2;
 try {
 Thread.sleep(sleepTime);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 
 }
 }
 
 }
}

ImgToCharacter.java


package com.dance;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class ImgToCharacter {

 private static BufferedImage[] realImgs;
 private static BufferedImage[] charImgs;
 private static int size = 4;
 private static String[] chars= {" ",".",":",";","-","~","1","i","o","r","a",
  "2","c","3","b","n","q","k","x","S","X",
  "7","Z","O","8","#","$","%","&","M","B",
  "W","@","@"};
 
 public static void draw() {
 int interval = 16777215 / (chars.length-1) ;
 BufferedImage oneRealImg = null;
 int index = 0;
 while(index < realImgs.length) {
 oneRealImg = realImgs[index];
 int width = oneRealImg.getWidth();
 int height = oneRealImg.getHeight();
 BufferedImage oneCharImg = new BufferedImage(width*3, height*3,BufferedImage.TYPE_INT_RGB);
 Graphics g = oneCharImg.getGraphics();
 g.setColor(Color.WHITE);
 g.fillRect(0, 0, oneCharImg.getWidth(), oneCharImg.getHeight());
 g.setColor(Color.BLACK);
 g.setFont(new Font(" Blackbody ", Font.BOLD, 15));
 
 for(int i= 0 ; i < height ; i+=size){ 
 for(int j = 0 ; j < width; j+=size){ 
  int rgb = getAvgRGB(j, i, oneRealImg);
 int k = rgb/interval; 
 g.drawString(chars[k], 12*j/size, 12*i/size);
 }
 }
 g.dispose();
 charImgs[index] = oneCharImg;
 index++;
 }
 
 }
 public static void readGiF() {
 try {
 System.out.println(ImgToCharacter.class.getClassLoader().getResource("").getFile());
 File f = new File(ImgToCharacter.class.getClassLoader().getResource("").getFile()+"image/"+Client.input);
 String name = f.getName();
 String suffix = name.substring(name.lastIndexOf('.')+1);
 
 Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
 ImageReader reader = iter.next();
 ImageInputStream imageIn;
 imageIn = ImageIO.createImageInputStream(f);
 reader.setInput(imageIn);
 int count = reader.getNumImages(true);
 realImgs = new BufferedImage[count];
 charImgs = new BufferedImage[count];
 for(int i=0; i< count; i++) {
 realImgs[i] = reader.read(i);
 }
 
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 public static BufferedImage[] getCharImgs() {
 return charImgs;
 }
 public static int getAvgRGB(int i, int j, BufferedImage img) {
 int result = 0;
 for(int m=0; m<size; m++) {
 for(int n=0; n<size; n++) {
 if(i+m < img.getWidth() && j+n < img.getHeight())
 result += img.getRGB(i+m, j+n);
 }
 }
 return Math.abs(result) / (size*size);
 }

}

Complete source code download address: code dance


Related articles: