Repaint and Delay Problem

From: Doug van Vianen (courses_at_shaw.ca)
Date: 11/27/03

  • Next message: Tony Dahlman: "Re: How to set (national) language in Java"
    Date: Thu, 27 Nov 2003 02:14:40 GMT
    
    

    I wrote the following short test applet to try to solve a problem but
    have not had any luck. Hopefully someone can help me with a solution.

    I simply wish to display a red letter 'A', wait a few seconds, and
    then display a blue letter 'B'. I use the string 'cheatop' to let the
    paint routine know which is to be displayed.

    What happens is that when the applet loads, a three second delay occurs
    okay and the blue letter 'B' is displayed. Nothing appears when the
    red letter 'A' should appear before the delay.

    What appears to be happening is that all of the painting is done after
    the delay so that the red 'A' is overwritten by the blue 'B'. Is there any
    way to force the first repaint to occur?

    import java.applet.*;
    import java.awt.*;

    public class test extends java.applet.Applet {

    String cheatop;

     public void init() {
      cheatop="A";
      repaint();

      try {
       Thread.sleep(3000);
      }
      catch (InterruptedException e) {
      }

      cheatop = "B";
      repaint();
     }

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

     public void paint(Graphics g) {
      if (cheatop == "A") {
       g.setColor(Color.red);
       g.drawString("A", 20, 20);
      }
      if (cheatop == "B") {
       g.setColor(Color.blue);
       g.drawString("B", 20, 20);
      }
     }
    }

    Thank you.


  • Next message: Tony Dahlman: "Re: How to set (national) language in Java"

    Relevant Pages

    • Re: Repaint and Delay Problem
      ... > I wrote the following short test applet to try to solve a problem but ... > I simply wish to display a red letter 'A', wait a few seconds, and ... > red letter 'A' should appear before the delay. ... All windowing stuff (like painting ...
      (comp.lang.java.help)
    • JSplitPane and another issue
      ... public class MainPanel extends JPanel implements ActionListener { ... private WebBrowserPane browserPane; ... public void actionPerformed{ ... // Display the window. ...
      (comp.lang.java.gui)
    • JSplit Pane and another issue
      ... public class MainPanel extends JPanel implements ActionListener { ... private WebBrowserPane browserPane; ... public void actionPerformed{ ... // Display the window. ...
      (comp.lang.java.gui)
    • Need Help converting int -> char -> string & TextArea display(!)
      ... display its contents character-by-character ... Please can someone show me how to read each character ... private TextArea inputTextArea; ... public void actionPerformed ...
      (comp.lang.java.help)
    • Re: hi i have a query regarding inheritance
      ... int a,b,c ... public void display() ... class abcde extends abcd ...
      (comp.lang.java.help)