java- 如何在画布上绘制消息?

huangapple 未分类评论46阅读模式
英文:

java- How to draw message over a Canvas?

问题

我正在使用 VLCJ 创建一个 视频播放器(Video Player)

该播放器使用 Canvas 作为其视频表面。

videoPlayer.newVideoSurface(canvas);

因此,我必须使用 Canvas

我想在 Canvas 上显示消息,比如 "Player Started"。我尝试使用 JLayeredPaneJLabel 来实现这一点。

一个简短的示例代码:

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();
            }
        });
    }
}

但是很快我发现,你不能混合使用重量级的 AWT 组件和轻量级的 Swing 组件。

因此,有人可以告诉我如何在 Canvas 上显示消息吗?

英文:

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

得分: 0

如果您想在嵌入的 vlcj 画布上叠加静态文本,您需要使用 Marquess

或者直接在媒体播放器中使用 marque 选项,可以在这里查看 此处链接

对于除文本以外的叠加层,您可以查看 AbstractJWindowOverlayComponent

或者您可以尝试这种方法,创建一个 JLayeredPane,将您的画布放在第一图层,然后可以在第0图层上放置您的 JComponent,它将以透明背景绘制您的叠加层,您可以覆盖它的 paintComponent() 方法并绘制所需内容。

英文:

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

huangapple
  • 本文由 发表于 2020年7月25日 11:19:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/63083965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定