Monday, September 13, 2010

Animated Bouncing Ball- The Bill Version

After I wrote up my Animated Bouncing Ball solution, my friend Bill left a comment. His suggestion had to do with how I tracked the location of my ball so I'd know when it had hit a wall and should reverse directions. My solution had been to declare the variables x and y, set them to the initial x and y coordinates of my ball, and change their value after each move the ball made to make them match the new coordinates. As Bill said, this is quite poor because you're tracking x and y in two places, leaving it inefficient and susceptible to bugs.

The truth is, I had wanted to use a built-in getX() kind of method, but I didn't think such a method existed, since it wasn't listed in the textbook. Then I talked to Andrew, a friend from work, and realized that not only were there hundreds of other methods for GOval than were in the book, but that Java has dozens of ways to conceptualize an oval, only one of which is the GOval class. It's like I've been walking around with blinders on and suddenly the blinders have come off and I realize there's much more than I even knew to look for. As my friend Jane says, half of programming is just knowing what to Google.

So, I looked up to see whether I can use getX() with the GOval class to make my program more efficient, and lo and behold, I can! New and improved code below.


/*
* File: BounceBall.java
* Name: 
* Section Leader:
* -----------------

*/

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class BounceBall extends GraphicsProgram {
    public void run() {
        
//Create ball in initial centered position
     int centeredx = (getWidth()-DIAMETER)/2;
     int centeredy = (getHeight()-DIAMETER)/2;
        
     GOval ball = new GOval (centeredx, centeredy, DIAMETER, DIAMETER); 
        ball.setFilled(true);
        add(ball);

        int dx = 1; //Setting the size of the ball's steps
        int dy = 1;
        
        while(true) { //Getting the ball to move
            ball.move(dx, dy);
            pause(PAUSE_TIME);

//  Declare x and y variables to locate the ball
          
            double x = ball.getX(); 
            double y = ball.getY();
            
//"If" statements to test whether the ball is at a wall, and if so, reassign dx or dy to switch direction.
            
            if (x >= getWidth()-DIAMETER) { //If it hits the right-hand wall`
             dx=-dx;     
            }
            if (y >= getHeight()-DIAMETER) { //If it hits the bottom
             dy=-dy;
            }
            if (x == 0) { //If it hits the left-hand wall
             dx=-dx;
            }
            if (y == 0) {//If it hits the top
             dy=-dy;
            }
        }
        }
        
//Private Constants
        
        private static final int DIAMETER = 40;
        private static final int PAUSE_TIME = 20;     
      
    }

11 comments:

  1. nice one renee...can i ask you?that's one ball bounce right?how i can make two ball and the ball can bounce each other...thakz before
    sory my english bad...

    ReplyDelete
  2. This site really has all of the information
    and facts I needed concerning this subject and didn't know who to ask.

    Also visit my web site - lame de terrasse en composite

    ReplyDelete
  3. Hey this is somewhаt of оff topіc but I was ωonԁеring іf
    blοgs uѕe WҮЅIWYG edіtors οr if you
    havе to manually coԁе with HТϺL.

    I'm starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

    my web-site - christophe chavigny

    ReplyDelete
  4. I was suggested this website by my cousin. I am now
    not positive whether or not this put up is written by means of him as
    nobody else recognize such particular about my problem. You're amazing! Thanks!

    Here is my webpage plombier paris 14

    ReplyDelete
  5. Greetings from Idaho! I'm bored to death at work so I decided to browse your site on my iphone during lunch break. I really like the information you present here and can't wait to take a look
    when I get home. I'm shocked at how quick your blog loaded on my phone .. I'm not
    even using WIFI, just 3G .. Anyways, superb blog!

    Stop by my blog devis porte fenetre pvc

    ReplyDelete
  6. I know this if off topic but I'm looking into starting my own weblog and was wondering what all is needed to get set up? I'm assuming having a blog like yours would cost a pretty penny?

    I'm not very web savvy so I'm not 100% certain.
    Any recommendations or advice would be greatly appreciated.
    Cheers

    Look at my homepage :: volet roulant

    ReplyDelete
  7. Excellent beat ! I wish to apprentice even as you amend your
    website, how could i subscribe for a weblog
    site? The account aided me a applicable deal. I had been tiny bit acquainted of this your broadcast offered bright
    transparent concept

    Feel free to surf to my weblog fenetre pvc sur mesure

    ReplyDelete
  8. Hi! Quick question that's totally off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone4. I'm trying to find a template or plugin that might
    be able to resolve this problem. If you have any suggestions, please share.
    Cheers!

    Here is my webpage plombier paris 1er

    ReplyDelete
  9. It's remarkable for me to have a web site, which is good in favor of my experience. thanks admin

    My web-site; imitation parquet

    ReplyDelete
  10. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is important and all.
    But think of if you added some great images or videos to
    give your posts more, "pop"! Your content is excellent but with pics and clips, this site could
    undeniably be one of the greatest in its niche. Great blog!


    Also visit my web page: decorateur mariage

    ReplyDelete
  11. It's a pity you don't have a donate button!

    I'd definitely donate to this outstanding blog! I guess for now i'll settle
    for book-marking and adding your RSS feed to my Google account.

    I look forward to new updates and will talk about this website with
    my Facebook group. Chat soon!

    My web-site ... portail coulissant electrique

    ReplyDelete