Search code examples
javajpanelrepaintjinternalframe

JInternalFrame repainting issue


Below code is my view where I get display a streaming camera. Controller handles sets the image and repaints the JInternalFrame. I have an issue with this because the camera image covers the whole JInternalFrame even the title bar. I tried using JPanel but I had problems getting the image on the JPanel because I extend JInternalFrame.

public class CameraView extends JInternalFrame{

    private BufferedImage image;

    public CameraView(){
        super("Camera", false,false,false, false);

        setSize(500,500);
        setLocation(200,200);

    }

    @Override
    public void paint(Graphics g){
        g.drawImage(image, 0, 0, null);
    }


   public void setImage(BufferedImage image){
       this.image = image;
   }
}

This is what it looks like. No title bar. enter image description here


Solution

  • You're overriding the paint method of the frame. This paint method is what draws the title bar.

    You should create a second class that extends JComponent, override the paint method on that class, and then add an instance of it to your frame.

    Something like:

    public class CameraView extends JInternalFrame{
    
        private BufferedImage image;
    
        public CameraView(){
            super("Camera", false,false,false, false);
    
            setLocation(200,200);
    
            add(new JComponent() {
    
                {
                    setSize(500, 500); // size of the image
                }
    
                @Override
                public void paint(Graphics g){
                    g.drawImage(image, 0, 0, null);
                }
    
            });
    
            pack();
        }
    
       public void setImage(BufferedImage image){
           this.image = image;
       }
    }
    

    You could also have the paint method call super.paint(g); but the way you have it set up now, your image will overlay the title bar.


    Also, calling super("Camera", false,false,false, false); is the same as calling super("Camera"). The defaults are false.