Vector Drawing?

I have imported the following Vec2 class into my project:

http://www.johnloomis.org/ece538/notes/java1/Vec2.java.html

I’m currently playing with the use of vectors as I am new to them.

I can’t seem to get the vector to draw using the Vec2.draw() method unless it is left in it’s constructed default position (0,0).

This is my code:

public class Avatar extends Sprite
{
    protected int hitPoints;
    protected int attackTime = 100; //ms
    protected int strength;
    protected boolean isAttack = false;
    Vec2 normal = new Vec2(posX, posY);

    public Avatar (int x, int y, int w, int h, int s, int hp, int str){
    super(w/2, h/2, w, h, s);
    hitPoints = hp;
    strength = str;
    }
    public void moveLeft()
    {posX-=step;}

    public void moveRight()
    {posX+=step;}

    public void moveUp()
    {posY-=step;}

    public void moveDown()
    {posY+=step;}

    public void attack(){
        if(isAttack)
        {isAttack = false;}
        else
        isAttack = true;
        System.out.println(normal.length());
    }

    public void isHit(int str){
        hitPoints-=str;
        if(hitPoints<= 0){
            this.dead();
        }//endif
    }

    public void dead(){
        //called when player hp <=0
    }
@Override
public void drawSprite(Graphics g){
    
        if(isAttack){
        g.setColor(Color.blue);
        g.fillOval(posX - 10, posY - 10, 30, 30);
        g.setColor(Color.black);
        
       }
        if(isActive()){
        g.setColor(Color.orange);
        g.fillOval(posX, posY, 10, 10);
        g.setColor(Color.black);
        }
         normal.draw(g);
}

}

I have checked its length with a print line and it returns two every time but does not draw to the canvas, I called its draw() method last to ensure it is drawn last (on top), but that doesn’t seem to make a difference.

Any help would be much appreciated :slight_smile:

For starters i should point out that a normal vector actually represents a direction not a point (however looking over your code i don’t think you meant what I’m on about here).

Secondly, I cannot see any thing wrong with the implementation of the drawing except i think the issue is where you position the vector

Vec2 normal = new Vec2(posX, posY);

This piece of code is outside the constructor when it should really be inside it like this


public Avatar (int x, int y, int w, int h, int s, int hp, int str){
    super(w/2, h/2, w, h, s);
    hitPoints = hp;
    strength = str;
    normal = new Vec2(posX, posY);
}

I’m not sure, but there seems to be some weird example specific code inside of Vec2.java:

try to replace this in the draw method:

public void draw(Graphics g)
{
    int [] iv = convert();
    g.fillOval(iv[0]-4,iv[1]-4,9,9);
} 

with this:

public void draw(Graphics g)
{
    g.fillOval(v[0]-4,v[1]-4,9,9);
} 

don’t know what your coordinate system looks like, so can’t say if that’ll help…