Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Getting all objects cleared off screen

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

    Default Getting all objects cleared off screen

    Hello there again. I am trying to clear all graphical objects of a certain type(all trees in particular). I have never had any issues doing so, and this method normally works well along with these variables:

    Java Code:
    public static ArrayList<GRect> historyT;
    public static ArrayList<GOval> historyL;
    public static GRect trunk;
    public static GOval leave;
    public void deleteTrees() {
            //clears all tree trunks and leaves
            for (Iterator<GRect> it = historyT.iterator(); it.hasNext();) {
                GObject gobj = it.next();
    
                this.remove(gobj);
            }
            for (Iterator<GOval> it2 = historyL.iterator(); it2.hasNext();) {
                GObject gobj = it2.next();
    
                this.remove(gobj);
            }
            historyT.clear();
            historyL.clear();
        }

    That is this normally worked until I started added more trees to the screen after they had all been erased. What happens is that it won't clear all of them unless the max has been reached, that is 6. It will remove all trees up till the last one if the max has not been reached. In other words here is my screen before the clear all if the max has not been reached(* is a tree, _ is erased): ****
    Here is it after: _ _ _ _ *
    So my question is, is why aren't all the trees deleted? Why does it leave one left behind?

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

    Default Re: Getting all objects cleared off screen

    It is worth noting every time I create a tree I do this:
    historyT.add(trunk);
    historyL.add(leave);

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

    Default Re: Getting all objects cleared off screen

    Furthermore the problem extends to this method as well:
    Java Code:
    public void undoTree() {
            //deletes one tree at a time
            if (historyT.size() > 0 && historyL.size()>0) {
                GObject LastGObject = historyT.get(historyT.size() - 1);
                GObject LastGObject2=historyL.get(historyL.size()-1);
                historyT.remove(LastGObject);
                historyL.remove(LastGObject2);
                this.remove(LastGObject);
                this.remove(LastGObject2);
            }
        }
    I probably should detail the problem some more to make what I am trying to do is clear, since my last explanation may have been poor at best.
    So lets say I have a screen with 4 trees. I use the delete tree method and all are erased except for the furthest tree on the right. This happens up until 6 trees are onscreen(note there special about the number six, that is just the array limit for the trees, and only six trees can fit on the screen anyway).

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

    Default Re: Getting all objects cleared off screen

    Just to provide some more necessary detail, here is an example of me adding all 6 trees onscreen:
    Java Code:
    for (int xpos = 10; xpos < getWidth() - 40; xpos += getWidth() / 6) {
                drawTree(150, 200, xpos, getHeight() / 2.0 - 50);
                historyT.add(trunk);
                historyL.add(leave);
                //treeNum=6;
            }

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

    Default Re: Getting all objects cleared off screen

    Can you make a small, complete program that compiles, executes and shows the problem for testing?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Getting all objects cleared off screen

    Oh gosh I just figured it out, I should have been using historyT.add(trunk) and historyL.add(leave) in the actual draw method. Simple mistake.
    Now I just need to make it so it will remember what tree I'm on when I'm drawing it again. Right now if all clearAll after making 4 trees, then when I start adding again it will start at the 4th position onscreen, or at the fourth place in the tree arrays. I just need to be able to reset the array to 0. I might need help with this one, but I might not.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Getting all objects cleared off screen

    Quote Originally Posted by Snake006 View Post
    I might need help with this one, but I might not.
    Given your recent history of asking questions and then quickly figuring it out yourself, I think you need to have a little more faith in your capabilities. And perhaps a tiny bit more patience to figure stuff out.
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 8
    Last Post: 08-16-2012, 09:41 PM
  2. Replies: 1
    Last Post: 08-11-2009, 03:19 PM
  3. Replies: 2
    Last Post: 08-11-2009, 02:58 PM
  4. Replies: 0
    Last Post: 03-09-2009, 06:55 PM
  5. Replies: 1
    Last Post: 08-09-2008, 12:10 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
  •