• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ThinkingsHard

JMenu, Listeners, and drawing

7 posts in this topic


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

 

0

Share this post


Link to post
Share on other sites

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);
	}
}

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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]));
}
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.  

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0