BBC (Brian's Boot Camp)

Not to be confused with the British Broadcasting Corporation, the BBC will focus on Brian's fast paced journey of the mastery of the Java programming language.

Wednesday, March 02, 2005

Assignment #4: The Ubiquitous Bouncing Balls

Ah, the bouncing balls app. A program Paul had me write in the early days, and then in turn that I asked Dave to write a year later, and now 7 years after that, you get to take a wack at this timeless classic (hmmm, wacking at balls?). This assignment covers GUI, Threads, animation, drawing, creating your own classes, and events; it's kind of a non-interactive game. Just in time for your solo weekend too:

Write a program that displays a frame. The top part of the frame should have a black background, this is the ball cage. The bottom of the frame contains a button labeled "Add Ball". When pressed, it will add a "ball" (a circle) to the cage that will head off in some direction and "bounce" off the borders of the cage indefinitely. Subsequent presses of the button will continue to add balls.

Notes:
1) It's up to you of course, but now is a good time to start doing some Object Oriented design. Consider writing a Ball class. You could even go crazy and write a BallCage class too...

2)Some extra options: make each ball a random color, and give it an initial random direction and velocity (a bunch of balls all the same color going the same direction and speed isn't very interesting).

3) Don't worry about collision detection between balls -- just the walls.

This will be your most ambitious effort yet. But for your reference, I wrote this from stratch in real-time, in front of a high school class in a 50 minute class period.

4 Comments:

Blogger Brian T. Grant said...

I skimmed through the next chapter which deals with graphics. My guess is that it'll get me as far as building the interface you describe as well as adding balls. But I didn't see anything in there that had to do with motion. Can I get a suggestion or two about places to look for that? You can even give me packages to look into so I'm still forced to dig around a little. I'm asking in advance because I don't want to hit a roadblock this weekend during my long haul.

By the end of the day Friday, I'll be finished with the 21 Days book per our prior discussion on what chapters to look at/ignore. So its all assignments and the Java Language book from that point forward, I guess. With Assigments #2 & 4 as well as Stacktropolis code and the Java Language book, I've got more than enough to go on.

1:47 PM  
Blogger Sten said...

I'll try to post a Stacktropolis based assignment before the week is out in case you get that far.

Yeah, over the weekend, don't hesitate to call me if you hit a block. You don't want to waste too much precious alone time banging your head against the wall.

Here are some hints on how to do the animation. Each hint gives progressively more away, so beware the spoilers:

1) It's not an API thing, that is you're not looking for an Animation class or anything (although you could certainly write one yourself).

2) The way should become more clear after you read up on Graphics objects and drawing.

3) You want an animation loop.

4) Your pseudo-code would look similar to:

while (!done) {
updateBallPositions();
repaint();
}

5) The above loop will want to run in a different thread.

2:15 PM  
Blogger Sten said...

As a final hint, the Stacktropolis codebase has all the information you need about animation, custom classes, multi-threading, etc. So you can check it out if you get stuck.

It's not really cheating since learning to read others' code is a skill you'll need as well.

2:24 PM  
Blogger Brian T. Grant said...

Hah! I stopped reading after item 2. Dave and I were e-mailing and he said that, once I know how to add a ball, I'll know how to make it move, too.

I'll work on what I've got.

3:27 PM  

Post a Comment

<< Home