When to have the Update function and When Not To


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.*;
import java.io.*;

zzz.java
public class zzz extends Applet
{       int i = 0;
        public void init()
        {
        }
        public void paint ( Graphics g)
        {       i++;
                g.drawString("value of i "+i,1,i);
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}
zzz.java
public class zzz extends Applet
{       int i = 0;
        public void init()
        {
        }
        public void paint (Graphics g)
        {       i++;
                g.drawString("value of i "+i,1,i);
        }
        public void update(Graphics g)
        {
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}
zzz.java
public class zzz extends Applet
{       int i = 0;
        public void init()
        {
        }
        public void paint ( Graphics g)
        {       i++;
                g.drawString("value of i "+i,1,i);
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}

zzz.java
public class zzz extends Applet
{       int i = 0;
        public void init()
        {
        }
        public void paint ( Graphics g)
        {       i++;
                g.drawString("value of i "+i,10,10);
                g.drawString("value of i "+i,10,40);
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}

/*
Start Notepad while the applet window is still on. Move the notepad window
in such a way that it covers up only the entire second string .
When the notepad window is now moved out of the applet window ,
the value of i changes  for the second string only changes. Click with
the mouse , both the values of i change.
*/

zzz.java
public class zzz extends Applet
{       Image i;int j=0;
        public void init()
        {       i = getImage(getDocumentBase(),"jim.graham.gif");
        }
        public void paint ( Graphics g)
        {       g.drawImage(i,0,0,this);
                showStatus("value of j "+j++);
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}

zzz.java
public class zzz extends Applet
{       Image i;int j=0;
        public void init()
        {       i = getImage(getDocumentBase(),"jim.graham.gif");
        }
        public void paint ( Graphics g)
        {       g.drawImage(i,0,0,null);
                showStatus("value of j "+j++);
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}
zzz.java
public class zzz extends Applet
{       Image i;int j=0;
        public void init()
        {       i = getImage(getDocumentBase(),"jim.graham.gif");
        }
        public void paint ( Graphics g)
        {       g.drawImage(i,0,0,null);
                showStatus("value of j "+j++);
        }
        public void update(Graphics g)
        {       paint(g);
        }
        public boolean mouseDown(Event e, int x,int y)
        {       repaint();
                return true;
        }
}

Your comments , feedback , suggestions are most welcomed. Tell us how we can improvise the tutorial.

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
1