Need for MediaTracker - I


The html file should have the applet tag for the class file

a1.html

<applet code="zzz" width=350 height=250></applet>


The import statements for the program codes are

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


zzz.java
public class zzz extends Applet implements Runnable
{       Thread a;
        Image m[] = new Image[10];
        int j=0,k=0,x=0;
        public void init()
        {       for (int i = 1; i <= 10; i++)
                {       m[j] = getImage(getDocumentBase(), "T"+i+".gif");
                        j++;
                }
                a=new Thread(this);
                a.start();
        }
        public void run()
        {       while (true)
                {       try Thread.sleep(100);
                        catch (InterruptedException e)
                        {       break;
                        }
                        x++;
                        if (x >= m.length)
                                x = 0;
                                repaint();
                }
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public void paint(Graphics g)
        {       g.drawImage(m[x], 150, 100, this);
                showStatus("value of k is " + k++);
        }
}
zzz.java
public class zzz extends Applet implements Runnable
{       Thread a;
        Image m[] = new Image[10];
        int j=0,k=0,x=0;
        public void init()
        {       for (int i = 1; i <= 10; i++)
                {       m[j] = getImage(getDocumentBase(), "T"+i+".gif");
                        j++;
                }
                a=new Thread(this);
                a.start();
        }
        public void run()
        {       while (true)
                {       try Thread.sleep(100);
                        catch (InterruptedException e)
                        {       break;
                        }
                        x++;
                        if (x >= m.length)
                                x = 0;
                        repaint();
                }
        }
        public void paint(Graphics g)
        {       g.drawImage(m[x], 150, 100, this);
                showStatus("value of k is " + k++);
        }
}
There is no update function in the above program

zzz.java

public class zzz extends Applet implements Runnable
{       Image b;
        Thread a;
        Image m[] = new Image[10];
        int j=0,k=0,x=0;
        public void init()
        {       b=getImage(getDocumentBase(), "borderex1.gif");
                for (int i = 1; i <= 10; i++)
                {       m[j] = getImage(getDocumentBase(), "T"+i+".gif");
                        j++;
                }
        a=new Thread(this);
        a.start();
        }
        public void run()
        {       while (true)
                {       try Thread.sleep(100);
                        catch (InterruptedException e)
                        {       break;
                        }
                        x++;
                        if (x >= m.length)
                                x = 0;
                        repaint();
                }
        }
        public void paint(Graphics g)
        {       g.drawImage(b, 0, 0, this);
                g.drawImage(m[x], 200, 150, this);
                showStatus("value of k is " + k++);
        }
}
zzz.java
public class zzz extends Applet implements Runnable
{       Image b;
        Thread a;
        Image m[] = new Image[10];
        int j=0,k=0,x=0;
        public void init()
        {       b=getImage(getDocumentBase(), "borderex1.gif");
                for (int i = 1; i <= 10; i++)
                {       m[j] = getImage(getDocumentBase(), "T"+i+".gif");
                        j++;
                }
                a=new Thread(this);
                a.start();
        }
        public void run()
        {       while (true)
                {       try Thread.sleep(100);
                        catch (InterruptedException e) {break;}
                        x++;
                        if (x >= m.length)
                                x = 0;
                        repaint();
                }
        }
        public void update(Graphics g)
        {       paint(g);
        } 
        public void paint(Graphics g)
        {       g.drawImage(b, 0, 0, this);
                g.drawImage(m[x], 200, 150, this);
                showStatus("value of k is " + k++);
        }
}
zzz.java
public class zzz extends Applet implements Runnable
{       MediaTracker t;
        Image b;
        Thread a;
        Image m[] = new Image[10];
        int j=0,k=0,x=0;
        public void init()
        {
                t=new MediaTracker(this);
                b=getImage(getDocumentBase(), "borderex1.gif");
                t.addImage(b,0);
                for (int i = 1; i <= 10; i++)
                {       m[j] = getImage(getDocumentBase(), "T"+i+".gif");
                        t.addImage(m[j],1);
                        j++;
                }
                try
                {       t.waitForID(0);
                        t.waitForID(1);
                }
                catch (InterruptedException e)
                {       return;
                }
                a=new Thread(this);
                a.start();
        }
        public void run()
        {       while (true)
                {       try Thread.sleep(100);
                        catch (InterruptedException e)
                        {       break;
                        }
                        x++;
                        if (x >= m.length)
                                x = 0;
                        repaint();
                }
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public void paint(Graphics g)
        {       g.drawImage(b, 0, 0, this);
                g.drawImage(m[x], 200, 150, this);
                showStatus("value of k is " + k++);
        }
}
Try the above progam without the update function.
Your comments , feedback , suggestions are most welcome. Let us know what you think .

Undertanding the Media Tracker a little more in detail.

Back to the Java Page


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in