I need to invoke getWidth()
on my JPanel object "gameboard" and store it in a variable "width". It would seem like a simple
int width = gameboard.getWidth();
However I get an error "cannot reference field before it is defined". Which led me to believe that the wrong object was being referred to, so I changed it to int width = this.gameboard.getWidth();
Now Eclipse shows that gameboard refers to GameBoard Game.gameboard
(which is the JPanel object I want), but I get a Exception in thread "AWT-EventQueue-0"
Here is some of the code, I hope its enough to explain my situation (all the code is 1000+ lines as its something my professor wrote that he is having us modify).
class Game extends JFrame {
/* Type your variable declarations here */
// Score of game
int score = 0;
// Get board size
// int width = this.gameboard.getWidth();
// int height = this.gameboard.getHeight();
// Game objects -- Was going to pass width and height variable to these
Sprite cat = new Sprite(new ImageIcon("cat.gif").getImage(), 267, 167);
Sprite bananas1 = new Sprite(new ImageIcon("bananas.png").getImage(), randomNumber(0, gameboard.getWidth()), randomNumber(0, 480));
Sprite bananas2 = new Sprite(new ImageIcon("bananas.png").getImage(), randomNumber(gameboard.getWidth(),640), randomNumber(0, 480));
...
/** The panel where all of the game's images are rendered */
GameBoard gameboard = null;
...
/**
* Constructor for the game.
* <p>
* <pre>
* The following tasks are performed:
* a frame is displayed on the screen
* a clock is created; the clock invoked repaint 15 times per second
* a keyboard listener listens for presses of the arrow keys & space bar
* </pre>
* <p>
*/
public Game() {
/* how large is the window? */
setSize(640,480);
/* if the end-user clicks on the x in the upper right corner, */
/* close this app */
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* allow the window to receive the focus; */
/* we want this window to see key presses */
setFocusable(true);
/* this window can receive keyboard events */
addKeyListener(new MyKeyListener());
/* make the window visible */
setVisible(true);
/* add MyPanel to the window */
setLayout(new GridLayout(1,1));
gameboard = new GameBoard();
add(gameboard);
validate();
}//Game
/**
* Panel that displays all the graphics in the game.
* <p>
* Why do I need a panel--why can't I display the graphics in the frame?
* I want to the top of the graphics area to be at y=0.
* <p>
* Offscreen buffers are used to create a rock-solid animation that does not blink.
* <p>
* Focus is given to the panel so that the panel can listen for key presses.
* <p>
* The clock invokes repaint 15 times per second.
*/
public class GameBoard extends JPanel {
/** offscreen buffers are used to create a rock-solid animation that does not blink */
protected Image offscreenImage = null;
/** offscreen buffers are used to create a rock-solid animation that does not blink */
protected Graphics offscreenGraphics = null;
/**
* Constructor for the main panel in this game;
* all of the graphics are displayed on this panel.
* <p>
* <pre>
* Focus is given to the panel so that the panel can listen for key pressed.
* NOTE: Focus determines who receives the characters that are typed on the keyboard--
* the entity that has the focus receives the characters.
* A keyboard listener is created to listen for key pressed.
* A clock is created; the clock invokes repaint 15 times per second.
* </pre>
* <p>
*/
public GameBoard() {
/* allow this panel to get the focus */
setFocusable(true);
/* give this panel the focus */
requestFocus();
/* Now that this panel has the focus, this panel can receive keyboard events */
addKeyListener(new MyKeyListener());
/* this window can receive mouse motion events */
addMouseMotionListener(new MyMouseMotionListener());
/* this window can receive mouse events */
addMouseListener(new MyMouseListener());
/* start a clock that invokes repaint 15 times per second */
new ThreadClock().start();
}//MyPanel
...
/**
* Play the game
* @param args Command line arguments
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Game();
}
});
}//main
}//Game