unreachable statements
1 error result during my compilation.
Unreachable statement. what does it mean? how can i resolved this?
import java.applet.*;
import java.awt.*;
public class snake extends Applet implements Runnable
{
// Initialisierung der Variablen
int x_pos = 30; // x - Position des Balles
int y_pos = 30; // y - Position des Balles
int x_speed = 1;
int y_speed = 1;
int radius = 110; // Radius des Balles
int appletsize_x = 300; // Gre des Applets in x - Richtung
int appletsize_y = 300; // Gre des Applets in y - Richtung
// Variablen fr die Doppelpufferung
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
// Schaffen eines neuen Threads, in dem das Spiel luft
Thread th = new Thread (this);
// Starten des Threads
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
public boolean keyDown (Event e, int key)
{
// linke Cursortaste
if (key == Event.LEFT)
{
// Ball bewegt sich dann nach links
x_speed = -1;
}
// rechte Cursortaste
else if (key == Event.RIGHT)
{
// Ball bewegt sich dann nach rechts
x_speed = 1;
}
// SpaceBar hat Wert 32
else if (key == 32)
{
x_speed = 0;
}
else
{
// Ausgabe von gedrktem Key und Zahlenwert an die Standardausgabe
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
}
return true;
if (key == Event.UP)
{
y_speed = 1;
}
// rechte Cursortaste
else if (key == Event.DOWN)
{
y_speed = -1;
}
else if (key == 110)
{
y_speed = 0;
}
else
{
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
}
return true;
}
public void run ()
{
// Erniedrigen der ThreadPriority um zeichnen zu erleichtern
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Solange true ist luft der Thread weiter
while (true)
{
// Wenn der Ball den rechten Rand berhrt, dann prallt er ab
if (x_pos > appletsize_x - radius)
{
// ndern der Richtung des Balles
x_speed = -1;
}
// Ball brhrt linken Rand und prallt ab
else if (x_pos < radius)
{
// ndern der Richtung des Balles
x_speed = +1;
}
// Verndern der x- Koordinate
x_pos += x_speed;
// Neuzeichnen des Applets
repaint();
try
{
// Stoppen des Threads fr in Klammern angegebene Millisekunden
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// Zurcksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
/** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
public void update (Graphics g)
{
// Initialisierung des DoubleBuffers
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// Bildschirm im Hintergrund lschen
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// Auf gelschten Hintergrund Vordergrund zeichnen
dbg.setColor (getForeground());
paint (dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
it occurs in if (key == Event.UP) i done know ow to resolve this. please help.
[4215 byte] By [
robmars] at [2007-11-11 10:17:04]

# 1 Re: unreachable statements
This is the problem code:
public boolean keyDown (Event e, int key)
{
// linke Cursortaste
if (key == Event.LEFT)
{
// Ball bewegt sich dann nach links
x_speed = -1;
}
// rechte Cursortaste
else if (key == Event.RIGHT)
{
// Ball bewegt sich dann nach rechts
x_speed = 1;
}
// SpaceBar hat Wert 32
else if (key == 32)
{
x_speed = 0;
}
else
{
// Ausgabe von gedrktem Key und Zahlenwert an die Standardausgabe
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
}
return true;
if (key == Event.UP)
{
y_speed = 1;
}
// rechte Cursortaste
else if (key == Event.DOWN)
{
y_speed = -1;
}
else if (key == 110)
{
y_speed = 0;
}
else
{
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
}
return true;
}
What happens is that the program goes through the first IF statement, and then returns true. That return statement will end the method, therefore, anything after the return can't be reached by the program.
If you get rid of the first "return true;" then the program will compile. As to it running properly, I don't know; but it will compile.
masher at 2007-11-11 22:31:47 >

# 2 Re: unreachable statements
Basically, you have your method keyDown(). Within this method you have various if statements.
However in your method you're doing something like this:
public boolean keyDown(...){
if(condition){
...
}
else if(condition){
...
}
else if(condition){
...
}
else{
...
}
return true;
/*
Method returns here.
All other statements after this point are "unreachable."
You should remove this return statement but keep the return statement
that you have at the end of your method.
*/
if(condition){
...
}
...//other conditional statements
....
return true;
}
pacify at 2007-11-11 22:32:41 >

# 3 Re: unreachable statements
ahh yeah... hmmm after i added the code for Down key and up then runned the file through my Jcreator ... it seems the statment for DOWN and UP are not workin'.
heres the code:
import java.applet.*;
import java.awt.*;
public class snake extends Applet implements Runnable
{
// Initialisierung der Variablen
int x_pos = 30; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int x_speed = 1; // Geschwindigkeit des Balles in x - Richtung
int radius = 20; // Radius des Balles
int appletsize_x = 300; // Gre des Applets in x - Richtung
int appletsize_y = 300; // Gre des Applets in y - Richtung
// Variablen fr die Doppelpufferung
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
// Schaffen eines neuen Threads, in dem das Spiel luft
Thread th = new Thread (this);
// Starten des Threads
th.start ();
}
public void stop()
{
}
public void destroy()
{
}
/* Diese Methode fngt das Ereigniss auf, das beim Drcken einer Keyboardtaste entsteht.
Die Tasten Cursor links, rechts und die Space Taste haben dabei eine bestimmte Bedeutung.
Wird eine andere Taste gedrckt, so wird ihr Integerwert in die Standardausgabe geschrieben. */
public boolean keyDown (Event e, int key)
{
// linke Cursortaste
if (key == Event.LEFT)
{
// Ball bewegt sich dann nach links
x_speed = -1;
}
// rechte Cursortaste
else if (key == Event.RIGHT)
{
// Ball bewegt sich dann nach rechts
x_speed = 1;
}
// SpaceBar hat Wert 32
else if (key == 32)
{
x_speed = 0;
}
if (key == Event.DOWN)
{
// Ball bewegt sich dann nach links
x_speed = -1;
}
// rechte Cursortaste
else if (key == Event.UP)
{
// Ball bewegt sich dann nach rechts
x_speed = 1;
}
// SpaceBar hat Wert 32
else if (key == 32)
{
x_speed = 0;
}
else
{
// Ausgabe von gedrktem Key und Zahlenwert an die Standardausgabe
System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
}
return true;
}
public void run ()
{
// Erniedrigen der ThreadPriority um zeichnen zu erleichtern
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Solange true ist luft der Thread weiter
while (true)
{
// Wenn der Ball den rechten Rand berhrt, dann prallt er ab
if (x_pos > appletsize_x - radius)
{
// ndern der Richtung des Balles
x_speed = -1;
}
// Ball brhrt linken Rand und prallt ab
else if (x_pos < radius)
{
// ndern der Richtung des Balles
x_speed = +1;
}
// Verndern der x- Koordinate
x_pos += x_speed;
// Neuzeichnen des Applets
repaint();
try
{
// Stoppen des Threads fr in Klammern angegebene Millisekunden
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
// Zurcksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
/** Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns */
public void update (Graphics g)
{
// Initialisierung des DoubleBuffers
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// Bildschirm im Hintergrund lschen
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// Auf gelschten Hintergrund Vordergrund zeichnen
dbg.setColor (getForeground());
paint (dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
Heres the result:
Note: D:\folder\snake\snake.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Process completed.
# 4 Re: unreachable statements
I don't know anything about applets, but you don't seem to refer to the keyDown method anywhere... Maybe that's why it doesn't work.
Also, why are you passing keyDown an Event if you don't use it in the method?
masher at 2007-11-11 22:34:44 >

# 5 Re: unreachable statements
how to use it in a method?.....help me please...
# 6 Re: unreachable statements
Looking at the code, it seems to me that while the x_pos is changing based upon key events the y_pos is not changing at all, or ever.
I also think that the keyDown method is passed arguments by the applet and that you are just overriding the method keyDown to your specifications.
Here's a link for more information on event handling in applets:
http://www.javacoffeebreak.com/java107/java107.html
pacify at 2007-11-11 22:36:47 >
