Results 1 to 14 of 14
Thread: A reference to an object type?
- 02-26-2012, 09:29 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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 :)
- 02-26-2012, 09:37 PM #2
- 02-26-2012, 09:40 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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
- 02-26-2012, 09:42 PM #4
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.
- 02-26-2012, 09:50 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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.
- 02-26-2012, 09:54 PM #6
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 =)
- 02-26-2012, 10:00 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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:
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 :)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){ } }
Thanks so much!
- 02-26-2012, 10:01 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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.
- 02-26-2012, 10:13 PM #9
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 =)
- 02-26-2012, 10:21 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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?
- 02-26-2012, 10:43 PM #11
Re: A reference to an object type?
Yeah ... so far so good ... probably want to add it to the JFrame also.
- 02-26-2012, 11:04 PM #12
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
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); } }
- 02-27-2012, 12:04 AM #13
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...
- 02-27-2012, 12:08 AM #14
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
What to do with object reference?
By kyle_maddisson in forum New To JavaReplies: 6Last Post: 11-04-2011, 05:58 AM -
Reference object
By clj89 in forum New To JavaReplies: 5Last Post: 10-22-2011, 11:32 PM -
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks