1

How to add JLabel with a transparent background on top of vlcj MediaPlayer?

I put source below, but it is not working as it should, because JLabel doesn't have transparent background set.

import com.sun.jna.NativeLibrary;
import javax.swing.*;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

class VideoExample extends JFrame {

    private EmbeddedMediaPlayerComponent mediaPlayerComponent;

    public VideoExample(String path) {
        super("Простой видео плеер");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(640, 480);

        //Create JLayeredPane 
        JLayeredPane mainLayer = new JLayeredPane();
        mainLayer.setSize(640,480);

        //Create MediaPlayer on background
        mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
        mediaPlayerComponent.setLocation(0, 0);
        mediaPlayerComponent.setSize(mainLayer.getSize());

        //Create MediaPlayer on foreground
        JLabel label = new JLabel("LABEL", JLabel.CENTER);
        label.setBounds(100, 100, 200, 100);
        label.setOpaque(false);

        mainLayer.add(mediaPlayerComponent, JLayeredPane.DEFAULT_LAYER); //add mediaPlayer in DEFAULT_LAYER
        mainLayer.add(label, JLayeredPane.PALETTE_LAYER); //add label in PALETTE_LAYER
        add(mainLayer); // add JLayeredPane in JFrame 

        setVisible(true);
        mediaPlayerComponent.getMediaPlayer().playMedia(path);
    }

    public static void main(String[] args) {
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), System.getProperty("user.dir") + "/lib/vlc64");
        final String mrl = "D:\\Candlelight_QT.mov";
        new VideoExample(mrl);
    }
}

This is the result:

The result http://vitamin.vipdesign.com.ua/1n7tJ.jpg

1 Answers1

2

You need to take the time to read the vlcj wiki

You can not simply add a lightweight transparent JComponent on top of the heavyweight video surface Canvas component - it will not work.

Which is what I suspected from the earlier post on the same subject

Check out Overlaying Graphics for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366