/** PipeApplet.java
* Example Java Swing program displays Magritte's pipe, menu, and button.
* Modification of Pipe.java application. This version can be run as
* an applet or application.
*
* Must extend JApplet, have init() and main() call makeContentPane().
* makeContentPane() creates the GUI in a JPanel container.
*
* Mike Barnes
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// subclass JApplet.
public class PipeApplet extends JApplet implements ActionListener {
// instantiate the menubar and menuitems
private boolean asApplet; // how program was started
private ImageIcon pipeImage, renzoImage;
private JMenuBar menuBar;
private JMenu aboutMenu;
private JMenuItem artistItem, authorItem;
private JButton aButton;
private boolean toggle = true;
private String frenchText = new String(
"Ceci n'est pas une pipe");
private String englishText = new String(
"This is not a pipe");
// starting point for applet usage
public void init() {
asApplet = true;
setContentPane(makeContentPane());
}
// starting point for application usage
public static void main(String args[]) {
JFrame frame = new JFrame("With apologies to Rene Magritte");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// note that no explicit PipeApplet constructor is needed.
PipeApplet applet = new PipeApplet();
applet.asApplet = false;
frame.setContentPane(applet.makeContentPane());
frame.setBounds(20,20,440,360); // set location and size
frame.setResizable(false);
frame.pack();
frame.setVisible(true); // display the JFrame
}
// create and setup menu, dialogs, icon, and button.
public Container makeContentPane() {
// see how to load icons: as applet or application.
if (asApplet) {
pipeImage = new ImageIcon(getImage(getDocumentBase(), "pipe.jpg"));
renzoImage = new ImageIcon(getImage(getDocumentBase(),"renzo.gif"));}
else {
pipeImage = new ImageIcon("pipe.jpg");
renzoImage = new ImageIcon("renzo.gif"); }
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout(5,5));
// create menus
JMenuBar menuBar = new JMenuBar();
// setJMenuBar() works for JApplet and JFrame separately not combined
// setJMenuBar(menuBar);
aboutMenu = new JMenu("About");
aboutMenu.setMnemonic('A');
aboutMenu.setToolTipText(
"See information about Magritte and Barnes");
// create a menu item and the dialog it invokes
artistItem = new JMenuItem("artist");
artistItem.addActionListener( // anonymous inner class event handler
new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog( PipeApplet.this,
"Rene Magrite\n" +
"Belgium surrealist 1898-1967",
"Artist",
JOptionPane.PLAIN_MESSAGE);
}}
);
aboutMenu.add(artistItem); // add menu item to menu
// create a menu item and the dialog it invokes
authorItem = new JMenuItem("author");
authorItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog( PipeApplet.this,
"G. Michael Barnes\n" +
"Computer Science Dept\n" +
"California State University Northridge\n\n" +
"www.csun.edu/~renzo\n"+
"818.677.2299",
"Author",
JOptionPane.INFORMATION_MESSAGE, renzoImage );
}}
);
aboutMenu.add(authorItem);
menuBar.add(aboutMenu);
// create icon label from jpeg file
JLabel pipeLabel = new JLabel();
pipeLabel.setIcon(pipeImage);
contentPane.add(pipeLabel, BorderLayout.CENTER);
aButton = new JButton(frenchText);
aButton.setToolTipText("Click to switch between French and English");
aButton.addActionListener(this);
contentPane.add(aButton, BorderLayout.SOUTH);
// with combined JApplet, JFrame use NORTH for menu
contentPane.add(menuBar, BorderLayout.NORTH);
return contentPane;
}
// Respond to button clicks
public void actionPerformed (ActionEvent e) {
if (toggle) {
aButton.setText(englishText);
toggle = false; }
else {
aButton.setText(frenchText);
toggle = true; }
}
}