java实现抖音代码舞源码
本文实例为大家分享了java实现抖音代码舞源码,供大家参考,具体内容如下
Client.java
packagecom.dance; importjava.awt.Container; importjava.awt.EventQueue; importjava.awt.Toolkit; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.IOException; importjavax.imageio.ImageIO; importjavax.swing.JFileChooser; importjavax.swing.JFrame; importjavax.swing.JMenu; importjavax.swing.JMenuBar; importjavax.swing.JMenuItem; importjavax.swing.JPanel; /** *//gif图片路径,推荐使用像素在400*400以内的 *//gif图片生成路径,这里没有整合AnimatedGifEncoder包 *//所以生成的是一些jpg图片,需要自己借助其他工具整个成gif *//如果有条件可以继续改进,直接生成一个gif文件 *@ClassName:Client *@Description:TODO(这里用一句话描述这个类的作用) *@authorbaitp *@date2018年12月27日 * */ publicclassClient{ publicstaticStringinput="2.gif"; publicstaticStringoutput="D://charGif//"; publicstaticvoidmain(String[]args){ EventQueue.invokeLater(newRunnable(){ @Override publicvoidrun(){ MainFramemainFrame=newMainFrame(); //创建并添加菜单栏 JMenuBarmenuBar=newJMenuBar(); JMenumenuFile=newJMenu("GIF图片"); menuBar.add(menuFile); JMenuItemitemSave=newJMenuItem("上传"); //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(newActionListener(){ @Override publicvoidactionPerformed(ActionEvente){ JFileChooserfd=newJFileChooser(); //fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fd.showOpenDialog(null); fd.setFileSelectionMode(0); Filef=fd.getSelectedFile(); Stringurl=Client.class.getClassLoader().getResource("").getFile()+"image/"+f.getName(); Fileoutfile=newFile(url); try{ FileInputStreamfi=newFileInputStream(f); FileOutputStreamout=newFileOutputStream(outfile); //创建搬运工具 bytedatas[]=newbyte[1024*8]; //创建长度 intlen=0; //循环读取数据 while((len=fi.read(datas))!=-1){ out.write(datas,0,len); } Client.input=f.getName(); //3.释放资源 fi.close(); out.close(); }catch(Exceptione1){ e1.printStackTrace(); } } }); } }); } } @SuppressWarnings("serial") classMainFrameextendsJFrame{ privateintx; privateinty; publicstaticintWIDTH=0; publicstaticintHEIGHT=0; { Toolkitkit=Toolkit.getDefaultToolkit(); intscreen_width=kit.getScreenSize().width; intscreen_height=kit.getScreenSize().height; x=(screen_width-WIDTH)/2; y=(screen_height-HEIGHT)/2; } publicMainFrame(){ setTitle("intlstarcodedance666"); initBounds(); createGIF(); setBounds(x,y,WIDTH,HEIGHT); setResizable(false); setIconImage(null); JPanelpanel=newGamePanel(); Containercontainer=getContentPane(); container.add(panel); } //将字符图片输出到指定目录 publicvoidcreateGIF(){ BufferedImage[]charImgs=ImgToCharacter.getCharImgs(); try{ for(inti=0;imax_width) max_width=img.getWidth(); if(img.getHeight()>max_height) max_height=img.getHeight(); } WIDTH=max_width; HEIGHT=max_height; } }
GamePanel.java
packagecom.dance; importjava.awt.Color; importjava.awt.Graphics; importjava.awt.image.BufferedImage; importjavax.swing.JPanel; @SuppressWarnings("serial") publicclassGamePanelextendsJPanel{ privateintfps=6; privateBufferedImage[]charImgs; intcount=0; privateBufferedImagebackground=null; publicGamePanel(){ charImgs=ImgToCharacter.getCharImgs(); initBackgroudImg(); Threadt=newMyThread(); t.start(); } publicvoidinitBackgroudImg(){ background=newBufferedImage(MainFrame.WIDTH,MainFrame.HEIGHT,BufferedImage.TYPE_INT_RGB); Graphicsgb=background.getGraphics(); gb.setColor(Color.white); gb.fillRect(0,0,background.getWidth(),background.getHeight()); gb.dispose(); } @Override publicvoidpaint(Graphicsg){ super.paint(g); intsize=charImgs.length; intindex=count%size; BufferedImageimg=charImgs[index]; intw=img.getWidth(); inth=img.getHeight(); //intx=(MainFrame.WIDTH-w)/2; //inty=(MainFrame.HEIGHT-h)/2; //背景涂白 g.setColor(Color.white); g.fillRect(0,0,MainFrame.WIDTH,MainFrame.HEIGHT); g.setColor(Color.black); //3/4为显示比例,考虑到有的gif图片太大,所以缩小至一定比例在窗口中显示 g.drawImage(img,0,MainFrame.HEIGHT-h,w*3/4,h*3/4,null); count++; } classMyThreadextendsThread{ longstartTime; longendTime; longsleepTime; longspendTime; longperiod=1000/fps; @Override publicvoidrun(){ 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(InterruptedExceptione){ e.printStackTrace(); } } } } }
ImgToCharacter.java
packagecom.dance; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.IOException; importjava.util.Iterator; importjavax.imageio.ImageIO; importjavax.imageio.ImageReader; importjavax.imageio.stream.ImageInputStream; publicclassImgToCharacter{ privatestaticBufferedImage[]realImgs; privatestaticBufferedImage[]charImgs; privatestaticintsize=4; privatestaticString[]chars={"",".",":",";","-","~","1","i","o","r","a", "2","c","3","b","n","q","k","x","S","X", "7","Z","O","8","#","$","%","&","M","B", "W","@","@"}; publicstaticvoiddraw(){ intinterval=16777215/(chars.length-1); BufferedImageoneRealImg=null; intindex=0; while(indexiter=ImageIO.getImageReadersBySuffix(suffix); ImageReaderreader=iter.next(); ImageInputStreamimageIn; imageIn=ImageIO.createImageInputStream(f); reader.setInput(imageIn); intcount=reader.getNumImages(true); realImgs=newBufferedImage[count]; charImgs=newBufferedImage[count]; for(inti=0;i 完整源码下载地址:代码舞
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。