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; }
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
ReplyDeletesory my english bad...
This site really has all of the information
ReplyDeleteand facts I needed concerning this subject and didn't know who to ask.
Also visit my web site - lame de terrasse en composite
Hey this is somewhаt of оff topіc but I was ωonԁеring іf
ReplyDeleteblο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
I was suggested this website by my cousin. I am now
ReplyDeletenot 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
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
ReplyDeletewhen 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
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?
ReplyDeleteI'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
Excellent beat ! I wish to apprentice even as you amend your
ReplyDeletewebsite, 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
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
ReplyDeletebe able to resolve this problem. If you have any suggestions, please share.
Cheers!
Here is my webpage plombier paris 1er
It's remarkable for me to have a web site, which is good in favor of my experience. thanks admin
ReplyDeleteMy web-site; imitation parquet
Have you ever considered about adding a little bit more than just your articles?
ReplyDeleteI 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
It's a pity you don't have a donate button!
ReplyDeleteI'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