Reputation: 495
I'm learning Java (The Gaming Side). I bought a book and it has some code in which I tried to copy and test it. The only problem is that it comes up with errors when I try to compile it.
C:\Users\James\Desktop\Java>Javac GamePanel.java
GamePanel.java:57: illegal start of expression
private void gameUpdate()
^
GamePanel.java:57: illegal start of expression
private void gameUpdate()
^
GamePanel.java:57: ';' expected
private void gameUpdate()
^
GamePanel.java:64: reached end of file while parsing
}→
^
4 errors
The code is:
public class GamePanel extends JPanel implements Runnable
{
private static final int PWIDTH = 500;
private static final int PHEIGHT = 400;
private Thread animator;
private volatile boolean running = false;
private volatile boolean gameOver = false;
public GamePanel()
{
setBackground(Color.white);
setPreferredSize( newDimension(PWIDTH, PHEIGHT));
}
public void addNotify()
{
super.addNotify();
startGame();
}
public void startGame()
{
if (animator == null || !running)
{
animator = new Thread(this);
animator.start();
}
}
public void stopGame()
{
running = false;
}
public void run()
{
running = true;
while(running)
{
gameUpdate();
gameRender();
repaint();
try
{
Thread.sleep(20);
}
catch(InterruptedException ex)
{
}
System.exit(0);
}
private void gameUpdate()
{
if (gameOver == false) {
}
}
}
I know I'm probably doing something wrong but I checked it over and over again, can someone please enlighten me on what I am doing wrong?
Upvotes: 4
Views: 29802
Reputation: 1
you are missing the closing brace for the While Loop Within the Run Method.....Develop using IDE like Netbeans, Eclipse etc...to avoid missing Braces because they are auto Inserted when necessary. hope it solves ur problem.
Upvotes: 0
Reputation: 11
Two missing end brackets! Try using an editor that will highlight such omissions. Eclipse is big & bulky but will really help you once you get past that learning curve. Notepad++ is lean & mean but you have to check for matching brackets yourself (it will find them for you).
Upvotes: 1
Reputation: 28059
Your missing a }
the while loop isn't closed.
public void run()
{
running = true;
while(running)
{
gameUpdate();
gameRender();
repaint();
try
{
Thread.sleep(20);
}
catch(InterruptedException ex)
{
}
} // <<< this is the missing brace
System.exit(0);
}
You might want to get an IDE like eclipse, netbeans or intellij (all free) and use them to format your code...things like missing braces become a lot easier to find when your code is correctly formatted.
Upvotes: 6
Reputation: 11098
You didn't close your loop }
.
I suggest you use an IDE that points this out for you.
Upvotes: 1
Reputation: 4514
Check your run()
method. You're missing a closing curly bracket to terminate the while loop. The gameUpdate() method declaration is then being included in the previous function (incorrectly)
Upvotes: 1
Reputation: 33437
You missed a closing } for the while loop. And if you're wondering (and you should be), it's saying "illegal start of expression because "private ..." is trying to start a new block of code, but the Java parser knows that it hasn't finished the block it's in yet.
Upvotes: 1