java - 为什么要在此代码中调用重绘?

标签 java swing applet repaint jcomponent

如果您将一个 jpanel 实例添加到内容 Pane ,就会调用 paintComponent 方法,对吧?

content_pane.add (fDrawingPanel);

那么为什么要在 run() 方法的第一行调用“repaint”? (它说“绘制加载消息,但它应该已经被绘制了,因为之前调用了 paintComponent,并且一旦 fShow 为真,我们就不会将其设置回来为假,所以我认为这段代码应该被调用一次):

public class MediaTrackApplet extends JApplet
                       implements Runnable
{
  // Need a reference to the panel for the
  // thread loop.
  DrawingPanel fDrawingPanel;

  // Parameters to track the image
  MediaTracker fTracker;
  Image fImg;
  int fImageNum = 0;
  boolean fShow = false;
  String fMessage ="Loading...";

  /** Use a MediaTracker to load an image.**/
  public void init () {
    Container content_pane = getContentPane ();

    // Create an instance of DrawingPanel
    fDrawingPanel = new DrawingPanel (this);

    // Add the DrawingPanel to the contentPane.
    content_pane.add (fDrawingPanel);

    // Get image and monitor its loading.
    fImg = getImage (getCodeBase (), "m20.gif.jpg" );
    fTracker = new MediaTracker (this);

    // Pass the image reference and an ID number.
    fTracker.addImage (fImg, fImageNum);
  } // init

  /** If the image not yet loaded, run the thread
    * so the run() will monitor the image loading.
   **/
  public void start () {
    if (!fTracker.checkID (fImageNum) ) {
        Thread thread = new Thread (this);
        thread.start ();
    } else
        // Unloading/reloading web page can will leave
        // checkID true but fShow will be false.
        fShow = true;
  } // start

  /** Use a thread to wait for the image to load 
    * before painting it.
   **/
  public void run ()  {
    // Paint the loading message
    repaint ();
    // The wait for the image to finish loading
    try {
      fTracker.waitForID (fImageNum );
    } catch (InterruptedException e) {}

    // Check if there was a loading error
    if (fTracker.isErrorID (fImageNum ))
        fMessage= "Error";
    else
        fShow = true;
    // Repaint with the image now if it loaded OK
    repaint ();
  } // run

}// class MediaTrackApplet


/** This JPanel subclass draws an image on the panel if
  * the image is loaded. Otherwise, it draws a text message.
 **/
class DrawingPanel extends JPanel {
  MediaTrackApplet parent = null;

  DrawingPanel (MediaTrackApplet parent) {
    this.parent = parent;
  }// ctor

  public void paintComponent (Graphics g) {
    super.paintComponent (g);

    // Add your drawing instructions here
    if (parent.fShow)
        g.drawImage (parent.fImg,10,10,this);
    else
        g.drawString (parent.fMessage, 10,10);
  } // paintComponent

} // class DrawingPanel

谢谢

最佳答案

Are you referring to the EDT?

是的。这个古老但过时的例子来自一个早于我们现代理解的时代,即 Swing GUI 对象应该event dispatch thread 上构造和操作。 . repaint() 的初始调用具有在 EventQueue 上发布新的 Runnable 的效果,同时允许后台线程完成加载图像对后续 repaint() 的预期。另见 Memory Consistency Properties和这个相关的example .

关于java - 为什么要在此代码中调用重绘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9121784/

相关文章:

java - 为小型桌面应用程序构建 GUI

java - 用于在本地计算机上打开文件但保存在云驱动器中的小程序

java - 如何解决java.io.IOException : failed to load image contents for serialization of jcchart in ByteArrayOutputStream

java - 有没有办法根据线程 "attached"的目标来控制线程?

java - 阻止 java 使用某些库

java - 在 Swing 中,如何在没有特定组件的情况下应用 KeyListener

java - 使用Swing的JList

java - "Allow access to the following application from this website"提示-Java

java - EJB 3.1 无接口(interface) Singleton/Stateful session bean 的多个实例

java - 设置剪贴板内容