Overridable Events
You can override any of the methods in this list to make your applet respond to events.
public void init() {
This method is called when your applet begins.
public void destroy() {
This is called when your applet is unloaded. This is when the browser containning your applet is exited or when the user leaves the page.
public void start() {
This is called after the "init" event. It is also called at times when the user is not using your applet and starts to use it again such as when a minimized browser containning your applet is maximized.
public void stop() {
This is called when the user stops using your applet. Such as when he minimizes the browser. You generally want to stop any processor intensive operations when this is called.
public boolean mouseDown(Event evt, int x, int y) {
This is called when the user presses the mouse button down. This event recieves three paramaters. The first is of the type "Event" found in the Java "awt" package. This parameter is actually not very useful. This just means that you must include the statement "import java.awt.Event;" or "import java.awt.*" at the beginning of your program. The next two paramater - "x" and "y" contain the coordinates where the button was pressed down. At the end of this method, you should always include the statement "return true" because the method has a return value of "boolean." This tells the Applet that you did override the method. Because many platforms (such as the Macintosh) have mice with only one button Java only supports one button.
public boolean mouseUp(Event evt, int x, int y) {
This is much like the mouseDown event except it is called when the mouse button is released.
public boolean mouseMove(Event evt, int x, int y) {
This event is called when the mouse if moved over the applet. The coordinates are the current coordinates of the mouse pointer.
public boolean mouseDrag(Event evt, int x, int y) {
The same as the "mouseMove" event except it is called when the mouse is moved with its button held down.
public boolean mouseEnter(Event evt, int x, int y) {
This event is called when the cursor is moved into the applet. The coordinates are the point where the mouse pointer enters the applet.
public boolean mouseExit(Event evt, int x, int y) {
This event is called when the mouse pointer leaves the applet. The coordinates are the point where the pointer left the applet.
public boolean keyDown(Event evt, int x) {
This is called when one of the keys is pressed down on the keyboard. The "x" contains a code representing the key which was pressed.
public void paint(Graphics g) {
This is called when the applet needs to be repainted (because something moved over it) or when the "repaint" method is called. It is where you will want to draw things on the screen. The Graphics class is a graphics context that allows you to draw to the screen. You must include "import java.awt.Graphics;" or "import java.awt.*" in the beginning of the applet to use this event.
public void Update(Graphics g) {
This method is actually not an event. It is called when the "repaint" sub is called. The method draws the gray background to the screen, and then calls the "paint" method. You may want to override this to prevent the gray background from being drawn and to prevent screen flicker in animations. If you do, make sure to call the paint method at the end of the method and pass the Graphics class to it.