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
need the attention of a java programmer.. Posted by czaiza on 7 Oct 2010 at 9:50 PM
i am a student and new to java programming..
can yOu please heLp me to make my tank move?
just need it to go around the applet,
up, down, left and right directions.
please someone heLp!
here's the code for my tank:

import javax.swing.*;
public class tank extends JApplet implements KeyListener{
int x1,x2;
int y1,y2;
int enemies,hits;
public void init(){
x1=0;
x2=0;
y1=0;
y2=0;
enemies=10;
hits=0;
resize(500,500);
}
public void paint(Graphics c){
c.setColor(Color.yellow);
c.fillRect(0,0,500,80);
c.setColor(Color.yellow);
c.fillRect(0,0,80,500);
c.setColor(Color.yellow);
c.fillRect(420,80,80,420);
c.setColor(Color.blue);
c.drawRect(180,320,150,150);
c.drawRoundRect(180,320,30,150,10,10);
c.drawRoundRect(210,345,90,100,10,10);
c.drawRoundRect(230,370,50,50,10,10);
c.drawRect(249,290,10,80);
c.drawRect(244,280,20,10);
c.drawRoundRect(300,320,30,150,10,10);
c.fillPolygon(x,y,3);
c.drawString("ENEMIES : " + enemies,50,30);
c.drawString("HITS : " + hits,375,30);
}
public void keyTyped(KeyEvent ev){}
public void keyReleased(KeyEvent ev){}
public void keyPressed(KeyEvent ev){}
}


please anyone heLp me..
teach me the missing lines of codes to make my program complete..
please.........
thanks in advance. :)
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();
}
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.