Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How do I delete an object from my program?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I delete an object from my program?

    So I have this bit of code, not complete but can give you complete code if needed, and so far, it makes a few shapes which is great and all but I need it to delete shapes on request. I want to make a brick game and basically I will edit this code so the ball bounces off the walls and hits bricks but before I do this I want to know how to at the very least know how to delete these shapes that I made so I can then use that code to practice making a brick game that will delete multiple elements.

    Long story short, I want to know how to make the squares disappear. The only thing I've heard so far is to set what I want equal to NULL, but I'm not sure how to do it.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package bouncing.ball;
     
    /**
     *
     * @author Marlin
     */
    import javax.swing.*;
    import java.awt.*;
     
    public class BouncePanel extends JPanel {
     
        //location of bouncing ball
        private int x;
        private int y;
        private int dx = 5;
        private int dy = 5;
     
        BouncePanel() {
            x = 100;
            y = 50;
     
            AnimationThread animethread = new AnimationThread(this);
            animethread.start();
        }
     
        public void paintComponent(Graphics g) {
            //first, clear screen
            super.paintComponent(g);
     
            g.drawLine(10, 20, 300, 200);
            g.drawRect(100, 100, 50, 70);
     
            g.setColor(Color.BLUE);
            g.fillOval(x, y, 40, 40);
        }
     
        public void update() throws InterruptedException {
     
            dy += 1;
            x += dx;
            y += dy;
     
     
            if (x < 0) {
                x = 0;
                dx = -dx;
            }
     
            if (x + 40 >= getWidth()) {
                dx = -dx;
            }
     
            if (y < 0) {
                y = 0;
                dy = -dy;
            }
     
            if (y + 40 >= getHeight()) {
                dy = -dy;
            }
     
            repaint();
        }
    }


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: How do I delete an object from my program?

    g.drawLine(10, 20, 300, 200);
            g.drawRect(100, 100, 50, 70);
     
            g.setColor(Color.BLUE);
            g.fillOval(x, y, 40, 40);

    it's only these lines that are using the paint method.

    inside the middle section of each (parameters) you will see 4 numbers (integers, x and y are also integers (variables)).
    Try to comment out the
    //g.drawRect(100, 100, 50, 70);

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I delete an object from my program?

    Quote Originally Posted by macko View Post
    Try to comment out the
    //g.drawRect(100, 100, 50, 70);
    Well, it's not that easy. I could comment it out but I want to get rid of it mid game. Not during compilation.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I delete an object from my program?

    Put the code inside of an if statement that is true when you want it executed and false when you don't want it executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I delete an object from my program?

    So in order to have the shapes drawn, I have to have them drawn every cycle and if I tell it to skip the statement then it will erase it? What if I wanted to draw multiple blocks that will erase when I hit them? How will I do that?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I delete an object from my program?

    skip the statement then it will erase it?
    That depends on how you code the paintComponent() method. A common way is to draw everything on every call
    which means there is no erasing done. If you don't want to see a shape, don't draw it.

    erase when I hit them
    Not sure what "hit them" means. If it means clicking on a shape with the mouse, that would require a mouse listener that would see where you clicked and would tell the paintCOmponent() method what shape is not to be drawn because it had been clicked on.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I delete an object from my program?

    all I want to do is make shapes at the top of the screen and make a paddle with a ball at the bottom and every time the ball hits the paddle and it hits the block the block disappears.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I delete an object from my program?

    There will need to be some way for the paintComponent() method to know if it is to draw a shape or not.
    One way is to have a boolean that says so
    Another way is to have the shapes in a list that paintComponent() uses and remove it from the list when it is not to be drawn.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I delete an object from my program?

    Would it be possible for me to put those shapes in an array so i don't have to write if statements for each and every one?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I delete an object from my program?

    Yes, or in an arraylist.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NEED HELP TO FIX Object oriented Program Facebook
    By Ryo in forum Object Oriented Programming
    Replies: 15
    Last Post: February 29th, 2012, 10:04 AM
  2. [SOLVED] wildcard delete
    By macko in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 1st, 2011, 03:31 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. Can I delete one of these two?
    By ice in forum Java IDEs
    Replies: 2
    Last Post: November 14th, 2010, 04:02 AM
  5. Object Oriented program, no output
    By boardbreaker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2009, 11:11 PM