Page 1 of 3 123 LastLast
Results 1 to 20 of 45
  1. #1
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default Have been stuck up coding the GUI for Memory Card Game

    Hi. Could anybody help me out with my project?
    I have been trying to figure out how I should code but just can't find some examples on the net.
    So far, I have just made this..

    Java Code:
      
    And I have been stuck up doing this. Matter is, it doesn't even give me a constant output as well. Sometimes, the image shows first, sometimes, the buttons do. I really am confused.. Help please..Thanks..
    Last edited by Galore; 10-21-2009 at 03:20 PM. Reason: code tags added

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

    Default

    Galore: welcome to the forum! I've taken the liberty of adding code tags to your post to make the code more readable, but you may wish to edit the post yourself and re-post code that is indented. Fact is, the easier it is for others to read your post and your code, the better your chances of getting a decent reply.

    Much luck!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    And also please clearly specify what's your question is. Reading a long post is quite difficult.

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

    Default

    Also:

    1) Use a JPanel here not a JFrame. This way, you can use this code in a JDialog if desired (and yep, it probably should be displayed as a JDialog).
    2) If using a JPanel, then override paintComponent, not paint.
    3) In your paintComponent override, be sure to call super.paintComponent(g); as the first line of the method. This allows the paintComponent to paint its background.
    4) Avoid calling setSize but instead call setPreferredSize and then pack your app.
    5) Learn to use the layout managers; they're your friends.

  5. #5
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    @Fubarable: Thanks for your response. I have been watching your responses to a couple of forums recently and just realized how big you have helped them, in fact, I really had been trying to post my own concern only that I could not do so. I was not aware how I should exactly deal about it. Thanks to Eranga, another expert, whose responses I've been watching as well. She taught me how to post this thread.
    Anyway,
    My first concern is, I should make a GUI for the project.
    At the onset of the game, a prompt message should be shown. The message would welcome the user who would play the game. A subsequent message would be shown asking the user whether or not he/she is ready to play. If the user chose to play, another subsequent message would be shown asking a certain question or trivia which could vary depending on the instance and the set of cards to be dealt with. If the user chose not to play, there would be a prompt message saying "Thanks for visiting Memo!" and the program would terminate.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FindingMemo2{
             public static void main(String[] args){
                      Memo frame = new Memo();
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setVisible(true);
    				 
             }
    }
    class Memo extends JFrame implements ActionListener{
    	Image mypic;
    	JLabel label;
    	JRadioButton yes,no;
    	ButtonGroup radioGroup;
             public Memo(){
    		Container c= getContentPane();
    		c.setLayout(null);
    		 label=new JLabel("Would You Like to Play?");
    		 radioGroup=new ButtonGroup();//ASSURES THAT ONLY ONE IS SELECTED
    		 yes=new JRadioButton("Yes", true);//TRUE IF SELECTED
    		 no=new JRadioButton("No", false);//FALSE IF SELECTED
    		//ButtonGroup.addActionListener(this);
    		yes.addActionListener(this);
    		no.addActionListener(this);
    		 radioGroup.add(yes);//INSURES THAT ONLY ONE IS CHOSEN
    		 radioGroup.add(no);
    		 label.setBounds(430,250,150,50);
    		 yes.setBounds(380,300,180,20);
    		 no.setBounds(560,300,200,20);
    		 c.add(label);//PLACES IN THE CONTAINER
    		 c.add(yes);//PLACES IN THE CONTAINER
    		 c.add(no);//PLACES IN THE CONTAINER
    				
    						 
    			mypic=Toolkit.getDefaultToolkit().getImage("5.jpg");
             // get screen dimensions
             Toolkit kit = Toolkit.getDefaultToolkit();//returns something of type dimension
             Dimension screenSize = kit.getScreenSize();
             int screenHeight = screenSize.height;
             int screenWidth = screenSize.width;
             // center frame in screen
             setSize(screenWidth, screenHeight);//half the size of whole=screenwidth/2
             //setLocation(screenWidth / 4, screenHeight / 4);//this will be used if screenwidth/2
    		 }
    		 public void paint(Graphics g){//g= acts like a pen
    		g.drawImage(mypic, 3,20,this);
    		}
    		 
    
    		public void actionPerformed(ActionEvent e){
                        if(e.getSource()==yes){
    						JOptionPane.showMessageDialog(null,"Welcome to Finding Memo!");
    						//startGame();
    				
    				
    				
    	
    	
    	
    	
    						
    						
    
    					}else { 
    					    JOptionPane.showMessageDialog(null,"Thanks for Visiting Memo!");
    						System.exit(0);
    					}
    	}
    
    		
            
    }
    I have been stuck up with this. I have been making this since last week but really could not move on.
    I hope you could help me get this through..
    Last edited by Galore; 10-17-2009 at 08:00 PM.

  6. #6
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    This gave me a couple of errors.. Don't know exactly how should I edit it..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FindingMemo2{
             public static void main(String[] args){
                      Memo frame = new Memo();
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setVisible(true);
    				 
             }
    }
    class Memo extends JPanel implements ActionListener{
    	Image mypic;
    	JLabel label;
    	JRadioButton yes,no;
    	ButtonGroup radioGroup;
             public Memo(){
    		Container c= getContentPane();
    		c.setLayout(null);
    		 label=new JLabel("Would You Like to Play?");
    		 radioGroup=new ButtonGroup();
    		 yes=new JRadioButton("Yes", true);
    		 no=new JRadioButton("No", false);
    		yes.addActionListener(this);
    		no.addActionListener(this);
    		 radioGroup.add(yes);
    		 radioGroup.add(no);
    		 label.setBounds(430,250,150,50);
    		 yes.setBounds(380,300,180,20);
    		 no.setBounds(560,300,200,20);
    		 c.add(label);
    		 c.add(yes);
    		 c.add(no);
    				
    						 
    		 mypic=Toolkit.getDefaultToolkit().getImage("5.jpg");
             Toolkit kit = Toolkit.getDefaultToolkit();
             Dimension screenSize = kit.getScreenSize();
             int screenHeight = screenSize.height;
             int screenWidth = screenSize.width;
             setSize(screenWidth, screenHeight);
    		 }
    		 public void paintComponent(Graphics g){
    		 super.paintComponent(g);
    		g.drawImage(mypic, 3,20,this);
    		}
    		 
    
    		public void actionPerformed(ActionEvent e){
                        if(e.getSource()==yes){
    						JOptionPane.showMessageDialog(null,"Welcome to Finding Memo!");
    						//startGame();
    
    						
    
    					}else { 
    					    JOptionPane.showMessageDialog(null,"Thanks for Visiting Memo!");
    						System.exit(0);
    					}
    	}
    
    		
            
    }

  7. #7
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    When I did this, there were no longer any errors; but nothing was shown on screen when I tried running it..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FindingMemo2{
             public static void main(String[] args){
                      Memo panel = new Memo();
                      //panel.setDefaultCloseOperation(JPanel.EXIT_ON_CLOSE);
                      panel.setVisible(true);
    				 
             }
    }
    class Memo extends JPanel implements ActionListener{
    	Image mypic;
    	JLabel label;
    	JRadioButton yes,no;
    	ButtonGroup radioGroup;
             public Memo(){
    		/*Container c= getContentPane();
    		c.setLayout(null);*/
    		 label=new JLabel("Would You Like to Play?");
    		 radioGroup=new ButtonGroup();
    		 yes=new JRadioButton("Yes", true);
    		 no=new JRadioButton("No", false);
    		yes.addActionListener(this);
    		no.addActionListener(this);
    		 radioGroup.add(yes);
    		 radioGroup.add(no);
    		 label.setBounds(430,250,150,50);
    		 yes.setBounds(380,300,180,20);
    		 no.setBounds(560,300,200,20);
    		 add(label);
    		 add(yes);
    		 add(no);
    				
    						 
    		 mypic=Toolkit.getDefaultToolkit().getImage("5.jpg");
             Toolkit kit = Toolkit.getDefaultToolkit();
             Dimension screenSize = kit.getScreenSize();
             int screenHeight = screenSize.height;
             int screenWidth = screenSize.width;
             setSize(screenWidth, screenHeight);
    		 }
    		 public void paintComponent(Graphics g){
    		 super.paintComponent(g);
    		g.drawImage(mypic, 3,20,this);
    		}
    		 
    
    		public void actionPerformed(ActionEvent e){
                        if(e.getSource()==yes){
    						JOptionPane.showMessageDialog(null,"Welcome to Finding Memo!");
    						//startGame();
    
    						
    
    					}else { 
    					    JOptionPane.showMessageDialog(null,"Thanks for Visiting Memo!");
    						System.exit(0);
    					}
    	}
    
    		
            
    }
    Last edited by Galore; 10-17-2009 at 09:40 PM.

  8. #8
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    Could somebody help me please? :(

  9. #9
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    Could even the other online moderators help me with my concern?Please...
    Last edited by Galore; 10-17-2009 at 08:12 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're using a JPanel, then you have to provide it with a root pane to display it, either a JFrame as you are doing:
    Java Code:
    public class FindingMemo2SmallDemo {
       public static void main(String[] args) {
          Memo memo = new Memo();
          
          JFrame frame = new JFrame("Finding Memo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(memo);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }
    or a JDialog, as I suggest.

    Also, JPanels don't have contentPanes. You'll need to remove any reference to this and deal directly with the JPanel.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Galore View Post
    Could the even other online moderators help me with my concern?Please...
    That's a pretty quick bump. A little patience goes a long way. Much luck.

  12. #12
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    That's a pretty quick bump. A little patience goes a long way. Much luck.
    Sorry.. I checked if you were still online but found out you were not. I am really pressured about this since the deadline was really already over, though a lot of us weren't able to submit, I can't take it as a justification that I would let my instructor's nerves crash.
    Sorry. Really sorry. I was a bit anxious about the time zone as well. Really sorry Fubarable :(

  13. #13
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    @Fubarable: Pretty please.. I am sorry :(

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Galore View Post
    @Fubarable: Pretty please.. I am sorry :(
    I have already answered your latest question (post #10) but don't see a reply to it. If you have further questions, please ask away.

  15. #15
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    Thank you. Sorry for being impatient.
    I tried running the code, I guess that was how I almost wanted it to be, but the background image did not appear correctly.
    I tried checking the difference between a JPanel and JFrame. I learned JFrame is the window while JPanel is the content. You told me that it would be more appropriate if I use JPanel. How would I be able to use it?
    And I am still a little confused as well, how could I maintain the background image all throughout the program or would I have to make separate background even while the game is ongoing?
    Sorry, I'm really confused..

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    There are ways of using the same image throughout. For instance you could use a JPanel as your JFrame's contentPane throughout the app, and have that the image show up in that JPanel. If you add JPanels and other JComponents on top of this JPanel though, you'll have to take care that you set their opaque property as false via

    otherJPanel.setOpaque(false);

    or something similar otherwise you won't see the image on the backing panel.

    Myself, I'd use a JOptionPane to do this initial question of whether or not to start the game, not worry about giving it a background image, and be done with it. You have much harder work ahead of trying to create the game itself without having to worry about this little initial dialog.

  17. #17
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    Actually, I guess that was really the root problem. I kept myself locked into making this sort of intro to the real game. When I run the first code I posted here, I felt like a child satisfied with a very little "accomplishment", until I realized it wasn't at all since it wasn't done as it should have appropriately been. I then could not move on. After being more pressured by the time, I told myself, I really should go on, the problem was, I could not find an example code of using not the usual heart, ace etc as the very cards in the game, but my own saved images..
    Could you possibly help me out with this as well?

  18. #18
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    Actually, even before the first (Easy) category displays 6 cards, a trivia should be shown first. The trivia should come in random; I included this in my proposal, not realizing the memo card game itself was already difficult. I guess that's one of my weaknesses, I run a lot of ideas, but can't put my hands on doing the programming process..

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

    Default

    As for creating memory games, it's a bit more involved. There are examples posted here and also elsewhere. You may wish to examine them before starting out. Myself, I like using a non-GUI model class as the nucleus of the game and then displaying a grid of JPanels that each hold a JButton and a JLabel in a CardLayout for the GUI. YMMV.

  20. #20
    Galore is offline Member
    Join Date
    Aug 2009
    Posts
    38
    Rep Power
    0

    Default

    I got this from the proposal I submitted to my instructor..

    If the user chose to play, another subsequent message would be shown asking a certain question or trivia which could vary depending on the instance and the set of cards to be dealt with. The set of cards would come in random as well. After posting the question/trivia, the user would be hung for a while and be indulged to start playing the very game, that is, beginning with the first level. There would be six cards from which three pairs must be formed. Twenty five seconds would be allotted to solve the first category.

Page 1 of 3 123 LastLast

Similar Threads

  1. Java-programmed card game pairs
    By Galore in forum Java Applets
    Replies: 0
    Last Post: 08-30-2009, 08:34 AM
  2. need help coding my game (inkball)
    By tornbacchus in forum New To Java
    Replies: 2
    Last Post: 06-09-2009, 05:38 AM
  3. Creating a Card Game in Java
    By Natrix in forum New To Java
    Replies: 1
    Last Post: 05-05-2009, 06:55 PM
  4. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 04:53 PM
  5. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 07:37 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
  •