Results 1 to 4 of 4
  1. #1
    JoePenguin is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default adding different images using if statements

    i have decided to create a dictionary for minecraft.
    i would like it so when the user inputs the name of an item in minecraft into a text field, a description and picture will show.
    i can get the descriptions showing easily but the images... not so easy. i decided to create another program just to try and do it before adding to the code i already have so that i don't mess it up somehow >.<
    the new program was just to show an image depending on what the user inputs. for example to show a picture of a banana when "banana" was entered. and to save time, i used a scanner instead of a JTextField to get user input for each picture (this shouldn't effect it?).

    the problem i am having is that images aren't showing up at all. here is the code i have just to show 3 images depending on the input.

    Java Code:
    import java.util.Scanner;
    import javax.swing.*;
    import java.awt.*;
    
    public class main {
    JFrame frame1 = new JFrame();
    JPanel panel1 = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    Scanner joe = new Scanner(System.in);
    	
    	public void frames(){
    		frame1.setTitle("pictures");
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame1.setSize(500,500);
    		frame1.setVisible(true);
    		
    		panel1.setBackground(Color.black);
    		frame1.add(panel1);
    	}
    	public void ifs(){
    		if(joe.equals("banana")){
    		
    		JLabel label1 = new JLabel();
    		label1.setIcon(new ImageIcon("banana.png"));
    	c.gridx = 0;
    	c.gridy = 0;
    	panel1.add(label1, c);
    	}
    	
    		else if(joe.equals("picture 2")){
    		JLabel label2 = new JLabel();
    		label2.setIcon(new ImageIcon("minecraft1.png"));
    		c.gridx = 0;
    		c.gridy = 0;
    		panel1.add(label2, c);
    }
    		else{
    			JLabel label3 = new JLabel();
    			label3.setIcon(new ImageIcon("topofitems.png"));
    			c.gridx = 0;
    			c.gridy = 0;
    			panel1.add(label3, c);
    		}
    	}
    
    
    //main method run
    
    public static void main(String[]args){
    	main j = new main();
    	j.frames();
    }
    }
    i have also tried creating a jlabel before the if statement and just adding it within each if statement with a different image. anyone know what im doing wrong? probably painfully simple but ya'know :)

    sorry if the code is messy, im new so havent really learnt how to lay it out.

    thanks for your time and effort :D
    Last edited by Norm; 02-16-2012 at 03:25 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,875
    Rep Power
    25

    Default Re: adding different images using if statements

    images aren't showing up at all
    Are you sure you have the correct path to the images?

    Create a File object and print its path.

    How do you execute the code that loads the images?
    Where is the ifs method called?

    You need to look at how to use the Scanner class.
    It has methods you call to read input from the user.
    Last edited by Norm; 02-16-2012 at 03:27 AM.

  3. #3
    JoePenguin is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: adding different images using if statements

    where is the ifs method called?
    this was the problem thanks :D
    i had it in my head for some reason that if i called the frames method, it would call anything after that. i see that illogical now i think about it and feel like a pumpkin?
    anyway, cheers norm :) +rep for you <3

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,875
    Rep Power
    25

    Default Re: adding different images using if statements

    Don't forget to look at the Scanner class's methods.

Similar Threads

  1. Arrays and For statements for Images in Applet
    By SummitWhiteMaro in forum Java Applets
    Replies: 0
    Last Post: 03-07-2011, 04:59 AM
  2. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 07:19 AM
  3. Adding multiple images to frame
    By 435.mahesh in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2009, 09:31 PM
  4. [new] need help on GUI adding images
    By counterfox in forum AWT / Swing
    Replies: 5
    Last Post: 03-19-2009, 11:29 AM
  5. if else statements and using images
    By Joshr in forum New To Java
    Replies: 8
    Last Post: 10-05-2008, 07:51 PM

Tags for this Thread

Posting Permissions

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