1

I am trying to draw about 100 images on the Applet. When i did that I was not able to look at an image as the process was too fast. So I added sleep function so that I can give a pause between transition from one image to another. But that worked abnormally. I could not see any pictures and I think the sleep is getting called again and again. Please help.
Here is my code:

public class Test extends Applet
{
public void init()
{

    setSize(1000,1000);


}

public void make(Graphics g,int i)
{


}
public void paint(Graphics g)
{
    int i=0;
    for(i=0;i<100;i++)
    {
        if(i!=65)
        {
            Image img = getImage(getDocumentBase(), "abc"+i+".png");
            g.drawImage(img, 0, 0, this);
            try
            {
                Thread.sleep(1000);
            }
            catch(Exception exception)
            {

            }
        }
    }

}

}

Now you can see I have images from 0 to 99 and I want them on my Applet window and after an image is displayed 1 sec delay should be there. But this is not the case. Please help

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Akshay Bhasin
  • 581
  • 2
  • 6
  • 13
  • 2
    Why are you catching an exception and not printing/handling it? This will hide the exception and you'll have no idea if something was unexpected. Try to avoid that, it's bad practice. – Maroun Aug 17 '14 at 07:02
  • sorry for that. I am a beginner actually. will take care in the future..any help on the thing I asked? – Akshay Bhasin Aug 17 '14 at 07:11
  • 1) Why code an applet? If it is due to spec by your instructor, 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 components rather than Swing? See [this answer](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon AWT. – Andrew Thompson Aug 18 '14 at 01:25

3 Answers3

2

sleep will freeze the EDT (Event Dispatching Thread). Since Swing is single threaded framework, anything that blocks (like sleep), prevents the EDT from running since paint is called from the context of the EDT. Don't use sleep, use Timer instead.

Another note, it's bad practice to catch an exception and not handling it. This will hide serious unexpected things that might occur in your code, at least print the error message.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

Do not use Thread.sleep() as it will freeze your Swing application.

Instead you should use a javax.swing.Timer.

See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
0

What you should do

  1. Do not draw directly on applet. Draw on a seperate panel like JPanel and add it to applet.
  2. Dont call sleep() as it blocks the EDT. All Swing components are painted on EDT, so if it is blocked it will cause problem.
  3. Someone also tries with a seperate thread but it is too not a good option as the thread sleeps for a time and then a repaint() mis called. It is better to use javax.swing.Timer which triggers a event after a period of time.

Try this code

public class Test extends JApplet {

    int imgNo = 0;
    BufferedImage bi;

    JPanel p = new JPanel(){
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);

            g.setColor(Color.white);
            g.fillRect(0, 0, getWidth(), getHeight());
            if(bi != null)
               g.drawImage(bi, 0, 0, null);
        }
    };
    Timer t;

    @Override
    public void init() {
        super.init();
        setSize(400,400);
        t = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                   if(imgNo != 65)
                     bi = ImageIO.read(new File("abc"+i+".png"));
                }catch(Exception e){
                   e.printStackTrace();
                }
                imgNo++;
                p.repaint();
            }
        });
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                p.setOpaque(true);
                p.setBackground(Color.white);
                setContentPane(p);
            }
        });
    }

    @Override
    public void start() {
        super.start();
        t.start();
    }

    @Override
    public void stop() {
        super.stop();
        t.stop();
    }
}

There is still a disadvantage in my code as I read the images from file in the ActionListener of Timer. It is now ok for a beginner but I will recommend you to use a SwingWorker which will load the images beforehand and before even applet is started. This upgradation I will provide later

Niru
  • 732
  • 5
  • 9