import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* This simple applet recreates most of the interface from
the GUI3 "application." If you click on the button, it
disappears. If you click on the image, the image is swapped
with the alternate image. That is all it does. The Java
1.1 event model is used.
*/
public class GUI3Applet extends Applet
{
//The button that disappears
Button disappearButton = new Button("I Disappear");
//Variables to hold the two images
Image img1, img2, imgTemp;
//For the layout of the interface
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc1 = new GridBagConstraints();
//Listens for mouse clicks on the images
MouseAdapter mouser = new MouseAdapter ()
{
public void mousePressed(MouseEvent m)
{
//Swap the images
imgTemp=img1;
img1=img2;
img2=imgTemp;
//Draw the other image
forGo.repaint();
}
};
//Listens for a click on the disappearing button
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//Make the button disappear
remove(disappearButton);
}
};
//A component to draw the images on
Canvas forGo = new Canvas()
{
public void paint(Graphics g)
{
//Draw the image in img1 g.drawImage(img1,0,0,100,100, this);
}
};
//A single method to put everything into the interface
public void init()
{
//Make the applet background black
setBackground(Color.black);
//Load the two images from the server
img1 = getImage(getDocumentBase(), "on.gif");
img2 = getImage(getDocumentBase(), "off.gif");
//Register the event listeners with their components
forGo.addMouseListener(mouser);
disappearButton.addActionListener(listener);
//Set up the components with the layout manger
gridbag.rowHeights = new int[] {175};
setLayout(gridbag);
gbc1.weightx=1.0;
gridbag.setConstraints(disappearButton, gbc1);
gbc1.fill=gbc1.BOTH;
gbc1.gridheight=gbc1.REMAINDER;
gridbag.setConstraints(forGo, gbc1);
//Put the components on the applet
add(disappearButton);
add(forGo);
}
}