Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Snake006

Thread: how to specify how many objects you want to delete

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

    Default how to specify how many objects you want to delete

    I have a question about how to delete only a certain amount of objects on screen in a graphics window. I have a method that will enable me to get rid of every object of the same type off screen, for example this method:
    Java Code:
    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 Will delete all trees.
    Now In my main method I populate the screen with trees by doing this

    Java Code:
    for (int xpos = 10; xpos < getWidth() - 40; xpos += getWidth() / 8) {
                drawTree(150, 200, xpos, getHeight() / 2.0 - 50);
                historyT.add(trunk);
                historyL.add(leave);
    
            }
    My question is how to specify only deleting x amount of trees instead of the whole thing. I pretty sure I need to use something like history.remove(trunk) and to iterate through the amount of trees specified, but not sure how to implement that. Any thoughts on how that might look?

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

    Default Re: how to specify how many objects you want to delete

    how to specify only deleting x amount of trees
    Stop the loop when x trees have been deleted.
    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 specify how many objects you want to delete

    I'm am just unsure how to do that in a for loop that uses a iterator like this:
    for (Iterator<GRect> it = historyT.iterator(); it.hasNext();) {
    GObject gobj = it.next();

    this.remove(gobj);
    }
    I am still sort of new to these kind of for loops, but I really want to better understand them. How might I specify iterating through only part of the list?

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

    Default Re: how to specify how many objects you want to delete

    iterating through only part of the list?
    The break statement can be used to exit the loop at any time. Use a counter to determine when x trees have been deleted.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: how to specify how many objects you want to delete

    Quote Originally Posted by Snake006 View Post
    I'm am just unsure how to do that in a for loop that uses a iterator like this:
    I am still sort of new to these kind of for loops, but I really want to better understand them. How might I specify iterating through only part of the list?
    Iterators are created by the designer of the class. Mostly they are designed to iterate a complete list. If you want to iterate only
    a portion of a list you would have to write a method that returns a customized iterator to iterate only the portions you specify. Another
    way would be to selectively skip values you aren't interested in or use counters to control the number of iterations.

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

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

    Default Re: how to specify how many objects you want to delete

    It would seem like the easiest way would be just to use counters, although I'm not sure if something like a nested for loop for work here. I know how to use counters with normal for loops but here I am unsure of how to implement an counter, since I am dealing with an iterator. Any examples would help greatly

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

    Default Re: how to specify how many objects you want to delete

    Of course I can manage to delete just one object by doing this:
    Java Code:
    public void deleteSomeTrees(){
            //int x=5;
            Iterator<GRect> it=historyT.iterator();
            GObject gobj=it.next();
            Iterator<GOval> it2 = historyL.iterator();
            GObject gobj2=it2.next();
            //for(int x=0;x<=5;x++){
            this.remove(gobj);
            this.remove(gobj2);
            historyT.remove(1);
            historyL.remove(1);
            //}
        }
    Unfortunately I can't just call that method over and over again to get the desired results. Neither can I use a for loop for how many times I want to remove the gobj, since that is in relationship with it.next().

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

    Default Re: how to specify how many objects you want to delete

    Quote Originally Posted by Snake006 View Post
    It would seem like the easiest way would be just to use counters, although I'm not sure if something like a nested for loop for work here. I know how to use counters with normal for loops but here I am unsure of how to implement an counter, since I am dealing with an iterator. Any examples would help greatly
    Here is one example of how it could be done:

    Java Code:
          for (int count = 0; it.hasNext(); count++) {
             it.next();
             if (count >= 3 && count <= 5) {
                it.remove();
             }
          }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: how to specify how many objects you want to delete

    Ok that makes sense. However, in my case the example given above only removes the object from the list, not from the screen and I need to be able to do both. Of course in order to do that I would need to create a goObject like GObject gobj=it.next(); but by using that, only one object on screen is removed when I use this.remove(gobj);
    Last edited by Snake006; 10-11-2014 at 08:59 PM.

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

    Default Re: how to specify how many objects you want to delete

    How might I get rid of the specified objects on screen?

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

    Default Re: how to specify how many objects you want to delete

    It depends on the nature of the objects. If they were say JComponents you could set visibility false or remove them from their
    parent container (or both). If they are homegrown geometric figures which are painted, then they could be removed from the list
    of objects which are being painted. Then call repaint(). I can't tell you specifically for your situation though.

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

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

    Default Re: how to specify how many objects you want to delete

    Yes I should specify. The objects are things like simple GRects, GOvals, etc. Using this.remove(gobj) gets rid of one object. It would be nice to simply say this.remove(gobj, x) where x is equal to the number of objects I want to be removed but I don't think remove has that capability. Once again I can iterate through all the objects and delete all of them, I just am not sure how to delete x amount of objects. I get remove objects from the iterator, I just want to then actually get rid of those objects on screen.

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

    Default Re: how to specify how many objects you want to delete

    First, before you can delete some number of objects you need to keep track of them. What are you removing them from? A JPanel? A List?
    If you have a list of objects you should be able to delete 0 or all or some number in between by checking some criteria (such as type, location,
    size, etc). That's all I can tell you because I am not familiar with the classes you are using.

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

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: how to specify how many objects you want to delete

    If you're up to Java 8 there's a fairly concise, procedural syntax using the new features.
    Java Code:
    list.stream().filter(predicate).collect(Collectors.toList();
    Where predicate could be a lambda expression or a method reference.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: how to specify how many objects you want to delete

    I figured it out. This will undo the last tree object
    Java Code:
    public void undo() {
            //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);
            }
        }
    So basically if I want to remove a tree twice I can just call the method twice. I just tried it and it worked. I know there are more elegant and efficient ways of doing what I just described so I will now see if I can enhance the above method.
    DarrylBurke likes this.

Similar Threads

  1. Replies: 3
    Last Post: 07-31-2014, 10:47 AM
  2. Sample to create, insert, modify and delete MySQL Objects
    By dbblogger in forum Reviews / Advertising
    Replies: 3
    Last Post: 06-30-2012, 12:24 PM
  3. Replies: 12
    Last Post: 04-29-2012, 03:55 PM
  4. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 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
  •