Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Safely stopping a thread

Reply
Thread Tools

Safely stopping a thread

 
 
Kevin Jennings
Guest
Posts: n/a
 
      04-21-2004
Hi, group! I bought a book, "Black Art of Game Programming in Java" and
I've started typing in some of the programs. The book is pretty old,
apparently, and very outdated. One of the initial programs uses a
deprecated method (thread.stop). Can someone look at the code below and
tell me the correct way this thread should be ended without me having to use
the deprecated method?

Thanks so much!!!

Kevin

import java.applet.*;
import java.awt.*;

public class Broadway extends Applet implements Runnable {

Graphics offscreen;
Image image;
Thread animation;
int locx, locy;
int width, height;

static final byte UP = 0;
static final byte DOWN = 1;
static final byte LEFT = 2;
static final byte RIGHT = 3;

byte state;

static final int REFRESH_RATE = 100;

public void init() {

System.out.println(">> init << ");
setBackground(Color.black);
locx = 80;
locy = 100;
width = 110;
height = 90;
state = UP;

image = createImage(300,300);
offscreen = image.getGraphics();
}

public void start() {
System.out.println(">> start <<");
animation = new Thread(this);
if (animation != null) {
animation.start();
}
}

public void paint(Graphics g) {

offscreen.setColor(Color.black);
offscreen.fillRect(0,0,300,300);

offscreen.setColor(Color.yellow);
offscreen.fillRect(0,0,90,90);
offscreen.fillRect(250,0,40, 190);
offscreen.fillRect(80, 110, 100,20);

offscreen.setColor(Color.blue);
offscreen.fillRect(80,200,220,90);
offscreen.fillRect(100, 10,90,80);

offscreen.setColor(Color.green);
offscreen.fillRect(locx, locy, width, height);

offscreen.setColor(Color.red);
offscreen.fillRect(200,0,45,45);
offscreen.fillRect(0,100,70,200);

offscreen.setColor(Color.magenta);
offscreen.fillRect(200,55,60,135);
g.drawImage(image,0,0,this);
}

void updateRectangle() {
switch(state) {
case DOWN:
locy +=2;
if (locy >= 110) {
state = LEFT;
}
break;

case UP:
locy -= 2;
if (locy <= 90) {
state = RIGHT;
}
break;

case RIGHT:
locx += 2;
if (locx >= 90) {
state = DOWN;
}
break;

case LEFT:
locx -= 2;
if (locx <= 70) {
state = UP;
}
break;
}
}

public void run() {
while(true) {
repaint();
updateRectangle();
try {
Thread.sleep(REFRESH_RATE);
} catch (Exception exc) { };

}
}

public void update(Graphics g) {
g.clipRect(70,90,130,110);
paint(g);
}

public void stop() {
System.out.println(">> stop <<");
if (animation != null) {
animation.stop();
animation = null;
}
}
}


 
Reply With Quote
 
 
 
 
Kevin Jennings
Guest
Posts: n/a
 
      04-21-2004
Whoops! Sorry! I meant to post this to comp.lang.java.help! Please
disregard!

Thanks,

Kevin

"Kevin Jennings" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Hi, group! I bought a book, "Black Art of Game Programming in Java" and
> I've started typing in some of the programs. The book is pretty old,
> apparently, and very outdated. One of the initial programs uses a
> deprecated method (thread.stop). Can someone look at the code below and
> tell me the correct way this thread should be ended without me having to

use
> the deprecated method?
>
> Thanks so much!!!
>
> Kevin
>
> import java.applet.*;
> import java.awt.*;
>
> public class Broadway extends Applet implements Runnable {
>
> Graphics offscreen;
> Image image;
> Thread animation;
> int locx, locy;
> int width, height;
>
> static final byte UP = 0;
> static final byte DOWN = 1;
> static final byte LEFT = 2;
> static final byte RIGHT = 3;
>
> byte state;
>
> static final int REFRESH_RATE = 100;
>
> public void init() {
>
> System.out.println(">> init << ");
> setBackground(Color.black);
> locx = 80;
> locy = 100;
> width = 110;
> height = 90;
> state = UP;
>
> image = createImage(300,300);
> offscreen = image.getGraphics();
> }
>
> public void start() {
> System.out.println(">> start <<");
> animation = new Thread(this);
> if (animation != null) {
> animation.start();
> }
> }
>
> public void paint(Graphics g) {
>
> offscreen.setColor(Color.black);
> offscreen.fillRect(0,0,300,300);
>
> offscreen.setColor(Color.yellow);
> offscreen.fillRect(0,0,90,90);
> offscreen.fillRect(250,0,40, 190);
> offscreen.fillRect(80, 110, 100,20);
>
> offscreen.setColor(Color.blue);
> offscreen.fillRect(80,200,220,90);
> offscreen.fillRect(100, 10,90,80);
>
> offscreen.setColor(Color.green);
> offscreen.fillRect(locx, locy, width, height);
>
> offscreen.setColor(Color.red);
> offscreen.fillRect(200,0,45,45);
> offscreen.fillRect(0,100,70,200);
>
> offscreen.setColor(Color.magenta);
> offscreen.fillRect(200,55,60,135);
> g.drawImage(image,0,0,this);
> }
>
> void updateRectangle() {
> switch(state) {
> case DOWN:
> locy +=2;
> if (locy >= 110) {
> state = LEFT;
> }
> break;
>
> case UP:
> locy -= 2;
> if (locy <= 90) {
> state = RIGHT;
> }
> break;
>
> case RIGHT:
> locx += 2;
> if (locx >= 90) {
> state = DOWN;
> }
> break;
>
> case LEFT:
> locx -= 2;
> if (locx <= 70) {
> state = UP;
> }
> break;
> }
> }
>
> public void run() {
> while(true) {
> repaint();
> updateRectangle();
> try {
> Thread.sleep(REFRESH_RATE);
> } catch (Exception exc) { };
>
> }
> }
>
> public void update(Graphics g) {
> g.clipRect(70,90,130,110);
> paint(g);
> }
>
> public void stop() {
> System.out.println(">> stop <<");
> if (animation != null) {
> animation.stop();
> animation = null;
> }
> }
> }
>
>



 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-21-2004
On Tue, 20 Apr 2004 21:09:56 -0500, "Kevin Jennings"
<(E-Mail Removed)> wrote or quoted :

>Can someone look at the code below and
>tell me the correct way this thread should be ended without me having to use
>the deprecated method?


see the class StoppableThread included in
http://mindprod.com/products.html#BUS

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
thread join() not stopping mclaugb Python 0 09-02-2006 01:43 PM
Stopping all threads from other thread amadeusz.jasak@gmail.com Python 2 08-20-2006 05:24 AM
Starting/Stopping a thread with a Web Page traviscook88@gmail.com ASP .Net 1 11-04-2005 07:13 PM
thread catching and stopping an OutOfMemoryError Ivan Villanueva Java 2 06-09-2005 06:06 AM
Stopping a thread from another one Fabiano Sidler Python 6 06-24-2004 08:24 AM



Advertisments