设置宽度为最大值时,JPanel重新绘制问题

时间:2014-03-17 01:30:43

标签: java swing jpanel repaint javacv

我正在测试JavaCV以使用我的网络摄像头中的图像更新JPanel。它运行良好,除非我使用JFrame的setSize将宽度设置为我的分辨率(我的分辨率为1366 x 768)。

当我这样做(设置宽度为1366)时,JPanel的重绘不能正常工作并且图像冻结。当我将宽度设置为1365或1367时,效果非常好。

为了测试,我创建了一个按钮,每次点击都会增加8到宽度。我以1350的宽度开始表单,当宽度更改为1366时(点击2次后),重绘停止。

有人知道会发生什么吗?在我的代码下面。

package com.pictap;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;

import org.apache.log4j.Logger;

import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class PicTap {

    private static final Logger logger = Logger.getLogger(PicTap.class);
    private static JFrame form;
    private static ImagePanel panel;
    private static FrameGrabber grabber;
    private static JTextPane status;

    private static Integer counter = 0;

    public static void main(String[] args) {

        //Declare FrameGrabber to import output from webcam
        grabber = new OpenCVFrameGrabber("");  

        try{

            IplImage img;
            BufferedImage bi;

            // Show GUI
            form = new JFrame("PicTap");
            form.setSize(1350, 768);
            form.setLayout(new BorderLayout());
            form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            panel = new ImagePanel();
            panel.setDoubleBuffered(true);
            panel.setBackground(Color.BLACK);
            form.add(panel, BorderLayout.CENTER);
            panel.setVisible(true);

            JButton increase = new JButton("Aumentar");
            form.add(increase,BorderLayout.NORTH);
            increase.setVisible(true);
            increase.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    form.setSize(form.getSize().width + 8, 768);
                }
            });

            JButton close = new JButton("Sair");
            form.add(close,BorderLayout.SOUTH);
            close.setVisible(true);
            close.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // Stop the processor doing the movie capture first
                    try {
                        grabber.stop();
                    } catch (Exception e3) {
                        logger.error("Exception 3" + e3.getMessage());
                        e3.printStackTrace();
                    }
                    form.setVisible(false);
                    System.exit(0);

                }
            });

            status = new JTextPane();
            status.setText("Status");
            form.add(status, BorderLayout.SOUTH);
            status.setVisible(true);

            form.setAlwaysOnTop(true);
            form.setUndecorated(true);
            //form.setLocationRelativeTo(form);
            form.setVisible(true);

            // Start grabber to capture video
            grabber.start();      

            while (true) {

                logger.debug("Counter loop: " + counter++);

                // insert grabed video fram to IplImage img
                img = grabber.grab();
                //IplImage grayImage = IplImage.create(width, height, IPL_DEPTH_8U, 1);
                bi = img.getBufferedImage();
                panel.updateImage(bi, status);
                panel.invalidate();
                panel.repaint();
                form.repaint();
                form.invalidate();
                form.repaint();

            }
        } catch (Exception e1) {
            logger.error("Exception 1" + e1.getMessage());
            e1.printStackTrace();
        }
    }
}

class ImagePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(ImagePanel.class);

    private static Integer counter = 0;

    BufferedImage localImage;

    public ImagePanel(){
    }

    public void updateImage(BufferedImage image, JTextPane status){
        localImage = image;
    }

    public void paintComponent(Graphics g){
        logger.debug("Counter paint: " + counter++);
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(localImage, null, 0, 0);
    }
}

0 个答案:

没有答案