java中的jFrame GetGraphics在绘制图像时为空

2022-01-24 00:00:00 image graphics java swing jframe

在将图像绘制到 jframe 时出现空异常错误.我调试代码并检查图像和框架不为空,但在将图像绘制到框架时仍然抛出 NULL 异常.

I'm getting null exception error while drawing image to jframe. i debug the code and check the image and frame is not null but still it is throwing NULL exception while drawing image to frame.

请看:

public void run(){
    try{

        ObjectInputStream objVideoIn = new ObjectInputStream(serVideoIn);
        byte[] imgbytes=null;
        ByteArrayInputStream barrin=null;


        JFrame jf = new JFrame();
        Graphics ga=jf.getGraphics(); //Getting null exception 
                //Thread.sleep(10000);
        jf.setVisible(true);
        jf.setSize(400, 400);


        while(true){
                    int index=0;
                    //Thread.sleep(300);
                    int size= (int)objVideoIn.readObject();
                    imgbytes = new byte[size];
                    barrin = new ByteArrayInputStream(imgbytes);
                    System.out.println("image size" + size);
                    //Thread.sleep(200);
                    while(index<size)
                    {
                        System.out.println("reading image");
                        int bytesread = objVideoIn.read(imgbytes, index, size-index);
                        if(bytesread<0){
                            System.out.println("error in receiving bytes yar");
                        }
                        index+=bytesread;
                    }
                    //barrin.read(imgbytes, 0, imgbytes.length);
                    barrin = new ByteArrayInputStream(imgbytes);

                    buffImg = ImageIO.read(barrin);

                        if(buffImg==null)
                        {
                            System.out.println("null received");
                        }
                        else {
                            System.out.println("image received");

                        **ga.drawImage(buffImg, 0, 0, null);**


                        }
                    }

            }
    }
    catch(Exception ex)
    {
        System.out.println("error reading video" +ex.getMessage());
    }

}

推荐答案

NPE 很可能来自这里:

The NPE is likely coming from here:

Graphics ga=jf.getGraphics();

根据 docs:

为此组件创建图形上下文.此方法将返回如果此组件当前不可显示,则为 null.

Creates a graphics context for this component. This method will return null if this component is currently not displayable.

1) 不要使用 Component#getGraphics 作为其不好的做法/不持久,除非组件可见,否则将返回 null.

1) Dont use Component#getGraphics as its bad pratice/not persistent and will return null unless component is visible.

2) 而是使用 JPanel 并覆盖 paintComponent(Graphics g) 不要忘记调用 super.paintComponent(g); 作为第一次调用在重写的 paintComponent 中.

2) Rather use JPanel and override paintComponent(Graphics g) dont forget to call super.paintComponent(g); as first call in overriden paintComponent.

3) 覆盖 getPreferredSize() 并返回正确的 Dimension 以适应正在绘制的图像.

3) Override getPreferredSize() and return correct Dimensions to fit image being drawn.

4) 将 JPanel 添加到框架中以使图像当然可见.

4) add JPanel to the frame for image to be visible of course.

或者您也可以使用 JLabel,它只需要一个 setIcon(..) 调用并添加到 JFrame.

Alternatively You could also use a JLabel which would require nothing more than a setIcon(..) call and be added added to JFrame.

这是我的一些例子:

使用JPanel:

  • 从 Java 代码中加载图像C盘

JFrame 的图像绘图作品,但不是JPanel

如何在一个框架?

将 JPanel 转换为JScrollPane 中的图像

使用JLabel:

  • 图像大小调整和显示在 JPanel 或 JLabel 中而不损失质量

相关文章