0

I read this: http://www.dmc.fmph.uniba.sk/public_html/doc/Java/ch10.htm to help me gain an understanding of runnables and the applet and decided to test out this code. However, within the thread, the repaint() method isn't being called.

public class test extends Applet implements Runnable{
    int hello;

    public void start(){
        Thread run = new Thread(this);
        run.start();
    }

    public void run() {
        for (int i = 0; i < 30; i++){
            hello = i;
            repaint();
        }
    }

    public void paint(Graphics g){
        System.out.println(hello);
    }
}

I'd expect my results to be something like:

0
1
2
...
29

However, instead I get:

29
29

and I don't understand why. Is it because I don't have a stop() method?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ji Ho Oh
  • 15
  • 8
  • Multiple calls to `repaint()` in quick succession will typically be coalesced into a single call. 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Sep 20 '17 at 03:53

1 Answers1

-1

Edit:

I was thinking of components, not Applet. Repaint isn't the same as paint. If you wanted to see the loop index as you're saying, you'd need to call paint and supply a Graphics object to the method.

jdhurricanes
  • 142
  • 9
  • How would I do that? I tried putting in @Override for both run() and paint() but that doesn't seem to work – Ji Ho Oh Sep 19 '17 at 17:02
  • See my edit above. I forgot Applet doesn't have those methods. They're a part of JComponent, which is subclassed by classes like JPanel. See http://www.oracle.com/technetwork/java/applet-137165.html as an example of painting in an Applet. – jdhurricanes Sep 19 '17 at 17:28