5

別のスレッドから受け取ったデータに基づいて、パネルに画像を描画したいと考えていました。データとそれに続くピクセル配列はうまく機能すると確信していますが、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 );  
      } 
  } 
}

ありがとう

4

2 に答える 2

1

についてちょっとメモrepaint()repaint()画面の再描画をスケジュールしますが、私の経験では常にすぐに実行されるとは限りません。最善の解決策は、自分自身に直接電話することpaint()です。

Graphics g;
g = getGraphics();
paint(g);

これを新しい関数として、すぐに描画したいときにコードで呼び出すようにしました。また、これは画面上の以前のグラフィックを消去しないため、手動で行う必要があります。

于 2012-10-08T01:14:59.437 に答える
0

repaint();してからvalidate();、アプレットで試してください( PicturePlaza)。

于 2012-06-20T12:37:47.167 に答える