Java Applet where object stops moving through mouse click


<html>
<head> <title> Applet </title> </head>
<body>
<Applet code="Checkers.class" width=300 height=300></Applet>
</body>
</html>





import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.*;


public class Checkers extends java.applet.Applet implements Runnable, MouseListener {

  Thread runner;
  int xpos, ypos;
  Image offscreenImg;
  Graphics offscreenG;
  boolean pleaseWait = false;
 

  public void init() {
    offscreenImg = createImage(this.size().width, this.size().height);
    offscreenG = offscreenImg.getGraphics();
     addMouseListener(this);

  }
 
    public void mouseMoved(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
     
     public void mousePressed(MouseEvent e)
     {
        while(true)
        {
        pleaseWait = true;
        }
     
     }
     
     public void mouseReleased(MouseEvent e)
     {
       pleaseWait = false;
     }

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

  public void stop()
   {
       if (runner != null)
       {
       runner.stop();
       runner = null;
      }
  }

  public void run() {
 
  while(pleaseWait == false)
  {
     //Top checker starting position
     xpos = 5;
     //Bottom Checker starting position
     ypos = 105;
     
     //infinit loop to keep checkers moving
     while(true)
     {
     //moves checkers the from one side to the other
              while (xpos < 105 && ypos > 5)
     {
        xpos += 5;
        ypos -= 5;
       
                 repaint();
                 try { Thread.sleep(100); }
                 catch (InterruptedException e) { }
         
    }
        //moves checkers from the other side
    while(xpos > 5 && ypos < 105)
     {
          xpos -= 5;
          ypos += 5;
           
          repaint();
                   try { Thread.sleep(100); }
                   catch (InterruptedException e) { }
    }
    }
    }

     
 }
 


  public void update(Graphics g) {
     paint(g);
  }


  public void paint(Graphics g) {
    // Draw background onto the buffer area
    offscreenG.setColor(Color.black);
    offscreenG.fillRect(0,0,100,100);
     offscreenG.setColor(Color.black);
     offscreenG.fillRect(100,100,100,100);
    offscreenG.setColor(Color.gray);
    offscreenG.fillRect(100,0,100,100);
     offscreenG.setColor(Color.gray);
     offscreenG.fillRect(0,100,100,100);
     
    // Draw checker
    offscreenG.setColor(Color.red);
    offscreenG.fillOval(xpos,5,90,90);
     offscreenG.setColor(Color.blue);
     offscreenG.fillOval(ypos,105,90,90);
 
    // Now, transfer the entire buffer onto the screen
    g.drawImage(offscreenImg,0,0,this);
  }
  public void destroy() {
     offscreenG.destroy();
  }
}

Comments

Popular posts from this blog

IT resources @ DAIICT

I blinked and the day passed ...

The world is what it is