Closed Thread
Results 1 to 5 of 5

Thread: some help ..~

  1. #1
    R3.RyozKidz Guest

    some help ..~

    this is my given assignment .
    A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given a six cards in the deck, two might be labeled 1, two labeled 2, and two labeled 3. The cards are shuffled and placed face down on the table. A player then selects two cards that are face down, turns the face up, and if the cards match they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all the cards are face up.

    here is part of my source code for the GUI part . i haven't finished it with the event for it but i nid some help from everyone to help me to fix it .~ the source code has no bug but i need some comment whether i have done it correctly on this part a not ..~ tq ..

    Code:
    import javax.swing.JButton;
    import java.awt.GridLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import java.util.Random;
    
    public class Assignment
    {
        private JFrame frame;
        private JPanel panel;
        private JButton buttons[];
        private Random $rand;
        private String button_label[] = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8" };
        
        public Assignment()
        {
            frame = new JFrame("Ultimate game");
            panel = new JPanel(new GridLayout(4 , 4 , 5 , 5));
            buttons = new JButton[16];
            
            $rand = new Random();
            
            for(int i = 0 ; i < button_label.length ; i++)
            {
                int x = $rand.nextInt();
                String temp = button_label[i];
                button_label[i] = button_label[x];
                button_label[x] = temp;
                
            }
            
            for(int i = 0 ; i < buttons.length ; i++)
            {
                buttons[i] = new JButton("*");
            }
            
            for(int i = 0 ; i < 16 ; i++)
            {
                panel.add(buttons[i]);
            }
            
            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: some help ..~

    Code:
    int x = $rand.nextInt();
    better change it or it will crash.
    Code:
    int x = $rand.nextInt(16);
    the 16 will make it generate a number from 0 to 15.

    Just a hint:
    Since you'll always show the buttons in a Gridlayout, it may be more clear to use 2D arrays for both the buttons and the button labels.
    Thus in this case:
    Code:
    JButton[][] buttons = new JButton[4][4];
    On the other hand, you'll now always need a nested for loop to run trough the array.

    If the random gets fixed you can get it working once you add an event to the buttons. It works with me so

  4. #3
    R3.RyozKidz Guest

    Re: some help ..~

    sorry..~ i missed the 16 inside the random ..~
    is it ok to have without any inheritance ..? cause most of my source codes i get from the book that deal with GUI will have the inheritance ..~ maybe extends from JPanel or JFrame ...~
    first time doing the GUI components ..~

  5. #4
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: some help ..~

    Ye, i always do extends JFrame for my GUI class. But just because i'll have less writing to do in the code

    Extends JPanel may have more use as for example you have a class "MyPanel extends JPanel" Then you can add MyPanel objects to a frame:
    Code:
    frame.getContentPane().add(myPanelObject);
    Which wouldn't be possible if you didn't extend it with JPanel. Then you would have to create a getter to get the JPanel and add that.


    wether or not to extend with JFrame, i don't know.
    Here they say it's better: Java: JFrame - Window
    They don't say why though.
    And on this forum they say nobody uses it anymore and it's bad in most cases: New To Java - extends JFrame and extends JPanel Together?
    (from the 6th reply and on)

  6. #5
    R3.RyozKidz Guest

    Re: some help ..~

    o..~ isn't it the same ...?
    Code:
    panel.add(JComponent);
    frame.setContentPane(panel);
    actually how to interpret this ...?
    Code:
    frame.getContentPane().add(panelObject);
    first , i will get the existing content pane .
    second , i will add the panelObject which inherited from JPanel

    ..~
    i have tried to extends from the JPanel . It is almost the same...~
    Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.GridLayout;
    
    public class Assignment2 extends JPanel
    {
        private JFrame frame;
        private JButton buttons[];
        
        public Assignment2()
        {
            super(new GridLayout(4 , 4 , 5 , 5));
            frame = new JFrame("Game");
            buttons = new JButton[16];
            
            for(int i = 0 ; i < 16 ; i++)
            {
                buttons[i] = new JButton("*");
                super.add(buttons[i]);
            }
            
            frame.setContentPane(this);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts