Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: need the attention of a java programmer.. Posted by Josh Code on 15 Oct 2010 at 4:52 PM
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();
}
}
Thread Tree
czaiza need the attention of a java programmer.. on 7 Oct 2010 at 9:50 PM
Josh Code Re: need the attention of a java programmer.. on 15 Oct 2010 at 4:52 PM



 

Recent Jobs