We help IT Professionals succeed at work.

pls help - java applet

prays asked

i am petty new to java hence met up alot of problem.
i run an applet but my mouse onclick isn't working.
i am using threading to run 2 .gif at the same time, when the mouse onclick, it suppose to stop the 2 .gif from running. but my thread is not working properly hence only one .gif is stopped, the other is still running.

plsss help.......
the coding is pretty long here. if possible, can i send my codes over 2u..

thankz =)
wonder if the points is enough
Watch Question

ahosangFinance Systems Developer

Well however you're stopping the other gif you should stop the second gif the same way:
Like setting a boolean variable to false - that's a common way

Show some relevant code: just the threads and the stopping code might do


here's the codes..
sorri guy.. afraid is too messy...there is some codes regarding the images ... i didn't put on it .. if it is hard to see here, i can send it to you..

thanks thankz...

public class Test extends Applet implements Runnable
  Thread runner;
  Image DB_Image;
  Graphics DB_Graphics;
  Dimension DB_Dimension;

  private boolean press=false;
  private Button stopbutton = new Button("Stop");
  private Button startbutton = new Button("Start");

  public void init(){
    img1 = getImage(getCodeBase(), "img1.gif");
    img2 = getImage(getCodeBase(), "img2.gif");

  public boolean action (Event event, Object object) {
    if (event.target == stopbutton) {
      stop() //calling the stop function
      return true;
    } else
      return super.action(event, object);

  public void run(){
     long startTime = System.currentTimeMillis();

     Thread currentThread = Thread.currentThread();

     while (currentThread == animatorThread) {

     try { startTime += delay;
     Thread.sleep(Math.max (0,startTime -  
           System.currentTimeMillis() ));
     } catch (InterruptedException e)
          { break; }

    public void start() {
      if ( runner == null) {
     runner = new Thread(this);

      if (press) {}
     else { if (animatorThread == null) {
          animatorThread = new Thread (this);



    public void stop() {
     runner = null; //}
     animatorThread = null;
     offScreenImage = null;
     offScreenGraphics = null;

Finance Systems Developer
Can't rewrite code now, but just a quick tip for threaded animation.
It is usual to have a boolean variable which is in the while loop of the run() method:

while (global) {
   // Run animation logic

DO NOT USE stop() method. It is deprecated and bad.
Instead, in your action method, don't call stop. Simply set the boolean to false:

public boolean action(Event event, Object object) {
  // test for button
  global=false; //THIS IS THE KEY LINE

Remember to declare global at the outer level of the class:


thanx pal =)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.