java repaint 没反应_java – repaint()无法正常工作

我想根据从另一个线程收到的数据在我的面板上绘制一个图像.我确信数据和随后的像素数组运行良好,但repaint()永远不会工作.谁能告诉我这里出了什么问题?

import javax.swing.*;

import java.awt.*;

import java.awt.image.*;

/** Create an image from a pixel array. **/

public class PicturePlaza extends JApplet

{

ImagePanel fImagePanel;

ReadCom readComPort;

Thread readPortThread;

public void init () {

// initiate the read port thread so that it can receive data

readComPort = new ReadCom();

readPortThread = new Thread(readComPort,"ReadCom");

readPortThread.start();

Container content_pane = getContentPane ();

fImagePanel = new ImagePanel ();

content_pane.add (fImagePanel);

}

// Tell the panel to create and display the image, if pixel data is ready.

public void start () {

while(true){

if(readComPort.newPic){

fImagePanel.go();

}

try {

Thread.sleep(4000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/** Create an image from a pixel array. **/

class ImagePanel extends JPanel{

Image fImage;

int fWidth = ReadCom.row, fHeight = ReadCom.col;

void go() {

//update the image if newPic flag is set to true

fImage = createImage (new MemoryImageSource (fWidth, fHeight, ReadCom.fpixel, 0, fWidth));

repaint();

readComPort.newPic = false; //disable the flag, indicating the image pixel has been used

}

/** Paint the image on the panel. **/

public void paintComponent (Graphics g) {

super.paintComponent (g);

g.drawImage (fImage, 0, 0, this );

}

}

}

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值