Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

need the attention of a java programmer..

czaizaczaiza Posts: 2Member
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:

[code]
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){}
}
[/code]

please anyone heLp me..
teach me the missing lines of codes to make my program complete..
please.........
thanks in advance. :)

Comments

  • Josh CodeJosh Code Posts: 675Member
    [code]
    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();
    }
    }
    [/code]
Sign In or Register to comment.