woensdag 28 december 2011

Blinking Unit Example



Press the mouse on th applet to activate. Then use wsad to move the blinking unit across the applet screen.
I was preparing a turn based example but I felt it was a little bit to much to do in one time. So I decided to make a part of that example as this example. In this applet you can move a unit across the field. It stops blinking and becomes visible if you move it. It then after one second becomes invisible to become visible again after another second.

 


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

public class BlinkingUnitExample extends Applet implements Runnable{
 Graphics bufferGraphics;
 Image offscreen;

 int unitx;
 int unity;
 int unitsize = 16;
 boolean unitvisible = false;
 long blinktime;

 public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
  unitx = getSize().width / 2 / unitsize - 1;
  unity = getSize().height / 2 / unitsize - 1;
  new Thread(this).start();
 }

    public void run() {
        for(;;) { // animation loop never ends
         repaint();
         try {
          doblinktime();
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

 public void doblinktime(){
  if ( System.currentTimeMillis() > blinktime ){
   if ( unitvisible == true ) {
    unitvisible=false;
   } else {
    unitvisible=true;
   }
   blinktime = System.currentTimeMillis() + 1000;
  }
 }

     public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
     bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Blinking Unit Example.",10,10);
        bufferGraphics.drawString("w/s/a/d to move unit.",10,220);
  if ( unitvisible == true ){
   bufferGraphics.fillRect(unitx * unitsize , unity * unitsize , unitsize , unitsize);
  }
        g.drawImage(offscreen,0,0,this);
     }

 public void resetblink(){
   blinktime = System.currentTimeMillis() + 1000;
   unitvisible = true;
 }


 public boolean keyUp (Event e, int key){
    if( key == 119 ) // w key
        {
         if( unity > 1 ) unity--;
         resetblink();
        }
        if( key == 97 ) // a key
        {
         if( unitx > 1 ) unitx--;
         resetblink();
        }
        if( key == 100 ) // d key
        {
         if( unitx < 18 ) unitx++;
         resetblink();
        }
        if( key == 115 ) // s key
        {
         if( unity < 12 ) unity++;
         resetblink();
        }

  return true;
 }

}

Geen opmerkingen:

Een reactie posten