Results 1 to 1 of 1

Thread: setIcon

  1. #1
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default setIcon

    Java Code:
    import javax.swing.*;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.util.*;
    
    
    
    class RPGContents extends JPanel
    {
    	RPGContents()
    	{  
    		
     		boolean flagA=true;
    		boolean flagB=true;
    		String name;
    		name = null;
    		int name_length;
    		Integer age1;
    		age1 = null;
    		while(flagA)
    		{
    			String inputString  = JOptionPane.showInputDialog(null,"What is the name of your character?");
    			Scanner keyboard = new Scanner (inputString);
    			name = keyboard.next();
    			name_length=name.length();
    			if (name_length>=3 && name_length<=12)
    			{
    			flagA=false;
    			}else
    			{
    			flagA=true;
    			}
    			while(flagB)
    			{
    			String age  = JOptionPane.showInputDialog(null,"What is the age of your character?");
    			Scanner keyboard1 = new Scanner (age);	
    			age1 = keyboard1.nextInt();
    			if (age1>=18 && age1<=72)
    			{
    			flagB=false;
    			}else
    			{
    			flagB=true;
    			}
    			}
    	}
    		String [] shop={"Armor 40 gp", "Weapon 30 gp", "Potion 15gp", "Leave store"};
    		int gold =100;
    		do {
    		int choice = JOptionPane.showOptionDialog(null,
    	                            "Select purchase you have " + gold + "gp",
    	                            "SHOP",
    			                     JOptionPane.YES_NO_CANCEL_OPTION, 
    			                     JOptionPane.QUESTION_MESSAGE, 
    			                     null,
    			                     shop,
    			                     shop[0]);
    		if (choice == JOptionPane.YES_OPTION) {
    			gold = gold - 40;
    		} else if (	choice == JOptionPane.NO_OPTION) {
    			gold = gold - 30;
    		} else if (	choice == JOptionPane.CANCEL_OPTION) {
    			gold = gold - 15; 
    		} else if (	choice == JOptionPane.CLOSED_OPTION) {
    			break; }
    
    		}while (gold>0);
    	
    	
    	
    		
    		
    		this.setLayout(new BorderLayout());
    		JPanel profile = new JPanel();
    		JLabel labelProf = new JLabel();
    		Icon pic1 = new ImageIcon ("IMAGES/portrait.JPG");		
    		labelProf.setIcon (pic1);
    		labelProf.setHorizontalTextPosition(SwingConstants.CENTER);
    		labelProf.setVerticalTextPosition(SwingConstants.BOTTOM);
    		labelProf.setText(" " + name + "(" + age1 + ")");
    		profile.add(labelProf);
    		this.add(profile, BorderLayout.NORTH);
    		
    		JPanel stats = new JPanel();
    		JLabel labelStat = new JLabel();	
    		stats.setLayout(new GridLayout( 4, 2, 2, 4));
    		labelStat.setVerticalTextPosition(SwingConstants.CENTER);
    		labelStat.setHorizontalTextPosition(SwingConstants.LEFT);
    		
    		
           
    		Random randGen = new Random (); 				
    		int Strength = randGen.nextInt(16) + 3;
    		int Intelligence = randGen.nextInt(16) + 3;
    		int Constitution = randGen.nextInt(16) + 3;
    		int Agility = randGen.nextInt(16) + 3;	
    		
    		JLabel label1 = new JLabel("Strength :" + Strength );
          	JLabel label2 = new JLabel("Intelligence :" + Intelligence);
            JLabel label3 = new JLabel("Constitution :" + Constitution);
            JLabel label4 = new JLabel("Agility :" + Agility);   	     	   	
          	JLabel label5 = new JLabel("Attack :");
           	JLabel label6 = new JLabel("Armour :");
          	JLabel label7 = new JLabel("Health :");
           	JLabel label8 = new JLabel("Gold :"  );    
          	
          	stats.add(label1);
          	stats.add(label2);
          	stats.add(label3);
          	stats.add(label4);
          	stats.add(label5);
          	stats.add(label6);
          	stats.add(label7);
          	stats.add(label8);
          	this.add(stats, BorderLayout.CENTER);   	
          	
    		JPanel inventory  = new JPanel();		
    		JLabel labelInvent = new JLabel();
    		inventory.setLayout(new GridLayout( 2, 4, 1, 1 ));
    		
    		
    
    	JLabel label10 = new JLabel();
    	label10.setIcon(new ImageIcon ("IMAGES/empty.png"));
          	JLabel label11 = new JLabel();
          	label11.setIcon(new ImageIcon ("IMAGES/empty.png"));
            JLabel label12 = new JLabel();
            label12.setIcon(new ImageIcon ("IMAGES/empty.png"));
            JLabel label13 = new JLabel();   	     	   	
            label13.setIcon(new ImageIcon ("IMAGES/empty.png"));
          	JLabel label14 = new JLabel();
          	label14.setIcon(new ImageIcon ("IMAGES/empty.png"));
           	JLabel label15 = new JLabel();
           	label15.setIcon(new ImageIcon ("IMAGES/empty.png"));
          	JLabel label16 = new JLabel();
          	label16.setIcon(new ImageIcon ("IMAGES/empty.png"));
           	JLabel label17 = new JLabel();
           	label17.setIcon(new ImageIcon ("IMAGES/empty.png"));
          	
    		inventory.add(label10);
    		inventory.add(label11);
          	inventory.add(label12);
          	inventory.add(label13);
          	inventory.add(label14);
          	inventory.add(label15);
          	inventory.add(label16);
          	inventory.add(label17);
          	this.add(inventory, BorderLayout.SOUTH);
          	
    	
    	}
    		
    }
    heres a problem, that i stuck
    For every piece of armour the character owns, add a JLabel to the inventory JPanel which displays the following image – “IMAGES\armour.png”.
    i have to use setIcon method.
    So if i bought 2 armours(for example) how i have to declare it, that it will be visible in inventory label?

    p.s “IMAGES\armour.png” must appear instead
    label10.setIcon(new ImageIcon ("IMAGES/empty.png"));
    (for example)
    Last edited by p0rnstar; 01-28-2010 at 06:20 PM.

Similar Threads

  1. setIcon to jbutton
    By panic in forum New To Java
    Replies: 5
    Last Post: 05-15-2009, 06:33 PM
  2. setIcon
    By pgag45 in forum New To Java
    Replies: 2
    Last Post: 05-15-2009, 08:12 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
  •