HTW Berlin Medieninformatik HTW Berlin
Fachbereich 4
Internationaler Studiengang
Internationale Medieninformatik (Bachelor)
Info 1: Informatik I
Summer Term 2014

Laboratory 6 : Bouncing Balls

Pre-lab

This week's lab work is intended give you practice working with the basic concepts of programming.

What to Bring to Lab

Please bring these exercises printed out or written out with you to lab. Please have your name on your page.

P1. Download the Balls project. Create a BallDemo object and execute the drawDemo and bounce methods. Then read the BallDemo source code. Describe, in detail, how these methods work for your report.

P2. Read the documentation of the Canvas class. Then answer the following questions in writing, including fragments of Java code:

    1. How do you create a Canvas?
    2. How do you make it visible?
    3. How do you draw a line?
    4. How can you erase something?
    5. What is the difference between draw and fill?
    6. What does wait do?

Post-Lab, AKA  What To Turn In

Your completed assignment, submitted in Moodle as a pdf, should include:

Lab assignments are due before your next lab at 22.00. They may, of course, be turned in earlier. You hand them in by preparing the report in PDF and submitting it to Moodle.


Assignment

Bouncing Balls

  1. Install the project in the lab. Experiment with canvas operations by making changes to the drawDemo method in BallDemo. Draw some more lines, shapes and text.
  2. Draw a frame around the canvas by drawing a rectangle 20 pixels inside the window borders. Put this functionality into a method called drawFrame in the BallDemo class.
  3. Improve your drawFrame method to adapt automatically to the current canvas's size (that is, do not hard-code the size of the canvas into this method. To do this, you need to find out how to make use of an object of class Dimension. (Hint: check the API) Test it by manually resizing the canvas and calling drawFrame again.
  4. Change the method bounce to let the user choose how many balls should be bouncing. Use a collection to store the balls so that you can deal with one, three, or 75 balls at once. The balls should initially be placed in a row along the top of the canvas. But what kind of a collection? We know about ArrayList, HashMap, and HashSet. Explain in your report why you chose the collection you did.
  5. Change the method bounce to place the balls randomly anywhere in the top half of the screen.
  6. (For the bored) Write a new method named boxBounce. This method draws a rectangle (the 'box') on the canvas, and one or more balls inside the box. For the balls, do not use BouncingBall, but create a new class BoxBall that moves around inside the box, bouncing off the walls of the box so that it always stays inside. The initial position and speed of the ball should be random. The boxBounce method should have a parameter that specifies how many balls are in the box.
  7. (For the really bored) Give the balls in boxBounce random colors. Include a method to change the size. Make balls that get larger and smaller. Make exploding balls that produce new ones when they disappear. Or whatever catches your fancy.

Writeup

Submit your writeup as your post-lab to the Moodle area.

 


Some rights reserved. CC-BY-NC-SA Prof. Dr. Debora Weber-Wulff - CC-BY-NC-SA
Questions or comments: <weberwu@htw-berlin.de>


The exercises are adapted from Objects First with Java, A Practical Introduction Using BlueJ. David Barnes & Michael Kölling, 2011