Hi,
i am planning to develop a tetris game in java, but i am having trouble
in the intial stage where i have to interupt the current thread if any
arrow key or the cancel button is pressed. Somehow i am not able to
interupt it...
It would be great if anyone could tell me how to,
cheers,
Sumit
Source code :
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Brick extends Applet implements WindowListener,KeyListener
{
int x=100;
int y=20;
int w= 10;
int h=30;
int f=0;
Frame f1;
public void init()
{
/*
f1= new Frame("Brick GAME");
f1.setVisible(true);
f1.setSize(640,480);
f1.setBackground(Color.white);
f1.addWindowListener(this);*/
}
public void paint(Graphics g)
{
SUMIT:
{
Graphics2D g2= (Graphics2D)g;
if(f==1)
{h=10;
w=30;
f=0;
}
switch ((int)(Math.random()*3)){
case 0:
g2.setPaint(Color.blue);
break;
case 1:
g2.setPaint(Color.red);
break;
case 2:
g2.setPaint(Color.green);
break;}
g2.fillRect(x,y,w,h);
g2.setPaint(Color.white);
g2.fillRect(x,y-h,w,h);
y++;
try {
//Thread.currentThread().suspend();
Thread.currentThread().sleep(5);
//Thread.currentThread().notify();
if(y>200){
x=x+w;
y=20;
}
}
catch (InterruptedException e) { }
if(x>300)
{
Thread.currentThread().stop();
break SUMIT;}
else{
paint(g);
}
}
}
public void keyPressed(KeyEvent ke)
{
}
public void keyUp(KeyEvent ke)
{
}
public void keyDown(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void windowClosing(WindowEvent e) //incase of X clicked on the
window
{
System.exit(0);
}
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
Chris Smith - 01 Dec 2005 15:16 GMT
> i am planning to develop a tetris game in java, but i am having trouble
> in the intial stage where i have to interupt the current thread if any
> arrow key or the cancel button is pressed. Somehow i am not able to
> interupt it...
Please step back and think about what you're trying to accomplish,
instead of focusing on the specific technical step that you think you
need. You do not need to interrupt the current thread, ever. The very
concept is non-sensical. Thread interruption is one of a number of
methods that one thread may communicate with another. A thread never
needs to communicate with itself. In the code below, you only have one
thread, and you should therefore never need to do anything like
interrupting.
Worse yet, you have a call to Thread.stop, and another to Thread.suspend
that's been commented out. NEVER call either of those two methods. For
complicated technical reasons, they are ALWAYS bugs, even if you really
need what they accomplish. You don't even want to do those things.
Since I can't get inside your head and figure out why you think you need
to do these things, I can't suggest a replacement. There's no reason
you should need anything like their functionality.
You also can't call System.exit from an applet. It's an applet, so it's
going to be there as long as the HTML page that contains it. You need
to design around that, perhaps by providing a title page when the game
ends, with an option to start a new game.
Next, don't call Thread.sleep from within the event dispatch thread.
That will block all events, and your code won't work. Instead, you
should look into using a timer: probably java.util.Timer since you're
working with AWT.
At a higher level than any of this, though, you are clearly programming
by coincidence... that is, trying a bunch of stuff to see what works.
The non-sensical code both in comments and otherwise gives it away. You
aren't to the point yet where you can do this. You really need to get a
good book and work through the examples there. Once you've gotten
enough of a handle on the environment that you can understand how things
fit together, then it may be time to return to your Tetris project. But
you're just in over your head, and it's obvious to everyone who reads
this code. No number of newsgroup questions is likely to help until you
go back and pick easier sample code to write and understand, and work up
your level of difficulty.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
asksumit@gmail.com - 01 Dec 2005 20:22 GMT
Thanks,
I apologize for sending a mixed code, it was all messed up b/w the
application version and applet version, the only problem i had was the
sleep() method, thanks for sorting it out. I would use the Timer
function in the java.util package
thanks once again.
> > i am planning to develop a tetris game in java, but i am having trouble
> > in the intial stage where i have to interupt the current thread if any
[quoted text clipped - 46 lines]
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation
Roedy Green - 01 Dec 2005 18:22 GMT
On 1 Dec 2005 03:23:57 -0800, "asksumit@gmail.com"
<asksumit@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>i am planning to develop a tetris game in java, but i am having trouble
>in the intial stage where i have to interupt the current thread if any
>arrow key or the cancel button is pressed. Somehow i am not able to
>interupt it...
>It would be great if
If it is calculating it is not wise two stop it. You can leave the
world is too crazy a state.
So what you do instead is set a variable that the thread periodically
checks to request it to shut down gracefully like this:
package com.mindprod.common11;
/**
* A Thread you can stop gracefully. Remember nullify references to
the thread
* after you are finished. Threads are big. You can't restart it!
version 1.0
* 2002-08-01 version 1.1 2002-08-02 Make pleaseStop volatile and
synchronize
* access to it. Required for multi-cpu cache synchronization.
*
* @author Roedy Green
*/
public class StoppableThread extends Thread {
/**
* true when we want the current run method to stop as soon as it
can.
* volatile so threads will take always take a fresh look at what
other
* threads have set.
*/
private volatile boolean pleaseStop = false;
/**
* Stop this thread gracefully. If the thread is already stopped,
does
* nothing. For this to work, this.run must exit by checking
stopping() at
* convenient intervals and returning if it is true.
*
* @param interrupt
* true if this thread should be interrupted from sleep or
from doing
* an i/o (which might close the channel), before stopping
it.
* @param timeout
* How long in milliseconds to wait for the thread to die
before
* giving up. 0 means wait forever. -1 means don't wait at
all.
*/
public void gentleStop ( boolean interrupt, long timeout )
{
if ( !this.isAlive() ) return;
synchronized ( this )
{
// ask thread to stop.
this.pleaseStop = true;
}
// wake this thread up if it is sleeping,
// and get it to notice stopping() flag.
// Will also interrupt i/o.
if ( interrupt )
{
this.interrupt();
}
if ( timeout >= 0 )
{
try
{
// wait for the thread to die.
this.join( timeout );
}
catch ( InterruptedException e )
{
}
}
} // end stop
/**
* Start this thread executing its run method on a separate
thread. You may
* only call start once. After the thread dies it cannot be
restarted.
*
* @exception IllegalThreadStateException
* if this thread is already started.
*/
public void start ()
{
super.start();
}
/**
* this.run should call stopping() at convenient invervals to see
if it has
* been requested to stop. If true, it should finish up quickly
and return.
* Will have to be called via while ( !
* ((StoppableThead)Thread.currentThread()).stopping() )
*
* @return true if run should exit soon.
*/
public synchronized final boolean stopping ()
{
return pleaseStop;
}
/**
* Constructor.
*
* @param r
* Class that has a run method
*/
public StoppableThread( Runnable r )
{
super( r );
}
} // end StoppableThread

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.