Search code examples
javaswingvlcj

Text Overlay on JPanel


I have written a media player using VLCj; And I am trying to replicate VLC player, so that when the space key is pressed, the word "Play" will briefly appear on the screen. Is this possible?

How would I go about showings this temporary overlay?


Solution

  • One way to do this with vlcj is to use the "marquee".

    The marquee is provided by native LibVLC library functions, wrapped by vlcj.

    First:

    import static uk.co.caprica.vlcj.player.Marquee.marquee;
    

    Then in your mouse click listener:

    marquee()
        .text("Play")
        .location(x, y)
        .position(libvlc_marquee_position_e.bottom)
        .opacity(0.7f)
        .colour(Color.white)
        .timeout(5000)
        .size(20)
        .apply(mediaPlayer);
    

    This is a "builder" style of API, there is another API with individual methods for the marquee, e.g.:

    mediaPlayer.setMarqueeText("Play");
    mediaPlayer.setMarqueeSize(60);
    mediaPlayer.setMarqueeOpacity(70);
    mediaPlayer.setMarqueeColour(Color.green);
    mediaPlayer.setMarqueeTimeout(3000);
    mediaPlayer.setMarqueeLocation(300, 400);
    mediaPlayer.enableMarquee(true)
    

    All of this is documented in the vlcj Javadoc:

    http://caprica.github.io/vlcj/javadoc/3.0.0/uk/co/caprica/vlcj/player/Marquee.html http://caprica.github.io/vlcj/javadoc/3.0.0/uk/co/caprica/vlcj/player/MediaPlayer.html

    There are other ways...

    You can try overlaying an AWT Label with absolute positioning on top of the video, this will work but the label will not have a transparent background.

    You can use the so-called "direct" rendering media player (where you render the video yourself) and then you can paint your own graphics on top of the video, or use a Swing JLabel. In this case you can use transparency.

    You could even overlay a transparent top-level window on top of your video window and paint/put your label in that window.

    All of these approaches are demonstrated in the various examples in the vlcj test sources. There are test examples for marquee, and lightweight and heavyweight overlays.

    But using the marquee is the simplest and therefore recommended way.