ayuda con este codigo por favor

rafacho
13 de Julio del 2005
Hola, miren por favor tengo este codigo, pero no he podido hacerlo correr, por favor ayudeme, no me tira ningun error

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author José Rafael
*/
public class tanks extends Applet{

private Button sbutton = new Button("Start");
private Image tankimg; // image of tank
public Image skullimg;
public int twidth = 30; // height and width of tank
public int theight = 30;
private Image buffer; // the off-screen buffer
public int ah, aw; // width and height of applet
public int yoff = 40; // y offset
private tank[] H; // array of tanks
public target[] Targ; // only some info are accessable!
public int tanknum = 2; // number of tanks in race
public boolean gameInProgress = false;
AnimationMonitor monitor; // all drawing through here
int ioff = 10; // image offset (for gun)
public void init()
{ int i;
ioff = twidth / 3;
skullimg = getImage(getDocumentBase(), "skull9.gif"); // get image gif
prepareImage(skullimg,this);
tankimg = createImage(twidth,theight);
Graphics tgc;
tgc = tankimg.getGraphics();
tgc.setColor(Color.red);
tgc.fillRect(0,0,twidth,theight);
tgc.setColor(Color.black); // black turet
tgc.fillOval(6,6,18,18); tgc.setColor(Color.red);
H = new tank[tanknum]; // create tankarray
Targ = new target[tanknum];
aw = 500; // applet width and height
ah = 400;
// make two tanks!!
int ix = (int) (Math.random() * aw); // random initial position
int iy = (int) (Math.random() * ah);
H[0] = new tank1(0,tankimg,ix,iy,this); // create threads
Targ[0] = new target(ix,iy); // target access
Targ[0].gunposition = H[0].gunposition;
ix = (int) (Math.random() * aw); // random initial position
iy = (int) (Math.random() * ah);
H[1] = new tank2(1,tankimg,ix,iy,this); // create threads
Targ[1] = new target(ix,iy); // target access
Targ[1].gunposition = H[1].gunposition;

setBackground(Color.white);
Image track = createImage(aw,yoff+ah); // static buffer
buffer = createImage(aw,yoff+ah); // double buffer
Graphics gc;
gc = track.getGraphics(); // gc prepares static image
gc.setColor(Color.green);
gc.fillRect(0,0,aw,ah); // green background
// creates animation monitior
monitor = new AnimationMonitor(tanknum,track,buffer,this);
sbutton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
showStatus("And off they go...");
gameInProgress = true;
monitor.prepare(); // prepare background image
for(int j=0;j<tanknum;j++) H[j].start(); // start threads
}
} );
add(sbutton);
}
public void update(Graphics g) { paint(g); } // reduces flickering
public void paint(Graphics g) { } // must override!
// }

// main tank thread class:
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
tanks ta = new tanks();
ta.start();
// TODO code application logic here
}

abstract class tank extends Thread // tank thread class
{ int ID; // number that identifies itself
tanks A; // applet object that tank belongs to
private int xpos;
private int ypos; // position (center) coords
Image img; // tank image
final int gunrange = 200; // range of gun in pixels
final int north = 0;
final int east = 1;
final int south = 2;
final int west = 3;
final int left = 1;
final int right = -1;
private int gunposition = 90; // degrees , 0 is down, 90 is right
private final int rotangle = 2; // turret rotation angle unit
protected boolean destroyed = false;
int ammo = 20; // not used for now;
// thread has access to targets through A.Targ[] array of targets
tank(int s, Image i, int x, int y, tanks h) // constructor
{ ID = s; img = i; xpos = x; ypos = y; A = h;
if ((ID%2)==1) // change color for some
{ img = A.createImage(A.twidth,A.theight);
Graphics tgc;
tgc = img.getGraphics();
tgc.setColor(Color.red);
tgc.fillRect(0,0,twidth,theight);
tgc.setColor(Color.blue); // blue turret
tgc.fillOval(6,6,18,18); tgc.setColor(Color.red);
}
}
public int xcord() { return xpos; } // prevents illegal changes
public int ycord() { return ypos; }
public int distanceTo(int x2, int y2) // computes distance to x2,y2
{ int dx = xpos - x2;
int dy = ypos - y2;
return (int) (Math.sqrt( (dx*dx) + (dy*dy) ));
}
protected boolean inrange(target T) // is tank in range?
{ return (distanceTo(T.xpos,T.ypos) <= gunrange); };
protected boolean target_insight()
{ boolean ax = false;
for (int i=0;i<A.tanknum;i++)
if ((ID != i) && (!A.Targ[i].destroyed) && (insight(A.Targ[i]))) ax = true;
return ax;
}
public double gunslope()
{ // compute equation for line of fire:
double gundr = gunposition * 0.01745; // radiant conversion
int gx = xpos + ((int) (25.0 * Math.sin(gundr)));
int gy = ypos + ((int) (25.0 * Math.cos(gundr)));
return ((1.0 * (gy-ypos)) / (1.0* (gx - xpos)));
}

public boolean aligned(target T)
{ // line equation is y == slope (x-xpos) + ypos
// compute y coord when x coord is T.xpos:
double gundr = gunposition * 0.01745; // radian conversion
int gx = xpos + ((int) (25.0 * Math.sin(gundr)));
int gy = ypos + ((int) (25.0 * Math.cos(gundr)));
double slope = (1.0 * (gy-ypos)) / (1.0* (gx - xpos));
int ty = (int) (slope * (T.xpos - xpos) + ypos); // target cord
boolean infront = ( distanceTo(T.xpos,ty) >
( Math.sqrt( ((gx-T.xpos)*(gx-T.xpos)) + ((gy-T.ypos)*(gy-T.ypos)))));
boolean temp = (Math.abs(ty-T.ypos) < 14);
if (slope != 0.0) // do same for x cord proximity
{ int tx = (int) ((T.ypos-ypos)/slope) + xpos;
temp = temp || (Math.abs(tx-T.xpos) < 14);
}
return (infront && temp);
} // end insight
public boolean insight(target T) // !!!!
{ return (inrange(T) && aligned(T)); }

protected void fire()
{ if (target_insight()) // firing at no target has no effect, sorry.
{
double gundr = gunposition * 0.01745; // radiant conversion
int gx = xpos + ((int) (25.0 * Math.sin(gundr)));
int gy = ypos + ((int) (25.0 * Math.cos(gundr)));
double slope = (1.0 * (gy-ypos)) / (1.0* (gx - xpos));
int tx = ((int) ( gunrange * Math.cos(gundr) )) + gx; //
int ty = (int) (slope * (tx - gx)) + gy;
A.monitor.fire(this,gx,gy,tx,ty,slope); // firing must be synched
try { Thread.sleep(40); } catch (Exception e) {} // delay
}
}
protected void rotateTurret(int newdir) // left or right?
{
if (newdir == left)
gunposition = (gunposition + rotangle) % 360;
else if (newdir == right)
{ gunposition = gunposition - rotangle;
if (gunposition < 0) gunposition = gunposition + 360;
}
A.monitor.drawto(ID,img,xpos,ypos,gunposition); // request draw
try { Thread.sleep(40); } catch (Exception e) {} // delay
} // end rotateTurret
protected void rotateTowards(target T) // doesn't work yet
{ double tdr = Math.atan(T.xpos/T.ypos); // degree of target
int td = (int) (tdr * 57.2958); // radian to degree
if (gunposition > td)
{ int a = gunposition - td;
int b = 360 - gunposition + td;
if (a > b) rotateTurret(left); else rotateTurret(right);
}
else
{ int a = td - gunposition;
int b = 360 - td + gunposition;
if (a > b) rotateTurret(right); else rotateTurret(left);
}
} // end rotateTowards
private void awayfrom(int x2, int y2) // prevents deadlock
{ if (xpos > x2) xpos = xpos + 2; else xpos = xpos - 2;
if (ypos > y2) ypos = ypos + 2; else ypos = ypos - 2;
}
protected void move(int dir) // move in a certain direction
{ int newx = xpos; int newy = ypos;
boolean change = true;
if (dir == north) { newy = newy - 4;
if (newy<yoff+15) newy = yoff+15; } // wrap
else if (dir == south)
{ newy = newy + 4;
if (newy>ah-15) newy = ah-15; }
else if (dir == east)
{ newx = newx + 4;
if (newx>aw-15) newx = aw-15; }
else if (dir == west)
{ newx = newx - 4;
if (newx < 15) newx = 15; }
xpos = newx; ypos = newy;
// detect collision
for(int i=0; i<A.tanknum;i++)
if ((i!=ID) && (!A.H[i].destroyed) &&
(distanceTo(A.H[i].xpos,A.H[i].ypos) < 35))
awayfrom(A.H[i].xpos,A.H[i].ypos);
A.monitor.drawto(ID,img,xpos,ypos,gunposition); // request change
try { Thread.sleep(40); } catch (Exception e) {} // delay
} // end move
abstract void manuver();
public void run() // thread function
{
A.monitor.drawto(ID,img,xpos,ypos,gunposition); // initial draw
while (true) manuver();
}// end run
} // end class tank

class AnimationMonitor // manages the animation to the applet
{ tanks A; // owner applet
Image buffer; // work "canvas"
Image base; // base background image - doesn't change
Graphics dg; // drawing device
int clientcount = 0; // number of drawing clients
/* draws only when clientcount gets to a certain number */
int requests; // number of drawing requests received so far
int srequests = 0; // synch call requests
AnimationMonitor(int c, Image ba, Image b, tanks x) // constructor
{ clientcount = c; // set number of drawing clients
A = x; buffer = b; base = ba;
dg = buffer.getGraphics();
requests = 0;
}
public void prepare()
{
dg.drawImage(base,0,0,null);
} // draw static background
public synchronized void drawto(int ID, Image img, int x, int y, int gund)
{ // requests++; // a client wants to draw!
double gundr = 0.0; int gx = 0; int gy = 0;
if (!A.H[ID].destroyed)
{
requests++;
gundr = gund * 0.01745; // radiant conversion
gx = x + ((int) (25.0 * Math.sin(gundr))); // gunposition
gy = y + ((int) (25.0 * Math.cos(gundr)));
dg.drawImage(img,x-15,y-15,null); // draw this client to buffer
dg.setColor(Color.black);
dg.drawLine(x,y,gx,gy); // draw Gun
// draw other tank too to avoid flicker!
if (ID == 0) ID = 1; else ID = 0;
if (!A.H[ID].destroyed)
{
img = A.H[ID].img;
x = A.H[ID].xpos; y = A.H[ID].ypos;
gund = A.H[ID].gunposition;
gundr = gund * 0.01745; // radiant conversion
gx = x + ((int) (25.0 * Math.sin(gundr))); // gunposition
gy = y + ((int) (25.0 * Math.cos(gundr)));
dg.drawImage(img,x-15,y-15,null); // draw this client to buffer
dg.setColor(Color.black);
dg.drawLine(x,y,gx,gy); // draw Gun
} // end draw other tank
(A.getGraphics()).drawImage(buffer,0,0,null); // display on screen
requests = 0; // reset and wait for clients;
dg.drawImage(base,0,0,null); // reprepare base image
for (int i=0;i<A.tanknum;i++) // update global target info
{ A.Targ[i].xpos = A.H[i].xpos;
A.Targ[i].ypos = A.H[i].ypos;
A.Targ[i].destroyed = A.H[i].destroyed;
A.Targ[i].gunposition = A.H[i].gunposition;
}
try { Thread.sleep(4); } catch (Exception e) {} // delay

} else
try { Thread.sleep(4); } catch (Exception e) {} // delay
} // end drawto
public synchronized void fire(tank FT, int gx, int gy, int tx, int ty, double slope)
{ if (!FT.destroyed)
{
boolean cease = false;
int t = 0;
while ((t<A.tanknum) && (!cease))
{ if ((FT.ID != t) && (!A.H[t].destroyed) && FT.insight(A.Targ[t])) // hit?
{ tx = A.H[t].xpos;
ty = (int) ( slope * (tx - gx) ) + gy;
A.H[t].destroyed = true;
A.Targ[t].destroyed = true;
cease = true;
clientcount--;
// A.H[t].stop();
(base.getGraphics()).drawImage(A.skullimg,
A.H[t].xpos-15,A.H[t].ypos-15,null);
if (clientcount < 2) A.showStatus("Tank "+ FT.ID + " Wins!");
}
t++;
} // end while
int oid = 0;
if (FT.ID == 0) oid = 1; else oid = 0;
A.H[oid].suspend();
Graphics ag = A.getGraphics();
for (t = 0; t<7; t++) // firing animation
{ ag.setColor(Color.red);
ag.drawLine(gx,gy,tx,ty);
try { Thread.sleep(20); } catch (Exception e) {} // delay
ag.setColor(Color.green);
ag.drawLine(gx,gy,tx,ty);
try { Thread.sleep(20); } catch (Exception e) {} // delay
}
} // else FT.suspend();
} // end fire
} // end class AnimationServer
public static class target
{ int xpos; int ypos; // target coordinates
public boolean destroyed = false;
public int gunposition; // degrees,
target(int x, int y) { xpos = x; ypos = y; }
} // end class target

/* put your code here */
class tank1 extends tank
{
tank1(int a, Image b, int c, int d, tanks e)
{ super(a,b,c,d,e); }

void manuver()
{ int rand1 = 0;
int rand2 = left;
while (!destroyed)
{
if (0 == ((int) (Math.random() * 10)))
rand1 = (int) (Math.random() * 5); // new direction
if (0 == ((int) (Math.random() * 10))) // change rotation
rand2 = -1 * rand2;
move(rand1);
rotateTurret(rand2);
if ( target_insight() )
{ fire();
}
} // !destroyed while loop
} // end manuver
} // end class tank1

class tank2 extends tank
{
tank2(int a, Image b, int c, int d, tanks e)
{ super(a,b,c,d,e); }

void manuver()
{ target Tg;
if ((ID%2)==0) Tg = A.Targ[1]; else Tg = A.Targ[0]; // set target
while (!destroyed)
{
while (!(inrange(Tg)))
{
fire();
if (target_insight()) fire();
if (Tg.xpos < xcord()) move(west); else move (east);
if (Tg.ypos < ycord()) move(north); else move(south);
} // inner while loop
while (!target_insight()) rotateTurret(left);
fire();
} // !destroyed while loop
} // end manuver
} // end class tank1

}


muchas gracias por la ayuda que me puedan dar