Results 1 to 10 of 10
  1. #1
    proceeded is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Need help with ImageIcon and int

    Hi everyone,
    Im making a Memory game and i need to add images to the buttons. i also use a int so it will randomise it. but this is my problem:

    this will add the image and goes correct now i need to add the int with it:
    ImageIcon img = new ImageIcon(this.getClass().getResource("mountain.gi f"));

    This wont work because i'm getting a nullPointerException:
    ImageIcon img = new ImageIcon(this.getClass().getResource("no[0]+mountain.gif"));

    Also if i do this:
    ImageIcon img = new ImageIcon(no[0]+ "" + this.getClass().getResource("mountain.gif"));

    then it works but it doesnt show the image

    How could i add the no[0] (int) to the image so it will randomise it?!

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

    Default

    One way is to have an ArrayList<ImageIcon> of ImageIcons, shuffle the list via Collections.shuffle(myImageIconList), and then use a for loop to add icons to the JButtons (which for ease can also be held within an array or ArrayList<JButton>.

  3. #3
    proceeded is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    ok, thanks for information i will give it a try.

    And again thanks in front

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

    Default

    Also, your list can hold two references to the same ImageIcon by simply adding the icon twice to the List. Then to compare if one button holds the same image as another button, you can simply compare the icons... if icon1.equals(icon2), and you'll know if they match.

  5. #5
    proceeded is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    ok thanks,

    i got this now but i wont shuffle the images what am i doing wrong?

    initPictureIcons(); // this is a class which has the path

    ArrayList<ImageIcon> image = new ArrayList<ImageIcon>();
    for(int i = 0; i < 16; i++)
    {
    image.add(pictureIcon[i]);
    }

    Collections.shuffle(image);


    for (int i = 0; i < gameBtn.length; i++) {
    gameBtn[i] = new JButton(pictureIcon[i]);
    gameBtn[i].setFont(new Font("Serif", Font.BOLD, 28));
    gameBtn[i].addActionListener(new KnopHandler());
    }

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

  7. #7
    proceeded is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Ok my images will shuffle now when i clicked now my last question is: Iwhen 2 images are the same then u got it right mostly i would do this with an IF construction but it wont work. My code also works with a int so that is also a problem because an image can never be an int. How could i accomplish that it checks if 2 images are the same?

    Java Code:
    package MemoryGame;
    
    import static java.util.Collections.shuffle;
    
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.Panel;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    
    import javax.swing.*;
    
    public class OpdrachtPaneel extends JPanel
    {
    	private static final long serialVersionUID = 1L;
    	private JButton exitBtn, replayBtn;
    	private JButton[] gameBtn = new JButton[16];
    	private ArrayList<Integer> gameList = new ArrayList<Integer>();
    	private int counter = 0;
    	private int[] btnID = new int[2];
    	private int[] btnValue = new int[2];
    	ImageIcon[] pictureIcon = new ImageIcon[16];
    	ArrayList<ImageIcon> image = new ArrayList<ImageIcon>();
    
    	public OpdrachtPaneel() 
    	{	
    		initPictureIcons();
    
    		for (int i = 0; i < gameBtn.length; i++) {
    			gameBtn[i] = new JButton(pictureIcon[i]);
    			gameBtn[i].setFont(new Font("Serif", Font.BOLD, 28));
    			gameBtn[i].addActionListener(new KnopHandler());
    		}
    		
    		exitBtn = new JButton("Exit");
    		exitBtn.addActionListener(new KnopHandler());
    		replayBtn = new JButton("Replay");
    		replayBtn.addActionListener(new KnopHandler());
    		
    		Panel gamePnl = new Panel();
    		gamePnl.setLayout(new GridLayout(4, 4));
    		for (int i = 0; i < gameBtn.length; i++) 
    		{
    			gamePnl.add(gameBtn[i]);
    		}
    		add(gamePnl, BorderLayout.CENTER);
    		
    		Panel buttonPnl = new Panel();
    		buttonPnl.add(replayBtn);
    		buttonPnl.add(exitBtn);
    		buttonPnl.setLayout(new GridLayout(1, 0));
    
    		add(buttonPnl, BorderLayout.SOUTH);
    		setArrayListText();
    	}
    	//gets the icons
    	public void initPictureIcons(){
    		pictureIcon[0]=new ImageIcon(this.getClass().getResource("kwal.gif"));
    		pictureIcon[1]=new ImageIcon(this.getClass().getResource("mountain.gif"));
    		pictureIcon[2]=new ImageIcon(this.getClass().getResource("flower.gif"));
    		pictureIcon[3]=new ImageIcon(this.getClass().getResource("vuurtoren.gif"));
    		pictureIcon[4]=new ImageIcon(this.getClass().getResource("koala.gif"));
    		pictureIcon[5]=new ImageIcon(this.getClass().getResource("tulpen.gif"));
    		pictureIcon[6]=new ImageIcon(this.getClass().getResource("struik.gif"));
    		pictureIcon[7]=new ImageIcon(this.getClass().getResource("pinguins.gif"));
    		pictureIcon[8]=new ImageIcon(this.getClass().getResource("kwal.gif"));
    		pictureIcon[9]=new ImageIcon(this.getClass().getResource("vuurtoren.gif"));
    		pictureIcon[10]=new ImageIcon(this.getClass().getResource("tulpen.gif"));
    		pictureIcon[11]=new ImageIcon(this.getClass().getResource("pinguins.gif"));
    		pictureIcon[12]=new ImageIcon(this.getClass().getResource("koala.gif"));
    		pictureIcon[13]=new ImageIcon(this.getClass().getResource("flower.gif"));
    		pictureIcon[14]=new ImageIcon(this.getClass().getResource("mountain.gif"));
    		pictureIcon[15]=new ImageIcon(this.getClass().getResource("struik.gif"));
    	}
    	
    	
    	class KnopHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e) 
    		{
    			if (exitBtn == e.getSource()) {
    				System.exit(0);
    			}
    			if (replayBtn == e.getSource()) {
    
    			}
    			for (int i = 0; i < gameBtn.length; i++) {
    				if (gameBtn[i] == e.getSource()) {
    					gameBtn[i].setText("" + gameList.get(i));
    					gameBtn[i].setIcon(image.get(i));
    					gameBtn[i].setEnabled(false);
    					counter++;		
    					
    					if (counter == 3) {
    						if (sameValues()) {
    							gameBtn[btnID[0]].setEnabled(false);
    							gameBtn[btnID[1]].setEnabled(false);
    						} else {
    							gameBtn[btnID[0]].setEnabled(true);
    							gameBtn[btnID[0]].setText("");
    							gameBtn[btnID[1]].setEnabled(true);
    							gameBtn[btnID[1]].setText("");
    						}
    						counter = 1;
    					}
    					if (counter == 1) {
    						btnID[0] = i;
    						btnValue[0] = gameList.get(i);
    					}
    					if (counter == 2) {
    						btnID[1] = i;
    						btnValue[1] = gameList.get(i);
    					}
    				}
    			}
    	}
    	}
    
    	public void panel() 
    	{
    		Panel buttonPnl = new Panel();
    		buttonPnl.add(replayBtn);
    		buttonPnl.add(exitBtn);
    		buttonPnl.setLayout(new GridLayout(1, 0));
    
    		
    		add(buttonPnl, BorderLayout.SOUTH);
    	}
                   //add the icons in the array (and the int in its own array) and shuffles it ( the int will be different as the image and it listens to int right now)
    	public void setArrayListText() {
    		for (int i = 0; i < 2; i++) {
    			for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++) {
    				gameList.add(ii);
    				image.add(pictureIcon[ii]);
    			}
    		}
    		
    		Collections.shuffle(image);
    		Collections.shuffle(gameList);
    
    		// ///////////////////
    		int newLine = 0;
    		int nieuwlijn = 0;
    		for (int o = 0; o < image.size(); o++)
    		{
    			nieuwlijn++;
    			System.out.print(""+ image.get(o));
    			if(nieuwlijn == 0)
    			{
    				System.out.println();
    				nieuwlijn = 0;
    			}
    		}
    		for (int a = 0; a < gameList.size(); a++) {
    			newLine++;
    			System.out.print(" " + gameList.get(a));
    			if (newLine == 4) {
    				System.out.println();
    				newLine = 0;
    			}
    		}
    	}
    
    	public boolean sameValues() {
    		if (btnValue[0] == btnValue[1]) {
    			return true;
    		}
    		return false;
    	}
    }
    Last edited by proceeded; 01-23-2011 at 07:43 PM.

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

    Default

    Just for clarity's sake, I would try to use different ActionListeners for the different buttons, meaning one ActionListener for exit, one for new game or reset, and the exception being one single ActionListener for the actual image buttons combined. In the image button ActionListener, I'd have an Icon variable, say called firstIcon, that I'd set to null. When a button is pressed, I'd check if firstIcon were null, and if so, the user is pressing the first button and I'd pass a reference from the pressed button's Icon to this button. If it wasn't null, then the user was pressing the second button and I'd compare its Icon with the Icon held by the firstIcon variable in an if block which would tell me if the Icons were the same. I'd then set firstIcon back to null to reset the state of the listener so that it would expect a first button press again.

  9. #9
    proceeded is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    ok thanks. im off to work then ;)

  10. #10
    proceeded is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Edit:
    i solved my whole problem.

    gameBtn.getSource(e); works so that is less code.

    I made a ImageIcon array with 2 imageicons in it. I compare image 1 with image 2 and that will go like this:
    when i clicked button 1 then get that image.
    when i clicked button 2 then get that image

    compare button 1's image with button 2's image and if those equals then keep those buttons disabled.


    My problem was that i wanted to equal the buttons but they will never equal because every button has a different name.

    thanks for the response though (Y)

    Greetings,

    Proceeded
    Last edited by proceeded; 01-25-2011 at 06:43 PM.

Similar Threads

  1. ImageIcon Transparency
    By Lingerz in forum New To Java
    Replies: 2
    Last Post: 06-04-2010, 02:53 PM
  2. ImageIcon HELP!
    By ben1989 in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 08:48 PM
  3. ImageIcon background
    By Yannick in forum New To Java
    Replies: 11
    Last Post: 02-12-2009, 04:13 PM
  4. send imageIcon over RMI
    By nadia in forum Advanced Java
    Replies: 4
    Last Post: 01-06-2009, 03:41 PM
  5. ImageIcon and size
    By wokena in forum Java 2D
    Replies: 3
    Last Post: 12-21-2008, 08:47 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
  •