Safely stopping a thread

Discussion in 'Java' started by Kevin Jennings, Apr 21, 2004.

  1. 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;
    }
    }
    }
     
    Kevin Jennings, Apr 21, 2004
    #1
    1. Advertising

  2. Whoops! Sorry! I meant to post this to comp.lang.java.help! Please
    disregard!

    Thanks,

    Kevin

    "Kevin Jennings" <> wrote in message
    news:p...
    > 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;
    > }
    > }
    > }
    >
    >
     
    Kevin Jennings, Apr 21, 2004
    #2
    1. Advertising

  3. Kevin Jennings

    Roedy Green Guest

    On Tue, 20 Apr 2004 21:09:56 -0500, "Kevin Jennings"
    <> 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.
     
    Roedy Green, Apr 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    470
    Alvin Bruney - ASP.NET MVP
    Nov 4, 2005
  2. Ivan Villanueva
    Replies:
    2
    Views:
    679
    Antti S. Brax
    Jun 9, 2005
  3. Fabiano Sidler

    Stopping a thread from another one

    Fabiano Sidler, Jun 23, 2004, in forum: Python
    Replies:
    6
    Views:
    1,485
    Antoon Pardon
    Jun 24, 2004
  4. Replies:
    2
    Views:
    443
    alfanick
    Aug 20, 2006
  5. mclaugb

    thread join() not stopping

    mclaugb, Sep 2, 2006, in forum: Python
    Replies:
    0
    Views:
    307
    mclaugb
    Sep 2, 2006
Loading...

Share This Page