/**
This frame will display a picture that the user
selects using radio buttons.
@author: B.Brenner
@version: 1/26/10
*/
import java.awt.* ; // for FlowLayout and Dimension classes
import javax.swing.*; // for Swing components
import java.awt.event.* ; // for ActionListener interface
import javax.swing.BorderFactory;
import javax.swing.border.Border;
@SuppressWarnings("serial")
public class ImageViewerFrame extends JFrame{
// Instance variables
String [] descriptions = new String[6]; // 1 description per image
String [] pictures = new String[6];
Border loweredbevel = BorderFactory.createLoweredBevelBorder(); // Set up the border
JPanel headingPanel;
JPanel controlPanel;
JLabel pictureLabel;
JLabel headingLabel;
JLabel instructionsLabel;
JTextArea outputTextArea; // I could have used a label containing HTML to create a multiple row label
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu exitMenu = new JMenu("Exit");
JMenu aboutMenu = new JMenu("About");
JMenu imageMenu = new JMenu("Images");
JMenuItem image1 = new JMenuItem("Gregory Lake");
JMenuItem image2 = new JMenuItem("Devils Post Pile");
JMenuItem image3 = new JMenuItem("JMT Sign");
JMenuItem image4 = new JMenuItem("Lonely Tree");
JMenuItem image5 = new JMenuItem("View From Whitney");
JMenuItem image6 = new JMenuItem("Lone Pine Lake");
JMenuItem exit = new JMenuItem("Close");
String mName ="";
// constructor
public ImageViewerFrame(){
// Helper methods
loadPictureInformation();
createHeadingPanel();
createOutputArea();
// Create the area where the pictures will be displayed
pictureLabel = new JLabel();
pictureLabel.setIcon(new ImageIcon("images/" + pictures[0]));
pictureLabel.setBorder(loweredbevel);
// Add the components to the frame
setLayout(new BorderLayout());
add(controlPanel, BorderLayout.WEST);
add(headingPanel, BorderLayout.NORTH);
add(outputTextArea, BorderLayout.SOUTH);
add(pictureLabel, BorderLayout.CENTER);
setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(imageMenu);
menuBar.add(aboutMenu);
menuBar.add(exitMenu);
imageMenu.add(image1);
imageMenu.add(image2);
imageMenu.add(image3);
imageMenu.add(image4);
imageMenu.add(image5);
imageMenu.add(image6);
exitMenu.add(exit);
}
/**
Loads the arrays with the file name and description for
each picture.
*/
public void loadPictureInformation() {
// Load the file names for the pictures
pictures[0] = "Gregory Lake.JPG";
pictures[1] = "Devils Post Pile.JPG";
pictures[2] = "JMT Sign.JPG";
pictures[3] = "Lonely Tree.JPG";
pictures[4] = "View From Whitney.JPG";
pictures[5] = "Lone Pine Lake.JPG";
// Load the descriptions for each picture
descriptions[0] = "Gregory Lake is located between Yosemite Valley and Reds Meadow pack station on the John Muir Trail in California";
descriptions[1] = "Devils Post Pile was caused by the glaciers. It is part of Yosemite National Park, even though is located south of Yosemite.";
descriptions[2] = "Not all signs on the trail were this nice. This was a nice reminder about the man that the trail was named for.";
descriptions[3] = "The tree seems lonely. It is located approximately 2 days, by foot, before Mt. Whitney.";
descriptions[4] = "Watching the sun rise from the summit of Mt. Whitney, the highest point in the continental US at 14,500 ft.";
descriptions[5] = "The view from our last campsite on the John Muir Trail before reaching the end of the 220 mile backpack.";
}
/**
Create the area for the selecting which picture to display
*/
public void createMenuPanel()
{
// The code needed to react to a radio button being selected
class MenuListener implements ActionListener
{
public void actionPerformed( ActionEvent evt )
{
//String menuName = "";
//menuName = (String) evt.getSource();
System.out.println("DEBUGGING" + (String) evt.getSource());
// mName = menuName;
}
}
class ExitItemListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
/*
ImageIcon imageIcon1 = new ImageIcon(pictures[0]);
Image actualImage1 = imageIcon1.getImage();
ImageIcon imageIcon2 = new ImageIcon(pictures[1]);
Image actualImage2 = imageIcon2.getImage();
ImageIcon imageIcon3 = new ImageIcon(pictures[2]);
Image actualImage3 = imageIcon3.getImage();
ImageIcon imageIcon4 = new ImageIcon(pictures[3]);
Image actualImage4 = imageIcon4.getImage();
ImageIcon imageIcon5 = new ImageIcon(pictures[4]);
Image actualImage5 = imageIcon5.getImage();
ImageIcon imageIcon6 = new ImageIcon(pictures[5]);
Image actualImage6 = imageIcon6.getImage();
*/
// Register the listener
ActionListener listener = new MenuListener();
image1.addActionListener(listener);
image2.addActionListener(listener);
image3.addActionListener(listener);
image4.addActionListener(listener);
image5.addActionListener(listener);
image6.addActionListener(listener);
ActionListener exitListener = new ExitItemListener();
exit.addActionListener(exitListener);
// Create the other components
JPanel myMenuPanel = new JPanel();
myMenuPanel.setLayout(new GridLayout(0, 1));
controlPanel = new JPanel();
controlPanel.setLayout( new GridLayout(0, 1) );
controlPanel.setBorder(loweredbevel);
}
/**
Create the heading area at the top of the window
*/
public void createHeadingPanel() {
headingLabel = new JLabel("<html>Pictures from <br>the John Muir Trail</html>", SwingConstants.CENTER );
headingLabel.setFont(new Font("Comic Sans MS", Font.ITALIC, 24));
instructionsLabel = new JLabel("Select a picture from the left to view", SwingConstants.CENTER);
instructionsLabel.setFont(new Font("Comic Sans MS", Font.ITALIC, 18));
headingPanel = new JPanel();
headingPanel.setLayout(new GridLayout(0, 1));
headingPanel.add(headingLabel);
headingPanel.add(instructionsLabel);
headingPanel.setBackground(Color.LIGHT_GRAY);
}
/**
Create the output area to display the descriptions of the pictures
*/
public void createOutputArea() {
outputTextArea = new JTextArea(descriptions[0], 3, 20); // 3 rows, 50 columns, initially empty
outputTextArea.setEditable(false); // make it be read only
outputTextArea.setLineWrap(true); // allow it to wrap
outputTextArea.setWrapStyleWord(true); // break lines between words
outputTextArea.setBackground(Color.LIGHT_GRAY);
outputTextArea.setForeground(Color.BLACK);
outputTextArea.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
outputTextArea.setBorder(loweredbevel);
}
}
/**
Creates a frame to display pictures in
@author: B.Brenner
@verison: 1/26/2010
*/
import javax.swing.JFrame;
public class ImageViewer {
/**
Create a frame for the GUI. The remainder of the
program is processed by the GUI.
*/
public static void main ( String[] args ){
// Create the frame and set its properties
ImageViewerFrame viewerFrame = new ImageViewerFrame() ;
viewerFrame.setSize( 580, 510 );
viewerFrame.setVisible( true );
viewerFrame.setTitle( "My Image Viewer" );
viewerFrame.setLocationRelativeTo(null); // center the frame on the screen
viewerFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
viewerFrame.setResizable(false); // don't allow the frame to be resized
}
}
So, I'm having trouble getting the actionlistener to draw the image when I click the appropriate image, also I've started getting a null pointer exception. I can't figure out what I'd want to put inside my action listener to respond to the JMenuItem to draw the appropriate image.