Jump to content

  • Log In with Google      Sign In   
  • Create Account

JMenu, Listeners, and drawing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 ThinkingsHard   Members   -  Reputation: 218

Like
0Likes
Like

Posted 09 May 2014 - 08:17 PM


/**
	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.

 



Sponsor:

#2 ThinkingsHard   Members   -  Reputation: 218

Like
0Likes
Like

Posted 09 May 2014 - 09:09 PM

Updated version of the first class. Now my only issue is that it doesn't display the images.

/**
	This frame will display a picture that the user
	selects using radio buttons.

	@author: Harv
	@version:  5/8/14 - 5/9/14
 */

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

	JMenu fileMenu = new JMenu("File");
	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("Exit");
	JMenuItem about = new JMenuItem("About");

	// constructor
	public ImageViewerFrame(){

		// Helper methods
		loadPictureInformation();
		createHeadingPanel();
		createOutputArea();
		createMenuPanel();

		// 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);
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		menuBar.add(fileMenu);
		menuBar.add(imageMenu);
		menuBar.add(aboutMenu);
		

		imageMenu.add(image1);
		imageMenu.add(image2);
		imageMenu.add(image3);
		imageMenu.add(image4);
		imageMenu.add(image5);
		imageMenu.add(image6);
		fileMenu.add(exit);
		aboutMenu.add(about);
		
		
		
		

	}
	class ImageListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{

			JMenuItem imageName;
			imageName = (JMenuItem)event.getSource();

			/**
			 * Finds what the imagename equals
			 * then finds that image and displays it
			 */

			if(imageName.getText().equals(image1)) 
			{
				outputTextArea.replaceRange(descriptions[0], 0, outputTextArea.getText().length());
				pictureLabel.setIcon(new ImageIcon("images/" + pictures[0])); 
			}
			else if(imageName.getText().equals(image2))
			{
				outputTextArea.replaceRange(descriptions[1], 0, outputTextArea.getText().length());
				pictureLabel.setIcon(new ImageIcon("images/" + pictures[1]));
			}
			else if(imageName.getText().equals(image3))
			{
				outputTextArea.replaceRange(descriptions[2], 0, outputTextArea.getText().length());
				pictureLabel.setIcon(new ImageIcon("images/" + pictures[2]));
			}
			else if(imageName.getText().equals(image4))
			{
				outputTextArea.replaceRange(descriptions[3], 0, outputTextArea.getText().length());
				pictureLabel.setIcon(new ImageIcon("images/" + pictures[3]));
			}
			else if(imageName.getText().equals(image5))
			{
				outputTextArea.replaceRange(descriptions[4], 0, outputTextArea.getText().length());
				pictureLabel.setIcon(new ImageIcon("images/" + pictures[4]));
			}
			else if(imageName.getText().equals(image6))
			{
				outputTextArea.replaceRange(descriptions[5], 0, outputTextArea.getText().length());
				pictureLabel.setIcon(new ImageIcon("images/" + pictures[5]));
			}
		}
	}

	/**
		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.";
	}

	class ExitItemListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			System.exit(0);
		}
	}

	/**
		Create the area for the selecting which picture to display
	 */
	public void createMenuPanel()
	{	
		// Register the listener
		ImageListener listener = new ImageListener();

		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);
		ActionListener aboutListener = new AboutListener();
		about.addActionListener(aboutListener);
		// Create the other components
		JPanel myMenuPanel = new JPanel();
		myMenuPanel.setLayout(new GridLayout(0, 1));

	}


	class AboutListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			JOptionPane.showMessageDialog(null,"Harvey version: ohgodhowmanyversionsdidIdo?");
		}
	}
	/**
		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 menu 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);
	}
}



#3 Glass_Knife   Moderators   -  Reputation: 4452

Like
1Likes
Like

Posted 09 May 2014 - 09:16 PM

Did you stop getting null pointers?  Some more information is really helpful here.  
What happens when you try to draw the image?  If there is an exception, post a copy of the stack trace.

 

Without trying out the code, I will take a guess:

 

After you set the new image, you need to call repaint() on the frame to trigger an update.

You can test if this is the problem by minimizing the window, and then displaying it again.  This will trigger a repaint of the JFrame.  If that isn't the issue, then it may have something to do with the JLabel not resizing correctly with the new image.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#4 ScoreX   Members   -  Reputation: 344

Like
1Likes
Like

Posted 10 May 2014 - 03:16 AM

In the actionPerformed method of the ImageListener class, you are comparing the text of the event source to the JMenuItem object, instead of the text of the object.

 

Instead of

if(imageName.getText().equals(image1))
{
    outputTextArea.replaceRange(descriptions[0], 0, outputTextArea.getText().length());
    pictureLabel.setIcon(new ImageIcon("images/" + pictures[0]));
}

Try

if(imageName.getText().equals(image1.getText()))
{
    outputTextArea.replaceRange(descriptions[0], 0, outputTextArea.getText().length());
    pictureLabel.setIcon(new ImageIcon("images/" + pictures[0]));
}


#5 ThinkingsHard   Members   -  Reputation: 218

Like
0Likes
Like

Posted 10 May 2014 - 08:41 AM

Did you stop getting null pointers?  Some more information is really helpful here.  
What happens when you try to draw the image?  If there is an exception, post a copy of the stack trace.

 

Without trying out the code, I will take a guess:

 

After you set the new image, you need to call repaint() on the frame to trigger an update.

You can test if this is the problem by minimizing the window, and then displaying it again.  This will trigger a repaint of the JFrame.  If that isn't the issue, then it may have something to do with the JLabel not resizing correctly with the new image.

There is no exception or errors. I tried minimizing. I'm going to look into the JLabel. Thanks.



#6 Glass_Knife   Moderators   -  Reputation: 4452

Like
0Likes
Like

Posted 10 May 2014 - 09:03 AM

By the way, if you haven't done much Swing programming, it is very much like debugging a graphics program.  You have to take very small steps when doing something you've never done before.  If you try to code everything up at once, with a bunch of images, optimized to be easy to read, but you haven't made a hacky prototype, and it doesn't work, you'll have no clue what is wrong.

 

Every time I do something new with Swing I make a simple prototype to throw away so I can explore how the control works.  


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#7 ThinkingsHard   Members   -  Reputation: 218

Like
0Likes
Like

Posted 10 May 2014 - 09:23 AM

I would have done it that way, except this was a project that was given to us to have us convert radiobuttons over to JMenuItems.

 

For my Final I've done things that way, where I get one thing working, before trying to get anything else working.



#8 ThinkingsHard   Members   -  Reputation: 218

Like
0Likes
Like

Posted 10 May 2014 - 07:27 PM

Solved... my images were not in the folder 'images'. >.<.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS