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

    Default How to remove objects in a a graphics program

    Hi I was wondering how to remove multiple objects that are onscreen in a java graphics program(specifically the one from the acm library). The clearAll works fine but I would like to keep the background in my frame. I just want to be able to do something like removing all the trees by typing in a phase into a console program. Any thoughts are how this might be achieved? He is a small snippet of code with details of what I am trying to achieve.

    Java Code:
    public void command(){
            Dictionary dic=new Dictionary();  
    //dic list of all phases that will either make animations or remove things in a graphical window
            String line = readLine("Type here: ");
            if (line.equals(dic.clearActions[i])) {
                cleared=true;
                ActionListener listen = new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                //box here refers to the graphical window.  The commands are issued
                                //in a console window
                                box.trunks.clear();  //tried making an array of tree trunks and clear them
                                box.repaint();
                                //box.remove(box.trunk);  I can only remove individual trunks and trees
                               // box.remove(box.leave);
                                
                            }
                        };

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: How to remove objects in a a graphics program

    Generally, you keep references to everything that you want to draw. This can be a List of shapes, for example. You might add or remove from that list of shapes when you push a button.

    Then when you paint, you first clear the screen, then simply iterate through that List and paint everything all over again.

    So to remove something from the screen, you simply remove it from the list and repaint the screen.

    If you want more specific help, please post an MCVE instead of a disconnected snippet.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

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

    Default Re: How to remove objects in a a graphics program

    You can also "remove" objects by just making them invisible. In Swing components, setVisible(false) retains the object but doesn't paint it. You can also add similar capabilities to your homegrown objects.

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

  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 remove objects in a a graphics program

    Also the classes that extend the Container class can use its remove() method to remove objects that had been added to it.
    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 remove objects in a a graphics program

    Ok I have tried something like this instead but unfortunately I can still only remove one GRect.
    Keep in mind that trunks is an array of GRects which I have made in another class. In the other class I make one GRect trunk and then I add that to the trunks ArrayList.
    Java Code:
    for (int i = 0; i < dic.clearActions.length; i++){
            if (isEarly==true && line.equals(dic.clearActions[i])) {
                cleared=true;
              for (Iterator<GRect> it = box.trunks.iterator(); it.hasNext();) {
                  GRect trunk=it.next();
                  trunk.setVisible(false);
            }
              box.trunks.clear();
            }
    I think the problem might be in the the other class where I make the actual background. Note I am only messing with trying to remove the trunks for now, I will handle the leaves later.
    Java Code:
     public void drawTree(double width, double height, double xpos, double ypos) {
            trunks = new ArrayList();
            trunk = new GRect(width / 15.0, height / 3.0);
            trunk.setFilled(true);
            trunk.setFillColor(Color.orange);
            leave = new GOval(width - 100, height / 5.0);
            leave.setFilled(true);
            leave.setFillColor(Color.GREEN);
            trunks.add(trunk);  //this is where I add the trunk to the ArrayList of trunks
            add(trunk, xpos + width / 2.0 - width / 2.8, ypos + height / 6.0);
            add(leave, xpos, ypos);
        }
    public void drawBackGround() {
            drawSky();
            drawGrass(1.0 / 2.0);
            for (int xpos = 10; xpos < getWidth() - 40; xpos += getWidth() / 10) {
                drawTree(150, 200, xpos, getHeight() / 2.0 - 50);
            }
        }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: How to remove objects in a a graphics program

    It's going to be pretty hard to help you without seeing an MCVE. This shouldn't be your whole project, but disconnected snippets don't really tell us much.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2013, 12:21 PM
  2. Remove Painted Image From Graphics
    By Ternary in forum New To Java
    Replies: 1
    Last Post: 06-20-2011, 08:35 AM
  3. Replies: 1
    Last Post: 12-04-2010, 03:07 PM
  4. Multiple Graphics Objects?
    By MrFish in forum Java 2D
    Replies: 7
    Last Post: 10-29-2010, 07:37 PM
  5. How do I remove objects from an Array in java?
    By hamed in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 01:20 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
  •