/*
A
"bare bones" outline of a simple Swing applet
The applet is created, a panel is placed on the applet to draw on
and the MouseListener, Mouse MotionListener, FocusListener, and KeyListener
interfaces are registered with the panel.
The mousePressed() method will request focus when clicked on the applet.
by W. Dodd 2005
*/
import java.awt.*;
import javax.swing.*; // Swing GUI classes are defined
here.
import java.awt.event.*; // Event handling class are defined
here.
public class BareBones extends JApplet implements ActionListener, MouseListener,
MouseMotionListener, FocusListener, KeyListener{
MyDrawingClass screen;
Color myColor=new Color(92,167,175);//Set a background
color for the applet
Boolean focussed=false;
// declare other variables here so they are global
public void init() {
screen=new MyDrawingClass();//create a new object that
is a subclass of JPanel
setContentPane(screen);//add the object to the applet
screen.addMouseListener(this);//register the MouseListener
screen.addMouseMotionListener;//register
the MouseMotionListener
screen.addKeyListener(this);//register the KeyListener
screen.addFocusListener(this);//register the FocusListener
screen.setBackground( myColor );
} // end init()
// Your program methods declared here for example....
public void drawSomething(){
Graphics g=getGraphics();//allows you to use the Graphics
object
//code to draw
}
// Internal class to draw on the panel placed in the applet
class MyDrawingClass extends JPanel{
//class to draw on a JApplet
public void paintComponent(Graphics g){
super.paintComponent(g);
//either call other drawing methods or put the code here.
For example....
drawSomething();
}
}//end MyDrawingClass
//
Interface methods
//MouseListener methods
public void mousePressed(MouseEvent evt){
screen.requestFocus();//gets focus on mousePressed()
}
public void mouseReleased(MouseEvent evt){
}
public void mouseClicked(MouseEvent evt){
}
public void mouseEntered(MouseEvent evt){
}
public void mouseExited(MouseEvent evt){
}
// MouseMotionListener
methods
public void mouseDragged(MouseEvent
evt){
}
public void mouseMoved(MouseEvent evt){
}
//
KeyListener methods
public void keyPressed(KeyEvent evt){
if (focussed){
repaint();}
}
public void keyReleased(KeyEvent evt){}
public void keyTyped(KeyEvent evt){}
//FocusListener methods
public void focusGained(FocusEvent evt){
focussed=true;
repaint();
}
public void focusLost(FocusEvent evt){
focussed=false;
repaint();
}
//ActionListener method
public void actionPerformed(ActionEvent evt) {
//Handle the action event
} // end actionPerformed()
} // end class BareBones