java applet interfaces
java applet interfaces
Spring 2002
Chapter 11 - Mouse and Keyboard Events
Interfaces
(a) in defining an interface the key word interface is used instead of the
keyword abstract
(b) none of the methods in an interface are defined, only signatures are
included.
Step 3: Register the listener object with the appropriate event source.
event_source_object.addevent_listenername(event_listener_object);
//Normally, you use the keyword this for event_listener_object.
Mouse Events
Four steps:
import java.awt.event.*;
Step 2: Include Implements MouseListener :
this.addMouseListener (this);
Example
The following program changes the applet background color, whenever
the mouse event occurs.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
this.addMouseListener(this);
}
setBackground(Color.blue);
}
setBackground(Color.cyan);
}
setBackground(Color.green);
}
setBackground(Color.magenta);
}
setBackground(Color.yellow);
}
Moving and dragging the mouse generates an object of the MouseEvent class.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
setBackground(Color.white);
}
setBackground(Color.blue);
}
setBackground(Color.cyan);
}
setBackground(Color.green);
}
setBackground(Color.magenta);
}
setBackground(Color.yellow);
}
Example
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MouseEvent3 extends Applet
implements
MouseMotionListener,MouseListener{
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
setBackground(Color.white);
int x = event.getX();
int y = event.getY();
print(x,y);
}
setBackground(Color.black);
int x = event.getX();
int y = event.getY();
print(x,y);
}
setBackground(Color.blue);
int x = event.getX();
int y = event.getY();
print(x,y);
}
setBackground(Color.cyan);
int x = event.getX();
int y = event.getY();
print(x,y);
}
setBackground(Color.magenta);
int x = event.getX();
int y = event.getY();
print(x,y);
}
setBackground(Color.yellow);
int x = event.getX();
int y = event.getY();
print(x,y);
}
Example
The following program uses the coordinates of the mouse to draw circles.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Example
Simple WhiteBoard
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
KeyEvent class For a list of Class variables (virtual key codes), which
are defined as static and final, see Virtual Key Codes
table below.
event.getModifiers().
VK_ESCAPE,
VK_PRINTSCREEN,
VK_SCROLL_LOCK,
VK_PAUSE
Note
Example
The following program displays several text boxes. Type any key in top box.
The other boxes in the applet show information about the key pressed.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Keys1 extends Applet implements
KeyListener{
TextField input;
TextField output1, output2, output3,
output4, output5;
TextField output6, output7, output8;
public void init(){
="+KeyEvent.getKeyModifiersText(modifiers));
Output
Example
The following program displays two boxes. Type each keyboard key in the first
box and see result in the second box.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Keys2 extends Applet implements
KeyListener{
TextField input;
TextField output;
public void init(){
input = new TextField(30);
input.addKeyListener(this);
output = new TextField(30);
add(input);
add(output);
}
switch(event.getKeyCode()){
Output
Example
A better whiteboard that lets you enter text in addition to freehand drawing
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Whiteboard extends SimpleWhiteboard {
protected FontMetrics fm;
Output