【问题标题】:How to put transparent JPanel over an opaque JPanel?如何将透明的 JPanel 放在不透明的 JPanel 上?
【发布时间】:2017-06-14 21:07:16
【问题描述】:

我已使用com.github.sarxos.webcam 将网络摄像头添加到我的软件中。它有一个名为WebcamPanelJPanel,并且有预定义的网络摄像头尺寸,而我需要自定义尺寸的图片。我设法在640 x 480 裁剪了从网络摄像头拍摄的图像。我想在WebcamPanel 上放置一个红色矩形,以表明这部分图像将被保存。

public class CardPanel {
    Dimension panelDim = new Dimension(640, 480);

    public Cardpanel(){
        //....Button Defined earlier

        btnTakePhoto.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                webcameFrame();
            }
        });
    }

    private void webcamFrame(){

        imageFrame = new JFrame("Photo Capture");
        // Did some calculations to put window at center
        imageFrame.setBounds(screenSize.width / 2 - frameWidth / 2, screenSize.height / 2 - frameHeight / 2, frameWidth,
            frameHeight);
        imageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        imageFrame.setContentPane(contentPane);

        JPanel webcamWindow = new JPanel();

        RedHighlighter redHighlighter = new RedHighlighter();

        Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(WebcamResolution.VGA.getSize());

        webcamPanel = new WebcamPanel(webcam);
        webcamPanel.setFillArea(true);
        webcamPanel.setMirrored(false);
        webcamPanel.setPreferredSize(panelDim);

        webcamWindow.add(webcamPanel);
        webcamWindow.add(redHighlighter);

        hBox.add(webcamWindow);
   }

   // Sub Class just for drawing the rectangle
   public class RedHighlighter extends JPanel{

        public RedHighlighter() {
            // If you delete the following line, nothing will appear
            setPreferredSize(new Dimension(400, 400));
        }

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.RED);
            g.drawRect(100, 100, 200, 200);
        }
    }
}

我使用了JLayeredPanes,但无论你做什么,它都会覆盖整个尺寸,并且一次只显示一个项目。

覆盖paint 方法帮助我绘制了矩形,但它在侧面而不是在顶部。

如您所见,矩形已将WebcamPanel 推向左侧。我希望webcamPanel 保持在它的位置,而它顶部的矩形在中心。请提出解决此问题的有效方法。谢谢!

【问题讨论】:

标签: java swing graphics jpanel awt


【解决方案1】:

由于您使用的布局管理器,一个 JPanel 被推倒。如果您希望一个 JPanel 覆盖另一个 JPanel,则需要考虑使用 JLayeredPane,视频图像位于较低级别,可能是 JLayeredPane.DEFAULT 层,而绘图 JPanel 位于其上方。

其他选项和问题:

  • 通过在paintComponent 方法和绘图中显示图像(在显示图像之后的代码行中),您可能会在显示图像的同一个JPanel 中进行绘制。
  • 研究使用 JLayer 作为在图像上添加绘图“装饰”的一种方式。
  • 总是覆盖paintComponent,paint
  • 始终在您的覆盖中调用 super 的绘画方法。

【讨论】:

  • WebcamPanel 来自 github 二进制文件,恐怕我无法编辑它。我尝试了JLayeredPane 的首选大小,但即使在setOpaque(false) 看不到网络摄像头之后,它也会调整大小以覆盖整个区域。
  • @MirwiseKhan:没有您当前的相关代码,很难猜测您可能遇到什么问题。请编辑您的问题。
  • @HovercraftFulllOfEels 代码更新
  • @MirwiseKhan 您不需要更改源代码,只需通过从WebcamPanel 扩展并覆盖其paintComponent 方法来创建自定义类
  • 是的,正如@MadProgrammer 所说。但是,如果您这样做(并且再次),请绝对确保在您自己的覆盖中调用 super.paintComponent(g) 方法。
【解决方案2】:

成功了!

public class MyWebcamPanel extends WebcamPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 2808353446021354508L;

    public MyWebcamPanel(Webcam webcam) {
        super(webcam);
    }

    @Override
    protected void paintComponent(Graphics g) {
        int x = 180;
        int y = 87;
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.drawRect(x, y, 640-2*x, 480-2*y);

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 2012-12-13
    • 2023-03-25
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多