Results 1 to 9 of 9
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default How to grab a graphical object from an arraylist and modify it

    Hello everyone again. I am now moving on to another problem which I must deal with, and one which I am not too familiar with. I am trying to grab a graphical object from an arraylist, and reposition its coordinates on a Jframe when adding it. My program of course deals with strings, and once it sees specific words in my console, some method is called that adds, removes, or otherwises modifies certain objects on screen. Here I want to say something like move(object[1],xpos,ypos) which will move a certain object from a specified point in the array, and move it to new x and y positions on the JFrame. I use a different class that extends a graphics program, so when I say add(something,x,y) it draws the object onscreen where I want it. These are some relevant, though incomplete, methods that should move an object already painted on screen:
    Console class
    Java Code:
    public void doMoveCommand(String cmd, String arg, String xpos, String ypos) {
            int x = Integer.parseInt(xpos);
            int y = Integer.parseInt(ypos);
    
            if (cmd.equals("posMake") && arg.equals("star")) {
                
    
                
                box.moveStar(box.historyG.get(1), x, y);  //historyG is an arraylist of GPolygons
                freeCommand();
            }
    Sandbox class
    Java Code:
    public void moveStar(GPolygon index,int xpos, int ypos){
            isSMoved=true;
            //historyG.get(index);
            undoStar();
            add(star,xpos,ypos);
        }
    When I say makepos(whatever) I am getting a arraylist out of bounds exception. How might I be able to fix that? Are there thoughts as to how I might be able to accomplish moving objects already on screen?
    Please feel free to shoot some questions, as I know I haven't explained very well what I am trying to accomplish.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to grab a graphical object from an arraylist and modify it

    I am getting a arraylist out of bounds exception. How might I be able to fix that?
    Compare the index against the size of the ArrayList and don't use it if it is past the end.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: How to grab a graphical object from an arraylist and modify it

    I realized when I was using box.historyG.get(1) when I had only put one object into the array was a mistake since My array would only have one object at the 0 spot. So I opted to use box.historyG.get(0) instead. I am however still getting a array index out of bounds exception. I guess I am unsure how to compare the size of the arrayList to the index.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to grab a graphical object from an arraylist and modify it

    how to compare the size of the arrayList to the index.
    Use a relationalship operator like > or < in a if statement:
    if(a > b)

    If there is one element in the list, the max index is 0
    if the list is empty, no index can be used.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default Re: How to grab a graphical object from an arraylist and modify it

    This actually got rid of my null ptr exception
    historyG=new ArrayList(10);
    but I still can reposition the actual object on the screen. Right now I am only trying to make one object onscreen, and then reposition that one object. I would think I would use box.historyG.get(0), but I think that is only returning the index. I want it to return the object so that way I can reposition it. How can I do that?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to grab a graphical object from an arraylist and modify it

    I want it to return the object
    Use the get() method to access an object that is in an ArrayList: historyG.get(index)
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to grab a graphical object from an arraylist and modify it

    if historyG is an array list, then historyG.size() should show the number of elements. If it is non-zero, then you have some elements starting
    an index 0 going to historyG.size() - 1.

    If you are still getting an index out of bounds exception, perhaps you are using the wrong box. Or you have inadvertently created a new
    box with an empty ArrayList.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: How to grab a graphical object from an arraylist and modify it

    Just a thought: it is not enough to create an instance of an array, one also has to instantiate the objects that go into the array. Maybe that was not the case when you called the get() and therfore the array is (still?) empty.
    Last edited by benji2505; 10-29-2014 at 02:25 AM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to grab a graphical object from an arraylist and modify it

    Quote Originally Posted by benji2505 View Post
    The objects in your array might not be instantiated when you run the get() method (and therefore the array is empty).
    How do you put something which has not been instantiated into anything? Perhaps you just meant to say the array is empty. But checking the size of the array has already been covered.

    Regards,
    Jim
    Last edited by jim829; 10-29-2014 at 02:33 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 09-06-2014, 04:47 AM
  2. Replies: 3
    Last Post: 02-18-2012, 02:00 AM
  3. Replies: 0
    Last Post: 11-14-2011, 06:54 AM
  4. Replies: 2
    Last Post: 08-12-2010, 07:30 AM
  5. How do I modify the values in an object array?
    By ConMan in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 11:26 PM

Posting Permissions

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