Results 1 to 6 of 6
  1. #1
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Moving elements in a vector

    Hey guys,

    I'm creating a snake game. My snake is made from a vector of shapes (the shape I am using is a custom shape class). The snake is implementing a keylistener.

    When I call a move method on my snake I want the head, which is a separate element from the body, to move and each subsequent element in the vector to take the place of the one in front of it.

    So I would like the first element in the vector to be at the heads previous location and the second element in the vector to be at the first, etc etc.

    At the moment I'm using this method.

    Java Code:
        if(ke.getKeyCode() == KeyEvent.VK_UP)
        {
            xLoc = head.getXLocation();
            yLoc = head.getYLocation();
            head.setLocation(xLoc, yLoc-12);
          for(int i = 0;i<5;i++)
          {
            body.get(i).setLocation(xLoc,yLoc);
            xLoc = body.get(i).getXLocation();
            yLoc = body.get(i).getYLocation();
          }
        }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Moving elements in a vector

    each subsequent element in the vector to take the place of the one in front of it.
    Have you read the API doc for the Vector class?
    Do any of its methods look like they will do what you want?

  3. #3
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Moving elements in a vector

    Well I think the get method will be fine, I'm just writing my method wrong. I am assigning each element in the vector to a single location, but I can't figure out the semantics to fix it.

    I have a point A that is being adjusted. Each time point A is adjusted to point B, I want a vector of points to be adjusted like this

    [A] Vector[1,2,3,4,5]

    After

    [B] Vector [A,1,2,3,4]

    But right now it looks like this

    [A] Vector[1,2,3,4,5]

    after

    [B] Vector[A,A,A,A,A]

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Moving elements in a vector

    Can you post a small program that compiles and executes and shows your problem?

  5. #5
    dru36 is offline Member
    Join Date
    Nov 2011
    Location
    New Hampshire
    Posts
    29
    Rep Power
    0

    Default Re: Moving elements in a vector

    Here is the basic problem I'm having. It's not my program but it shows the area that I need assistance in.

    You can see here that I'm assigning the values of all the components in the vector to the head value instead of assigning them the next position in the vector.

    Java Code:
    import java.awt.event.*;
    import java.util.*;
    import java.awt.Point;
    import java.awt.Color;
    
    public class Snakes implements KeyListener
    {
        private Point   last,  next;
        private int head,  tailPiece;
        private Vector<Integer> body;
        private int xLoc, yLoc, headX, headY;
    
        private int direction = 2;
    
        public Snakes ()
        {
    head = 100;
          
    
          
          
          body = new Vector<Integer>();
          
          for(int i= 0; i<5; i++)
         {
    tailPiece = i*12;
           body.add(tailPiece);
         }
    
        }
        public void keyTyped(KeyEvent e)
        {
        }
    
        public void keyPressed(KeyEvent ke)
       {        
        if(ke.getKeyCode() == KeyEvent.VK_UP)
        {
    
            head += 10;
          for(int i = 0;i<5;i++)
          {
            int x = body.get(i);
            x = head;
            
            System.out.println("Vector value at i: " +x);
            System.out.println("Snakes Head: " + head);
            System.out.println("Integer i: " + i);
    
          }
        }
        }
        public void keyReleased(KeyEvent e)
        {
        }
        public static void main(String[]args)
        {
          Frame f = new Frame();
          Snakes s = new Snakes();
          f.addKeyListener(s);
          
        }
    }
    Also, here is the list it prints out on the first key stroke UP.


    Vector value at i: 110
    Snakes Head: 110
    Integer i: 0
    Vector value at i: 110
    Snakes Head: 110
    Integer i: 1
    Vector value at i: 110
    Snakes Head: 110
    Integer i: 2
    Vector value at i: 110
    Snakes Head: 110
    Integer i: 3
    Vector value at i: 110
    Snakes Head: 110
    Integer i: 4

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: Moving elements in a vector

    I'm assigning the values of all the components in the vector to the head value instead of assigning them the next position in the vector.
    Where is the head value and where is the next position?

    The only methods I see you are using are the add and get methods.
    [A] Vector[1,2,3,4,5]

    After

    [B] Vector [A,1,2,3,4]
    In this example you gave the difference between A and B is that 5 was removed from the end of the Vector and A was inserted at the beginning of the Vector.

    Go read the API doc and see what methods will do that.

    Then write a 10 line program to test out the methods. Those 10 lines include two printlns to show the before and after contents of the Vector.
    Last edited by Norm; 12-06-2011 at 03:06 AM.

Similar Threads

  1. Replies: 3
    Last Post: 10-19-2011, 11:42 PM
  2. swapping elements of vector
    By sara12345 in forum New To Java
    Replies: 1
    Last Post: 01-07-2010, 09:45 PM
  3. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  4. compare newly added Vector Element with previous elements
    By nidhirastogi in forum Advanced Java
    Replies: 10
    Last Post: 09-10-2008, 01:32 AM
  5. Finding elements in a vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •