Forum Stats

  • 3,855,570 Users
  • 2,264,523 Discussions
  • 7,906,069 Comments

Discussions

Applet does not refresh properly after exposure (src included)

843807
843807 Member Posts: 46,582
edited Oct 2, 2002 8:02AM in Java Applet Development
Hi,

I wrote an image panel which extends class Panel to perform some image operations. This panel is reused several times by an applet to form a multi-image GUI. The applet is then started in Windows IE (OS XP Professional).

The refresh problem occurs when another Windows application GUI is on top of the applet. If I try to iconize the other Windows application, one of the image panels (always the right most among panels covered by the previous application) does not refresh itself so the artifact of the previous application GUI remains inside the panel. However, if the mouse moves into this panel, the panel gets refreshed and the artifact is removed.

The paint() method of Panel was overwritten in my class (see the src below).

I tried using validate() and some other methods without success. It seems that Windows exposure event is not handled or not transmitted properly. I could not understand why only right most panel could not be refreshed.

I included source and html below.

Thanks in advance for any help or advice.

Yanan

---------- Exp.java ---------------------
<pre>
import java.awt.*;
import java.applet.*;

public class Exp extends Applet{
ImagePanel [] imagePanels;
int numFrames = 4;

public void init() {

setLayout(new GridLayout(1,numFrames));
imagePanels = new ImagePanel[numFrames];
for (int i=0; i<imagePanels.length; i++) {
imagePanels[i] = new ImagePanel();
add(imagePanels);
}
validate();
}

public void destroy() {
for (int i=0; i<imagePanels.length; i++) {
remove(imagePanels[i]);
}
}

}

/**
* Class to build image panel which is the main class for copy-paste
*/
class ImagePanel extends Panel {
Color blankBgColor = new Color(212, 255, 252);
Color lineBgColor = new Color(153, 153, 153);
int xmargin = 5;
int ymargin = 4;

/**
* Constructor
*/
public ImagePanel() {
setBackground(Color.white);

}

/**
* Draw blank image
* @param g: graphics context
*/
void drawBlankImage(Graphics g) {
int w = getWidth();
int h = getHeight();

g.setColor(blankBgColor);
g.fillRect(xmargin, ymargin, w-xmargin*2, h-ymargin*2);
}

/**
* Draw frame
* @param g: graphics context
*/
void drawFrame(Graphics g) {
g.setColor(lineBgColor);

int k = 0;
int w = getWidth()-1;
int h = getHeight()-1;

g.drawLine(k, k, w, k);
g.drawLine(w, k, w, h);
g.drawLine(w, h, k, h);
g.drawLine(k, h, k, k);

// Draw once more to widen the line
k = 1;
w --;
h --;
g.drawLine(k, k, w, k);
g.drawLine(w, k, w, h);
g.drawLine(w, h, k, h);
g.drawLine(k, h, k, k);

System.err.println("I am redrawing frame...");
}

/**
* Overwrite super class paint
* @param g: graphics context
*/
public void paint(Graphics g) {
drawBlankImage(g);
drawFrame(g);
validate();
}

}

</pre>

---------------------------Exp.html-------------------
<pre>

<html>
<head>
<title>Applet Refresh Test</title>
</head>
<body>
<h1>Applet Refresh Test</h1>
<applet code=Exp.class width=800 height=200>
alt="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
Your browser is completely ignoring the <APPLET> tag!
</applet>
</body>
</html>

</pre>

Comments

  • 843807
    843807 Member Posts: 46,582
    Try doing a setOpaque(false) on the panel that is not refreshing properly. Hope it helps!
  • 843807
    843807 Member Posts: 46,582
    setOpaque(false) ??

    setOpaque is SWING, the man talks AWT !


    You can email me the program, i can't paste it from the forum.


    [email protected]
  • 843807
    843807 Member Posts: 46,582
    Your code works perfect in javaVM. It doesn't work in some browsers because Component.getWidth() and its wife are methods starting from JDK1.20. Most browsers are just 1.1.3 or 1.1.4. Thus, you can replace getWidth() by getSize().width and so on for its wife.

    Faifai
  • 843807
    843807 Member Posts: 46,582
    Thanks for the response.

    I tried getSize() but it gives me the same results.
  • 843807
    843807 Member Posts: 46,582
    It means then, sigh, you are as an old man as I am -- using very old browsers supporting only JDK1.0.

    Find "// Modified code starts here" to see how this is done.
    import java.awt.*;
    import java.applet.*;
    
    public class Exp extends Applet{
    ImagePanel [] imagePanels;
    int numFrames = 4;
    
    public void init() {
    
    setLayout(new GridLayout(1,numFrames));
    imagePanels = new ImagePanel[numFrames];
    for (int j=0; j<imagePanels.length; j++) {
    imagePanels[j] = new ImagePanel();
    add(imagePanels[j]);
    }
    validate();
    }
    
    public void destroy() {
    for (int j=0; j<imagePanels.length; j++) {
    remove(imagePanels[j]);
    }
    }
    
    }
    
    /**
    * Class to build image panel which is the main class for copy-paste
    */
    class ImagePanel extends Panel {
    Color blankBgColor = new Color(212, 255, 252);
    Color lineBgColor = new Color(153, 153, 153);
    int xmargin = 5;
    int ymargin = 4;
    
    /**
    * Constructor
    */
    public ImagePanel() {
    setBackground(Color.white);
    
    }
    
    /**
    * Draw blank image
    * @param g: graphics context
    */
    void drawBlankImage(Graphics g) {
    // Modified code starts here
    int w = bounds().width;
    int h = bounds().height;
    // Modified code ends
    
    g.setColor(blankBgColor);
    g.fillRect(xmargin, ymargin, w-xmargin*2, h-ymargin*2);
    }
    
    /**
    * Draw frame
    * @param g: graphics context
    */
    void drawFrame(Graphics g) {
    g.setColor(lineBgColor);
    
    int k = 0;
    // Modified code starts here
    int w = bounds().width-1;
    int h = bounds().height-1;
    // Modified code ends
    
    g.drawLine(k, k, w, k);
    g.drawLine(w, k, w, h);
    g.drawLine(w, h, k, h);
    g.drawLine(k, h, k, k);
    
    // Draw once more to widen the line
    k = 1;
    w --;
    h --;
    g.drawLine(k, k, w, k);
    g.drawLine(w, k, w, h);
    g.drawLine(w, h, k, h);
    g.drawLine(k, h, k, k);
    
    System.err.println("I am redrawing frame...");
    }
    
    /**
    * Overwrite super class paint
    * @param g: graphics context
    */
    public void paint(Graphics g) {
    drawBlankImage(g);
    drawFrame(g);
    validate();
    }
    
    }
    Faifai
  • 843807
    843807 Member Posts: 46,582
    Hi, Faifai, thanks for detailed help.

    However, I tried bound().width and bound().height and no effect on my XP Professional running IE Version 6. I don't understand your word about "using very old browsers supporting only JDK1.0." I am using JDK1.4.0.

    I wonder how Windows passes the exposure event back to Java and which Java method is used to do the repaint.

    Yanan
  • 843807
    843807 Member Posts: 46,582
    If you read some technical documents, you will find that the Microsxxt JSP is very different from the Java standard. So, SUN announced that MSIE supports up to version 1.1.4. For IE6.0, I bet you must read MSJ++ documentation for some hints (or wait for some expert advice here!)

    Hope that helps.

    Faifai
  • 843807
    843807 Member Posts: 46,582
    Hi Hi,
    I searched MSJ++ documentation and found both Component.getSize() and Component.getBounds() compatible with SUN's Java. Then I tried getSize().width and getSize().height and your applet worked perfectly (with MSIE5.5.)

    Your saying earlier that getSize() didn't work in your case triggered my thought that it might be caused by the IE caching function. Caching will cause IE to use old copies of the classes instead of loading new ones. From the Internet Options menu, the Temporary Internet Files setting should be set at something like Check New Version at Every Time of Starting IE. That was too among my first few troubles with Applets.

    I have your code compiled (using getSize()) and stored on my page. Please try with your MSIE6.0 to see if it works.

    http://home.hkstar.com/~yyf/exp.html

    Faifai
  • 796365
    796365 Member Posts: 8,041
    Also be aware of the same caching concern with the Plug-in cache.
  • 843807
    843807 Member Posts: 46,582
    Faifai,

    I tried your page and it turned out to be the same on my machine. The problem may be the drawing timing as the frame and background are all drawings.

    I have changed the scheme and use buffered the image to achieve the same effect. It slows things down a bit but at least display problem is gone.

    Thanks for all the helps.

    Yanan
This discussion has been closed.