在绘制图像时,jFrame GetGraphics在Java中为null [英] jFrame GetGraphics is null in java While Drawing image
问题描述
在将图像绘制到jframe时,我得到null异常错误。
i调试代码并检查图像和帧是否为空,但仍然在绘制图像到帧时抛出NULL异常。
请看一看: / p>
public void run(){
try {
ObjectInputStream objVideoIn = new ObjectInputStream(serVideoIn );
byte [] imgbytes = null;
ByteArrayInputStream barrin = null;
JFrame jf = new JFrame();
图形ga = jf.getGraphics(); //获取null异常
//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(接收字节时发生错误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很可能来自这里:
图形ga = jf.getGraphics();
按 docs :
为这个组件创建一个图形上下文。如果此组件目前无法显示,则此方法将返回
null。
1)不要使用组件#getGraphics
作为其不良实践/不持久,并且将返回 null
,除非组件可见。
2)而是使用 JPanel
并覆盖 paintComponent(Graphics g)
不要忘记调用 super.paintComponent(g);
作为overriden paintComponent
中的第一个调用。
$ b <3>覆盖 getPreferredSize()
并返回正确的维
s以适合正在绘制的图像。
4)将 JPanel
添加到框架中, 。
或者你也可以使用 JLabel
,它只需要一个 setIcon (..)
调用并添加到 JFrame
。
这里有一些我的例子:
使用 JPanel
:
使用 JLabel
:
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.
Please have a look :
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());
}
}
The NPE is likely coming from here:
Graphics ga=jf.getGraphics();
as per docs:
Creates a graphics context for this component. This method will return null if this component is currently not displayable.
1) Dont use Component#getGraphics
as its bad pratice/not persistent and will return null
unless component is visible.
2) Rather use JPanel
and override paintComponent(Graphics g)
dont forget to call super.paintComponent(g);
as first call in overriden paintComponent
.
3) Override getPreferredSize()
and return correct Dimension
s to fit image being drawn.
4) add JPanel
to the frame for image to be visible of course.
Alternatively You could also use a JLabel
which would require nothing more than a setIcon(..)
call and be added added to JFrame
.
Here are some of my examples:
Using JPanel
:
Using JLabel
:
这篇关于在绘制图像时,jFrame GetGraphics在Java中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!