0

I'm writing a small JAVA applet.

Which gets a random number between 1 to 6 and prints them on APPLET screen. What i want to do is.. Loop 50 times on screen and print various randdom numbers. [Each time clearing the previous number].

And after that loop.. It prints any 1 final random number on applet..

My problem is: The loop. It is printing all numbers over each ither and screen is not getting cleared. What is wrong? I have tried many methods of clearing applet screen like drawing rectangle or using clearRect() function. Nothing is working. Here's the code.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;



public class Shapes extends Applet{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    Random r = new Random();
    Dimension d = getSize();
    Font f = new Font("TimesRoman", Font.BOLD, 96);
    public void paint(Graphics g)
    {
        for(int m=0;m<=50;m++){     
                int k = Math.abs((r.nextInt()) % 6) + 1;
                g.setFont(f);
                g.setColor(Color.BLACK);
                g.drawString(String.valueOf(k) , 70, 100);  
                g.setColor(Color.WHITE);
                g.drawRect(0, 0, d.width, d.height);
                try{Thread.sleep(70);}catch(Exception e){}
        }



    }

}
michid
  • 10,536
  • 3
  • 32
  • 59
  • 1
    1) Why code an applet? If it is due to spec. by teacher, 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 AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 03 '14 at 06:58

3 Answers3

1

The matter is that at object creation time, the applet hasn't got size, so you must wait to get the dimension of the applet. For example, at render time, like this:

public void paint(Graphics g)
{
    d = getSize();
    for(int m=0;m<=50;m++){
        g.clearRect(0, 0, (int) d.getWidth(), (int) d.getHeight());

        int k = Math.abs((r.nextInt()) % 6) + 1;
        g.setFont(f);
        g.setColor(Color.BLACK);
        g.drawString(String.valueOf(k) , 70, 100);
        try{Thread.currentThread().sleep(70);}(Exception e){}
    }
}
angel_navarro
  • 1,757
  • 10
  • 11
  • Thank you very much.. It worked and i understood the concept perfectly.. :) – Arpit Agrawal May 02 '14 at 16:04
  • I'm according to you @ZoveGames, It's true is not recommendable (it's a bad thing) calling sleep() in EDT ;-). Really the correct answer is a mixing of both answer (yours and mine). Yours for the timed call from outer of EDT, and mine for the getSize() matter Regards, – angel_navarro May 02 '14 at 19:16
1

Remember that paint(Graphics) is on the event dispatch thread so sleeping it will freeze the entire UI. You need to use asynchronous repaints like this:

public void init(){
    Timer t=new Timer(70, new ActionListener(){
        public void actionPerformed(ActionEvent e){repaint();}
    });
    t.setCoalesce(true);
    t.setRepeats(true);
    t.start();
}
public void paint(Graphics g){...}
DankMemes
  • 2,085
  • 2
  • 21
  • 30
1
public void paint(Graphics g)
{
    // ..

Should be:

public void paint(Graphics g)
{
    super.paint(g); // VERY IMPORTANT! Draw BG and borders etc.  
    // ..
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433