Results 1 to 4 of 4
  1. #1
    MountainBoy is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Question Help with an image in JFrame

    I am making a java game and I'm working on the animation it works until I get the error in run time that it will say that it can not have 3 (or four in my case because I started at zero) images in one variable thingy (how professional of me) I have gotten around it by making another variable thingy but it won't show up because I don't have the first image going away I have tried remove() and pictureA.setVisible(false) but it just won't work! HELP! http://www.java-forums.org/images/sm...pe/(sweat).gif
    Code:

    ---------------------in a .java file called "apples"--------------------
    Java Code:
    import javax.swing.JFrame;
    
    class apples{
    
    	public static void main(String[] args) {
    
    		//MenuGui ho = new MenuGui();
    		BackgroundMenu go = new BackgroundMenu();
    
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		go.setSize(1950,1000);
    		go.setVisible(true);
    }
    	}
    ------------------in a .java file called "BackgroundMenu" (were my problem happens) --------------------------
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import java.awt.image.BufferedImage;
    
    
    public class BackgroundMenu extends JFrame{
    
    	 private JLabel picture;
    	 private JLabel pictureA;
    	 private JComboBox boxA;
    	 private JComboBox box;
    	 private JButton menuPlay;
    	 private JButton instructions;
    	 String message01 = "How to play: much like a texted based game this game is turn-based and will suprise you on the way."
    					    + "  On the way you will be faced with decisions. click on the button for your choice! And now , now make your legend! v0.4.2";
    	 String message02 = "You are one of the captured, a group of people with a past against the captors so terrible , they won't tell even you, one"
    	 	                + " of them and the only one who does not remember.";
    	 String message03 = "One dark, Stormy night";
    	 String message04 = "A strange, cloaked figure comes and...";
    	 String message05 = "Takes you out of the cell, clearly against the captor's will";
    	 private static String[] filenameA = {"Resources\\Images\\Opening\\02.png"};
    	 private static String[] filename = {"Resources\\Images\\Menu\\BackGroundMenu.png", "Resources\\Images\\Opening\\01.png", "Resources\\Images\\Opening\\02.png"};
    	 private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), new ImageIcon(getClass().getResource(filename[1])),
    	                         new ImageIcon(getClass().getResource(filename[2]))};
    	 private Icon[] picsA = {new ImageIcon(getClass().getResource(filename[0]))};
    
    	 public BackgroundMenu(){
    	 	super("The Legends v0.4.2");
    	 	setLayout(new FlowLayout());
    
    	 	box = new JComboBox(filename);
    
    
    	 	box.addItemListener(
    	 			new ItemListener(){
    	 				public void itemStateChanged(ItemEvent event){
    	 					if(event.getStateChange()==ItemEvent.SELECTED)
    	 						picture.setIcon(pics[box.getSelectedIndex()]);
    
    	 				}
    	 			}
    	 	);
    	 	picture = new JLabel(pics[0]);
    	 	add(picture);
    
    	 	boxA = new JComboBox(filenameA);
    
    
    	 	boxA.addItemListener(
    	 			new ItemListener(){
    	 				public void itemStateChanged(ItemEvent event){
    	 					if(event.getStateChange()==ItemEvent.SELECTED)
    	 						picture.setIcon(picsA[boxA.getSelectedIndex()]);
    
    	 				}
    	 			}
    	 	);
    	 	pictureA = new JLabel(picsA[0]);
    	 	add(pictureA);
    	 	pictureA.setVisible(false);
    
    
    
    
    
    
    	 	setLayout(new FlowLayout());
    		//Buttons down here!!!
    	 	menuPlay = new JButton("Play");
    	 	add(menuPlay);
    	 	menuPlay.setVisible(true);
    	 	instructions = new JButton("Instructions");
    	 	add(instructions);
    	 	instructions.setVisible(true);
    	 	//adds the idea of an action listener
    	 	//|
    	 	//|
    	 	//|
    	 	//V
    	 	InstructionClass instructionmessage = new InstructionClass();
    	 	BeginClass begincode = new BeginClass();
    		menuPlay.addActionListener(begincode);
    		instructions.addActionListener(instructionmessage);
    
    
    
    	}
    		private class InstructionClass implements ActionListener{
    			public void actionPerformed(ActionEvent event){
    				JOptionPane.showMessageDialog(null, String.format("%s", message01));
    			}
    		}
    		private class BeginClass implements ActionListener{
    			public void actionPerformed(ActionEvent event) {
    						instructions.setVisible(false);
    						menuPlay.setVisible(false);
    						picture.setIcon(pics[1]);
    						JOptionPane.showMessageDialog(null, String.format("%s", message02));
    						picture.setIcon(pics[2]);
    						JOptionPane.showMessageDialog(null, String.format("%s", message03));
    						picture.setIcon(picsA[3]);
    						JOptionPane.showMessageDialog(null, String.format("%s", message04));
    					pictureA.setVisible(true);
    					picture.setVisible(false);
    					remove(picture);
    					Game play = new Game();
    					play.beginGame();
                                            //the Game stuff is to trigger a game class (you can make it a comment if you want to compile and run it yourself)  
    					}
    				}
    			}
    Last edited by Fubarable; 07-14-2012 at 05:11 PM. Reason: code tags added

  2. #2
    MountainBoy is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with an image in JFrame

    LOL the sweat face appeared as a link when I had smilyes on :)
    Last edited by MountainBoy; 07-14-2012 at 04:25 PM.

  3. #3
    MountainBoy is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with an image in JFrame

    just fixed the first post :) I had tried to correct it here.
    Last edited by MountainBoy; 07-14-2012 at 04:27 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. How to add an image to JFrame?
    By gyijhbk in forum New To Java
    Replies: 2
    Last Post: 06-19-2012, 12:04 PM
  2. Image Dimensions in JFrame
    By BrooksyFC in forum Java 2D
    Replies: 5
    Last Post: 03-29-2012, 03:25 PM
  3. need help JFrame+image
    By The Brain Freak in forum New To Java
    Replies: 1
    Last Post: 02-05-2012, 03:03 PM
  4. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  5. JFrame + image
    By Blacknight in forum New To Java
    Replies: 2
    Last Post: 05-07-2009, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •