Search code examples
javaswingjpanelrepaintjinternalframe

JInternalFrame camera image repaint issue


I use JInternalFrame to view a camera image, I get an image but it flashes/blinks very frequently, so I can barely see the image. I extend JFrame instead of JInternalFrame, everything works perfectly fine. Maybe JInternalFrame works differently to JFrame but I can't figure out the issue with the update.

public class CameraView extends JInternalFrame{

  private JPanel contentPane;

  VideoCap videoCapture = new VideoCap();

public CameraView(){
    setSize(400, 400);

    setLocation(100, 100);

    contentPane = new JPanel();
    setContentPane(contentPane);
    contentPane.setLayout(null);
    new MyThread().start();
}

    @Override
    public void paint(Graphics g){
        g = contentPane.getGraphics();
        g.drawImage(videoCapture.getOneFrame(), 0, 0, this);
    }


    class MyThread extends Thread{
        @Override
        public void run() {
            for (;;){
                repaint();
                try { Thread.sleep(30);
                } catch (InterruptedException e) {    }
            }  
        } 
    }

}


Solution

  • You have to remove

    g = contentPane.getGraphics();
    

    and

    contentPane.setLayout(null);