JavaSwing

计算文字长度(判断字符数以及绘制String->Image图像)

判断字符数

//计算字符数
static public int[] count(String str) {
        /*中文字符 */
        int chCharacter = 0;
        /*英文字符 */
        int enCharacter = 0;
        /*空格 */
        int spaceCharacter = 0;
        /*数字 */
        int numberCharacter = 0;
        /*其他字符 */
        int otherCharacter = 0;
        if (null == str || str.equals("")) {
            return null;
        }
        for (int i = 0; i < str.length(); i++) {
            char tmp = str.charAt(i);
            if ((tmp >= 'A' && tmp <= 'Z') || (tmp >= 'a' && tmp <= 'z')) {
                enCharacter ++;
            } else if ((tmp >= '0') && (tmp <= '9')) {
                numberCharacter ++;
            } else if (tmp ==' ') {
                spaceCharacter ++;
            } else if (isChinese(tmp)) {
                chCharacter ++;
            } else {
                otherCharacter ++;
            }
        }
        return new int[]{chCharacter, enCharacter, numberCharacter, spaceCharacter, otherCharacter};
    }
//判断是否为中文字符(中文占两位)
    static public boolean isChinese(char ch) {
        //获取此字符的UniCodeBlock
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
        //  GENERAL_PUNCTUATION 判断中文的“号
        //  CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
        //  HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }

绘制指定字体的String->Image图像

static public BufferedImage stringImage(Color color,Font font,String string){
        int i[] = count(string);
        int width = i[0] * font.getSize() + (i[1] + i[2] + i[3] + i[4] + 1) * font.getSize() / 2;
        BufferedImage test = new BufferedImage(200, 200, BufferedImage.TYPE_4BYTE_ABGR);
        test.getGraphics().setFont(font);
        FontRenderContext context= ((Graphics2D)test.getGraphics()).getFontRenderContext();
        LineMetrics lineMetrics = font.getLineMetrics(string, context);
        BufferedImage bufferedImage=new BufferedImage(width, (int) lineMetrics.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d =(Graphics2D) bufferedImage.getGraphics();
        g2d.setFont(font);
        float offset = 0;
        float y = (font.getSize() + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
        g2d.setColor(color);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.drawString(string,0,y);
        return bufferedImage;
    }

设置窗体全屏

static public void setFullSceen(Window window){
        if (window!=null)((JFrame)window).getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //通过调用GraphicsEnvironment的getDefaultScreenDevice方法获得当前的屏幕设备了
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        // 全屏设置
        gd.setFullScreenWindow(window);
    }

VLCJ

VLC本地库设置

 private void vlcjInit(){
        if (!VlcjInit){
            //NativeDiscovery().discover();函数返回的是一个布尔类型的值,所有可以定义一个布尔类型的值,用来接收,利用控制台打印,是否发现本地库
            found = new NativeDiscovery().discover();
            if (found) System.out.println("已自动发现VLC库。");
            else {
                NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "\\lib\\vlc");
                //打印版本,用来检验是否获得文件
                System.out.println(LibVlc.libvlc_get_version());
            }
            VlcjInit=true;
        }
    }

JavaFX

初始化JavaFX功能(利用JFXPanel)

计数器:

final static protected CountDownLatch latch = new CountDownLatch(1);

初始化方法:

//初始化javafx功能
    private void fxInit(){
        if (!FXInit){
            FXInit=true;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new JFXPanel(); // initializes JavaFX environment
                    latch.countDown();
                }
            });
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("FX功能已经加载。");
        }
    }

利用JavaFX的MusicPlayer

MusicPlayer.java

import cn.korostudio.jsme.listener.CallBack;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.util.ArrayList;
import java.util.Arrays;
public class MusicPlayer {
    protected Media musicMedia;
    protected MediaPlayer musicMediaPlayer = null;
    protected String URI;
    protected boolean playing = false;
    protected ArrayList<CallBack> stopCallBacks = new ArrayList<>();
    protected ArrayList<CallBack> startCallBacks = new ArrayList<>();
    public void setURI(java.net.URI uri) {
        URI = uri.toString();
        musicMedia = new Media(URI);
        musicMediaPlayer = new MediaPlayer(musicMedia);
        musicMediaPlayer.setOnEndOfMedia(new Runnable() {
            @Override
            public void run() {
                playing = false;
                try {
                    for (CallBack callBack : stopCallBacks) {
                        callBack.run();
                    }
                } catch (Exception e) {
                }
            }
        });
        musicMediaPlayer.setOnPlaying(new Runnable() {
            @Override
            public void run() {
                playing = false;
                try {
                    for (CallBack callBack : startCallBacks) {
                        callBack.run();
                    }
                } catch (Exception e) {
                }
            }
        });
    }
    public void setStopCallBacks(CallBack... callBacks) {
        this.stopCallBacks.addAll(Arrays.asList(callBacks));
    }
    public void removeALLStopCallBacls() {
        this.stopCallBacks.removeAll(stopCallBacks);
    }
    public void removeStopCallBacls(CallBack... callBacks) {
        this.stopCallBacks.removeAll(Arrays.asList(callBacks));
    }
    public void setStartCallBacks(CallBack...callBacks) {
        this.startCallBacks.addAll(Arrays.asList(callBacks));
    }
    public void removeStartCallBacks(CallBack...callBacks){
        this.startCallBacks.removeAll(Arrays.asList(callBacks));
    }
    public void play() {
        playing = true;
        musicMediaPlayer.play();
    }
    public boolean isPlaying() {
        return playing;
    }
    public MediaPlayer getMusicMediaPlayer() {
        return musicMediaPlayer;
    }
    public void stop() {
        musicMediaPlayer.stop();
    }
    public void pause() {
        musicMediaPlayer.pause();
    }
}

CallBack.java

public interface CallBack {
     public void run();
}

Q.E.D.


在校大学生一名 熟悉java/cpp
正在摸索JavaFX ing......