java- How to draw message over a Canvas?

Viewed 149

I am creating a Video Player using VLCJ.

The player uses a Canvas as it's video surface.

videoPlayer.newVideoSurface(canvas);

So I am bounded to use Canvas.

I want to show message over the Canvas like, "Player Started". I tried using a JLayeredPane and a JLabel to accomplish this.

A short-demo code:

package canvasexample;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class CanvasExample extends JFrame
{

public JLabel label  = new JLabel("Message");
public Canvas canvas = new MyCanvas();  
    
public CanvasExample()
{
 setSize(500, 500);
 setLocationRelativeTo(null);
 
 createWindow();
 setVisible(true);
}        
    
public void createWindow()
{
 JLayeredPane pane = new JLayeredPane();
 
 JPanel panel1 = new JPanel(new BorderLayout());
 JPanel panel2 = new JPanel(new BorderLayout());

 panel1.setSize(500, 500);
 panel2.setSize(500, 500);
 
 label.setBorder(new LineBorder(Color.BLUE));
 
 panel1.add(canvas);
 panel2.add(label, BorderLayout.NORTH);
 
 pane.add(panel1, 1, 0);
 pane.add(panel2, 2, 0);
 add(pane);
}        

private class MyCanvas extends Canvas  
{  
        public MyCanvas() {  
        setBackground (Color.GRAY);  
        setSize(500, 500);  
     }  
  public void paint(Graphics g)  
  {  
    g.setColor(Color.red);  
    g.fillOval(75, 75, 150, 75);  
  }  
}

public static void main(String[] args) 
{
 SwingUtilities.invokeLater(new Runnable(){
 @Override
 public void run(){
   new CanvasExample();  
 }    
 });
}    
}

But soon I found out that you can't mix heavyweight AWT component and lightweight Swing components.

So, can anyone tell me how can I display a message over a Canvas?

1 Answers

If you are trying to overlay static text on an vlcj embedded canvas you need to use Marquess

Or use the marque options directly in the mediaplayer here

For overlays besides text you can have a look AbstractJWindowOverlayComponent

or I haven't tried this approach u can create an JLayeredPane where you have your canvas at layer 1 and can have your JComponent which will draw your overlays at layer 0 with transparent background you can override it's paintComponent() method and draw whatever you need

Related