import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class tank extends JApplet implements KeyListener
{
int x,y;
int enemies,hits;
private Color TANK_COLOR = new Color(0,100,0);
public void init()
{
x=100;
y=100;
enemies=10;
hits=0;
resize(500,500);
addKeyListener(this);
}
private void drawBackground(Graphics c)
{
c.setColor(Color.yellow);
c.fillRect(0,0,getWidth(),getHeight());
}
private void drawTank(Graphics c)
{
c.setColor(TANK_COLOR);
c.fillRect(x,y,10,10);
}
public void paint(Graphics c)
{
drawBackground(c);
drawTank(c);
c.setColor(Color.BLACK);
c.drawString("ENEMIES : " + enemies,50,30);
c.drawString("HITS : " + hits,375,30);
}
private void moveDown()
{
y+=10;
}
private void moveUp()
{
y-=10;
}
private void moveLeft()
{
x-=10;
}
private void moveRight()
{
x+=10;
}
public void keyTyped(KeyEvent ev){}
public void keyReleased(KeyEvent ev){}
public void keyPressed(KeyEvent ev)
{
int code = ev.getKeyCode();
switch (code)
{
case 38: // up arrow
moveUp();
break;
case 40: // down arrow
moveDown();
break;
case 39: // right arrow
moveRight();
break;
case 37: // left arrow
moveLeft();
break;
}
repaint();
}
}