Results 1 to 14 of 14
  1. #1
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default A reference to an object type?

    Hey everyone!!

    Just a warning, I started programming about three weeks ago so I'm pretty new at this :)

    Now, I have a question.
    I'm trying to write this program "Memory Game" with multiple classes. I have one class which I call Memory that represents the main window that will appear when the game starts, and a class Board that will hold the cards that are displayed; this class is also responsible for implementing the logic of the game.
    Now, the Memory class has been going well for me, except for one thing. I need to create a reference to an object of Board type. Now I have no clue on how to do this.
    Also, in the Board class, I have a constructor Board() that creates the cards that will be displayed on the board, store them in an array and display them on the screen (if that helps).

    Any advice will be appreciated :)

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: A reference to an object type?

    Quote Originally Posted by LuxoJr View Post
    Hey everyone!!

    Just a warning, I started programming about three weeks ago so I'm pretty new at this :)

    Now, I have a question.
    I'm trying to write this program "Memory Game" with multiple classes. I have one class which I call Memory that represents the main window that will appear when the game starts, and a class Board that will hold the cards that are displayed; this class is also responsible for implementing the logic of the game.
    Now, the Memory class has been going well for me, except for one thing. I need to create a reference to an object of Board type. Now I have no clue on how to do this.
    Also, in the Board class, I have a constructor Board() that creates the cards that will be displayed on the board, store them in an array and display them on the screen (if that helps).

    Any advice will be appreciated :)
    Board boardRefName = new Board();

    I do not fully understand what you have problems with ... can you explain it more detailed? Thanks

  3. #3
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    Thanks for the reply :)
    Basically, I have a constructor in my Board class that creates the cards, and I want to call this Board constructor into my Memory class in order to place the cards in the main window.

    Does that makes sense? :P

  4. #4
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: A reference to an object type?

    The constructor is called by the new operator .... so new Board(); will call the constructor to initialize the Board object.

  5. #5
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    Ok, so in my Memory class, if I insert new Board();, it'll call the constructor Board() from my Board class?

    I'll try to find a way to post my code here.

  6. #6
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: A reference to an object type?

    put your code in [code] tags ... if you insert new Board(); it will create a new board object thereby running the constructor ... I think its better that you post some code so we can see whats going on =)

  7. #7
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    So here's my Memory class:


    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    
    public class Memory extends JFrame implements ActionListener{
      
             private int[] hiddenImage;
             private int buttonsVisible;
             static  private JButton[] numButtons;
             private JPanel numPanels = new JPanel();
             private JPanel menuPanels = new JPanel(new GridLayout(0,3,0,3));
             private JLabel label = new JLabel();
             private  JLabel freeSpace = new JLabel("             ");
             private JButton restartButton = new JButton("Restart");
             ImageIcon icon = new ImageIcon("/Users/ImadGhadie/Documents/data-2/400/img-26.jpg");
             Image img = icon.getImage();
             Image newimg = img.getScaledInstance(75, 105, java.awt.Image.SCALE_SMOOTH);
             ImageIcon newIcon = new ImageIcon(newimg);
             private JButton btnQuit = new JButton("Quit");
             Board Board = new Board();
    
        
             
      Memory(int choice){
          
             super("Memory"); // New JFrame (from Superclass)
             
             numButtons = new JButton[choice]; // 2 x 8 buttons for the MemoryGame
    
             // (btnNumber.length) hidden numbers for the buttons
             hiddenImage = new int[ numButtons.length ];
      
             buttonsVisible = 0;
      
              setSize(550, 550);
                   setDefaultCloseOperation(EXIT_ON_CLOSE);
                   setResizable(false);
                   setLocationRelativeTo(null);
          
              numPanels.setLayout(new GridLayout(4, 6, 3, 3));
              numPanels.setBackground(Color.WHITE);
          
              add(numPanels);
              add(menuPanels, BorderLayout.SOUTH);
          
          
              menuPanels.add(freeSpace);
              label.setHorizontalAlignment(JLabel.CENTER);
    
              btnQuit.addActionListener(this);
              menuPanels.add(restartButton); // method that sets up the frame and draws the panel
              setVisible(true); // makes the frame visible
              new Board();
              }
        
      
      
      
      public void actionPerformed (ActionEvent e){
          
              for(int i = 0; i < numButtons.length; i++)
                {
                  numButtons[i].setText("");
                  numButtons[i].setBackground(Color.WHITE);
                  numButtons[i].setEnabled(true);
                }
              buttonsVisible = 0;
              
     }
      
      
      
      public static void main(String[] args){
          
          new Memory(24);
          
      }
      
     }

    And here's what I have so far for my Board class:


    Java Code:
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class Board extends JPanel implements ActionListener{
      
             static  private JButton[] numButtons;
             private JPanel numPanels = new JPanel();
             ImageIcon icon = new ImageIcon("/Users/ImadGhadie/Documents/data-2/400/img-26.jpg");
             Image img = icon.getImage();
             Image newimg = img.getScaledInstance(75, 105, java.awt.Image.SCALE_SMOOTH);
             ImageIcon newIcon = new ImageIcon(newimg);
             private Font fntNumbers = new Font("Arial Bold", Font.PLAIN, 20);
             
             
       Board() {
            for (int i = 0; i < numButtons.length; i++)
                 {
                 numButtons[i] = new JButton(newIcon);
                 numButtons[i].setBackground(Color.WHITE);
                 numButtons[i].setFont(fntNumbers);
                 numButtons[i].setFocusable(false);
                 numButtons[i].setEnabled(true);
                 numButtons[i].setText("");
                 numPanels.add(numButtons[i]);
                 numButtons[i].addActionListener(this);
                 }        
          public void actionPerformed (ActionEvent e){
            
          }
               }
    As you can see, in Memory(int choice), the main window for the game is created. Now I use Board() to actually create the cards that will be placed into the window, but I'm not sure on how to actually do that :)


    Thanks so much!

  8. #8
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    P.S. I hope my code doesn't give you a heart attack, I know it's kinda messy.

  9. #9
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: A reference to an object type?

    you have a Board Board = new Board(); and a separate new board(); why?
    I think what you are trying to do is just add a JPanel to your JFrame ... ?
    In the constructor of the JFrame create an object of board and add it to the frame =)

  10. #10
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    About the seperate new Board() stuff, I was just trying random stuff and forgot to delete them :p
    And yeah, that's basically what I want to do.
    So to create an object of board, I should do: Board board = new Board(); ?
    Then place it into the JFrame constructor?

  11. #11
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: A reference to an object type?

    Yeah ... so far so good ... probably want to add it to the JFrame also.

  12. #12
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    I'm sorry for being annoying, but I really appreciate the help buddy :)
    Below, lines 33 and 60 is where I tried to create the Board object and add it to the frame, but when I compile I get an error saying this:

    Exception in thread "main" java.lang.NullPointerException
    at Board.<init>(Board.java:42)
    at Memory.<init>(Memory.java:34)
    at Memory.main(Memory.java:93)


    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author ImadGhadie
     */
    
    public class Memory extends JFrame implements ActionListener{
      
             private int[] hiddenImage;
             private int buttonsVisible;
             static  private JButton[] numButtons;
             private JPanel numPanels = new JPanel();
             private JPanel menuPanels = new JPanel(new GridLayout(0,3,0,3));
             private JLabel label = new JLabel();
             private JLabel MatchingGame = new JLabel("Matching Game");
             private  JLabel freeSpace = new JLabel("             ");
             private JButton restartButton = new JButton("Restart");
             ImageIcon icon = new ImageIcon("/Users/ImadGhadie/Documents/data-2/400/img-26.jpg");
             Image img = icon.getImage();
             Image newimg = img.getScaledInstance(75, 105, java.awt.Image.SCALE_SMOOTH);
             ImageIcon newIcon = new ImageIcon(newimg);
             private Font fntNumbers = new Font("Arial Bold", Font.PLAIN, 20);
             private JButton btnQuit = new JButton("Quit");
             Board board = new Board();
             
      Memory(int choice){
          
             super("Memory"); // New JFrame (from Superclass)
             
    
             numButtons = new JButton[choice]; // 2 x 8 buttons for the MemoryGame
    
             // (btnNumber.length) hidden numbers for the buttons
             hiddenImage = new int[ numButtons.length ];
      
             buttonsVisible = 0;
      
             
              setSize(550, 550);
                   setDefaultCloseOperation(EXIT_ON_CLOSE);
                   setResizable(false);
                   setLocationRelativeTo(null);
                   try { 
                   setDefaultLookAndFeelDecorated(true); 
                   } 
                   catch (Exception e) {System.out.print(e); }
          
              numPanels.setLayout(new GridLayout(4, 6, 3, 3));
              numPanels.setBackground(Color.WHITE);
              
              add(board);
              add(numPanels);
              add(menuPanels, BorderLayout.SOUTH);
                
              menuPanels.add(freeSpace);
              label.setHorizontalAlignment(JLabel.CENTER);
    
              
              btnQuit.addActionListener(this);
              menuPanels.add(restartButton);
              setVisible(true); // makes the frame visible
              }
        
      
      
      
      public void actionPerformed (ActionEvent e){
          
              for(int i = 0; i < numButtons.length; i++)
                {
                  numButtons[i].setText("");
                  numButtons[i].setBackground(Color.WHITE);
                  numButtons[i].setEnabled(true);
                }
              buttonsVisible = 0;
              
     }
      
      
      
      public static void main(String[] args){
          
          new Memory(24);
          
      }
      
     }

  13. #13
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: A reference to an object type?

    This error says that in line 42 in the board class there is something that does not exist...

  14. #14
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Re: A reference to an object type?

    I got it :) Thanks so much!

Similar Threads

  1. What to do with object reference?
    By kyle_maddisson in forum New To Java
    Replies: 6
    Last Post: 11-04-2011, 06:58 AM
  2. Reference object
    By clj89 in forum New To Java
    Replies: 5
    Last Post: 10-23-2011, 12:32 AM
  3. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 09:39 PM
  4. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM
  5. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM

Posting Permissions

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