Results 1 to 20 of 45
- 10-17-2009, 05:58 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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..
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..Java Code:Last edited by Galore; 10-21-2009 at 02:20 PM. Reason: code tags added
-
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!
- 10-17-2009, 06:09 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also please clearly specify what's your question is. Reading a long post is quite difficult.
-
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.
- 10-17-2009, 06:28 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
@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.
I have been stuck up with this. I have been making this since last week but really could not move on.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 hope you could help me get this through..Last edited by Galore; 10-17-2009 at 07:00 PM.
- 10-17-2009, 06:45 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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); } } }
- 10-17-2009, 06:58 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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 08:40 PM.
- 10-17-2009, 07:02 PM #8
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
Could somebody help me please? :(
- 10-17-2009, 07:03 PM #9
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
Could even the other online moderators help me with my concern?Please...
Last edited by Galore; 10-17-2009 at 07:12 PM.
-
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:
or a JDialog, as I suggest.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); } }
Also, JPanels don't have contentPanes. You'll need to remove any reference to this and deal directly with the JPanel.
-
- 10-17-2009, 07:16 PM #12
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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 :(
- 10-17-2009, 08:01 PM #13
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
@Fubarable: Pretty please.. I am sorry :(
-
- 10-17-2009, 08:36 PM #15
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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..
-
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.
- 10-17-2009, 08:58 PM #17
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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?
- 10-17-2009, 09:02 PM #18
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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..
-
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.
- 10-17-2009, 09:03 PM #20
Member
- Join Date
- Aug 2009
- Posts
- 38
- Rep Power
- 0
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.
Similar Threads
-
Java-programmed card game pairs
By Galore in forum Java AppletsReplies: 0Last Post: 08-30-2009, 07:34 AM -
need help coding my game (inkball)
By tornbacchus in forum New To JavaReplies: 2Last Post: 06-09-2009, 04:38 AM -
Creating a Card Game in Java
By Natrix in forum New To JavaReplies: 1Last Post: 05-05-2009, 05:55 PM -
card game Rummy
By javafox in forum New To JavaReplies: 4Last Post: 03-14-2009, 03:53 PM -
A Online Card Game
By GonzaloP in forum NetworkingReplies: 0Last Post: 12-28-2008, 06:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks