Results 1 to 8 of 8
  1. #1
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Memory game - swing

    Hi. :)
    I am working on game called "Memory". I am quite sure you all know what that game is. Now I would like to make like return statement when I click on red box (JLabel). It should return me its own text (getText). And the problem is, I don't know how. Or if somebody should know better way to code this game please share it :).
    Thanks


    Java Code:
    import java.awt.Color;
    import java.awt.event.*;
    import java.awt.Font;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.EventListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    public class memory {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		JFrame prozor = new window();
    		int MAX = 10;
    		ArrayList <Integer> brojevi = new ArrayList<Integer>();
    		for(int a = 0;a<2;a++){
    			for(int i =0;i<MAX;i++){
    				brojevi.add(i);
    			}
    		}
    		Collections.shuffle(brojevi);
    		System.out.println(brojevi);
    		JLabel[] test = new JLabel[MAX*2];
    		int x = 90, y=0,i=0;
    		for(int row = 0;row<4;row++){
    			for(int cell = 0;cell<5;cell++){
    				test[i] = new prostor(Integer.toString(brojevi.get(i)));
    				test[i].setLocation(x,y);
    				prozor.getContentPane().add(test[i]);
    				y = y+90;
    				i++;
    			}
    			
    			x=x+90;
    			y = 0;
    		}
    		
    	}
    
    }
    
    
    class window extends JFrame{
    	
    	public window(){
    		setTitle("Memory for Vikica");
    		setSize(500,500);
    		setVisible(true);
    		setResizable(false);
    		setLayout(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    
    class prostor extends JLabel implements MouseListener{
    private String  what ="";
    	public prostor(String w){
    		what =w;
    		setSize(70,70);
    		setFont(new Font("Arial",Font.CENTER_BASELINE,40));
    		setOpaque(true);
    		setBackground(Color.RED);
    		addMouseListener(this);
    		setVerticalTextPosition(JLabel.CENTER);
    	}
    	
    	public void mouseClicked(MouseEvent arg0) {
    		
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		setText(what); //HOW COULD I MAKE LIKE RETURN STATEMENT HERE?
    		
    	}
    
    
    	
    }
    Last edited by Vizualni; 07-27-2010 at 02:02 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by Vizualni View Post
    Java Code:
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		setText(what); //HOW COULD I MAKE LIKE RETURN STATEMENT HERE?
    You can't! What are you trying to do in the first place?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    When I click on label it set text to private variable what (it's some random number). I am trying to retrive private variable what when I click on that JLabel.
    for example:
    click on: test[2], test[6].
    if(test[2].getText()==test[6].getText())then remove test[2] and test[6]
    else

    test[2].setText("") and test[6].setText("")

    I am rly bad at explaining. I am sorry.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Then you also need a mouseListener on your 'game panel' that gets the text from the flipped JLabels. Remember that you also need a mechanism to hide them again, if they did not match.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    Well the problem is I don't know how to do that. Could you please help me? :)

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    How to do what? Post your best attempt and ask a specific question. We all here are willing to help you, but we won't do it for you.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    No no. I am sorry. I didn't think that you write me whole code.
    Okay. So, the question is:How can I get get text of the label I've clicked on? Or at least how can I get some pointer to label I've clicked. So then I could access that "what" variable or just getText().

    Vizualni

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    You have a class that encapsulates all the prostor instances, right? Add this class as a MouseListener to all prostor instances. In mouseClicked you can get the instance via MouseEvent.getSource() and can call it's getText() method.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Memory game
    By ronyyy in forum New To Java
    Replies: 9
    Last Post: 04-15-2010, 12:00 PM
  2. memory game in JAVA
    By lclclc in forum New To Java
    Replies: 19
    Last Post: 10-18-2009, 05:41 PM
  3. Help to make memory game :=)
    By arian88 in forum AWT / Swing
    Replies: 7
    Last Post: 10-15-2009, 07:23 AM
  4. Memory Game
    By torres in forum AWT / Swing
    Replies: 6
    Last Post: 04-22-2009, 12:00 AM
  5. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-14-2009, 12:39 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
  •