[java] what function is called in an applet on focus??

Started by
5 comments, last by Scorpion 23 years ago
  
import java.applet.Applet;
import java.awt.*;

/**
 * Title:        Repaint Demo
 * Description:  Demonstratie van hoe repaint() dus werkt..
 * Copyright:    ----
 * Company:      ----
 * @author ----
 * @version 0.2
 */

public class repaintDemo extends Applet {

  private boolean debug = true;
  private int text = 1;
  private int xres,yres;

  private void debug(String s) {
    if (debug) System.out.println("debug: "+s);
  }

  public repaintDemo() {
    debug("repaintDemo()");
    xres=this.getWidth();
    yres=this.getHeight();
  }

  public void init() {
    debug("init()");
    setBackground(Color.black);
  }

  public void update(Graphics g) {
    debug("update("+g+")");
  }

  public void paint(Graphics g) {
    switch(text) {
      case 1 :
        text=2;
        g.setColor(Color.white);
        g.drawRect(0,0,xres,yres);
      break;
      case 2 :
      break;
    }
    debug("paint("+g+")");
  }

  public void repaint(int x,int y,int width,int height) {
    debug("repaint("+x+","+y+","+width+","+height+");");
    repaint();
  }

  public void repaint(int ms, int x,int y,int width,int height) {
    debug("repaint("+ms+","+x+","+y+","+width+","+height+");");
    repaint();
  }

  public void repaint() {
    Graphics g = getGraphics();
    update(g);
  }

}
  
as you can see if you fire this baby up, and move another window over it (drag&drop) then a repaint is involved, but only paint(Graphics g) is called, and no repaint or what so ever. can someone tell me what function IS called? Edited by - Scorpion on March 23, 2001 2:33:31 AM
Advertisement
You can''t override repaint. I don''t remember why.

Override paint. repaint calls paint. paint calls update.
quote:Original post by tunabox

You can''t override repaint. I don''t remember why.

Override paint. repaint calls paint. paint calls update.


that''s not it.

if you call repaint(), it will set an interval, which will call update(Graphics g), as soon as it runs out. If you call repaint again within the interval, it resets the interval, so it will only paint once in the ## ms. instead of 20x calling paint which wouldn''t be neccesery.

I find this quite a pain in the ass, so i removed the interval, by overriding repaint, which calls update, which calls paint.

backbuffering will be done in update, and the drawing in paint.

so that''s all fixed.

I encountered by the way that RepaintArea calls the paint(Graphics g) method. I just can''t override RepaintArea. there is no source etc. availible. Is it just me, or can''t this be done?
I think this is how it works: repaint is how YOU call update, which calls paint. Other things can call update but they probably don''t have to go through repaint to do so.
Yes, anonymous is right.

But that was not my problem
The problem is, when i add components to the applet, or a redraw is needed, because the applet gained focus after being behind some other window, it calls paint from another method. (RepaintArea i''ve heard on another board), which causes the applet to paint the background first, on the part that needs to be redrawn, and then it calls paint, to fill it up with the desired painting.

Can someone tell me how to override this, because it''s pretty annoying seeing this flickering.

thanks!
Ahh, I had that problem. Override update() in the applet.

The default behavior of update() is to paint the area the color of the background, then call paint(). That''s what''s causing the flickering.

Putting this block into your applet should fix it:

  public void update(Graphics g){	paint(g);} // update  


Not much to it, but it works.
quote:Original post by c_wraith

Ahh, I had that problem. Override update() in the applet.

The default behavior of update() is to paint the area the color of the background, then call paint(). That''s what''s causing the flickering.

Putting this block into your applet should fix it:

    public void update(Graphics g){	paint(g);} // update    


Not much to it, but it works.

  public void update(Graphics g) {debug("update("+g+")");}  


As you can see, i have implemented this as well. it works fine in my full-applet (It calls update, update makes backbuffering, and calls paint, swaps the buffers, and the image is showed on screen) But this function ISN''T called when i switch between fields (alt-tab) so it needs a repaint (another window overlaps the applet). Or when it adds a component.

I have checked some things, and came out onto "RepaintArea" which only repaints that part of the applet which is needed.

The problem is, i can''t override the method, and that method paints first the background color to the applet, and then calls paint, to paint the image onto it.

so NO update is involved...

This topic is closed to new replies.

Advertisement