FHTW Berlin

FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG1: Programmierung I
Wintersemester 03/04


Laboratory 12: Preparation for Scribble


The laboratory project for next week, the Scribble, will introduce you to AWT programming. The application that you will build is a line-drawing scribbler program.

This time you will not be provided with a Main class. Instead, you will be supplied with some of the basic component types that you will need to make your program work. You will also be given a smart repository that knows how to keep track of and draw lines -- but not (yet) which lines to manage.

Your job will be to glue these pieces together to provide a working Scribble application. This assignment emphasizes the following topics :

You should read through this entire assignment and complete the Lab preparation section before lab next week.

Contents

Pre-Lab

A writeup of the finger exercises should always be turned in along with your completed laboratory next week. Use the time this week to get them done.

A. Finger exercises

Using the Java AWT Quick Reference appendix or Sun's online Java Applications Programmer Interface (API) documentation, answer the following questions. For each question, give both the answer to the question and the way that you found this answer.

[CHECK-IN] [TURN-IN]

  1. Define a class that implements java.awt.event.MouseListener and extends the mouseClicked(MouseEvent) method by printing the coordinates of the point on which the mouse had clicked. You may also want to make use of the class java.awt.event.MouseAdapter. (Bonus: also print the components of the previous mouse click.)
  2. Now define a class that extends java.awt.Canvas and sends its mouse events to your MouseListener.
  3. Define a class that implements java.awt.event.WindowListener and extends the windowClosing()method by printing "Nah, nah, you can't kill me!" (Alternately, you can do the potentially more useful thing and (1) call the object's dispose() method and (2) call System.exit(0).) What class do you think would be useful when implementing WindowListener?
  4. What methods does a Graphics have that allow you to make things appear on it? (One such method is drawOval).
  5. Define a class that extends java.awt.Canvas and looks like a (black and white) Japanese flag, i.e., it has a circle at (100,100).
  6. (Optional Bonus) Make the circle red.
  7. (Optional Extra Bonus) Make the circle change color when the mouse is over your Canvas. (Hint: mouse enter, mouse leave.)

These are the only things that you need to program this week! The rest is just design and preparation for the lab!

B. Lab Preparation

This week's finger exercises are designed to help you with the lab assignment, and you have to do them before you begin your lab. You need to write them up explicitly for check-in. (You do not need to do the bonus exercises (at all), though.)

If you want to take a look at what the Scribble will look like and what your basic functionality should be like, take a look at a working demo at the www.cs101.org site.You download scribble.jar and type java -jar scribble.jar in a shell.

Your job in lab will be to implement (and possibly extend) that behaviour.

We have provided you with several support classes:

You should familiarize yourself with all three of these classes. Be especially certain that you understand the division of labor among instances of these classes!

Note that these links point to documentation for the classes, not to their code. You should not need to look at the code itself. However, you should be familiar with the interfaces provided by each of these three classes and how they interrelate. For example:

[CHECK-IN]

Building Runnable Code

Your first task in lab will be to define a standalone Java program. In order to do this, you will need to create a class with a public static void main( String[] args ) method. (The main method is the method called by default when the virtual java machine starts running your code.) Figure out how to do this. Think about a simple way to test this functionality.

[CHECK-IN]

Next, you'll need to connect your main method to this assignment by having it create a Whiteboard. Your next pre-lab task is therefore to figure out how to write this class. Bring your design with you to lab. If you want to test it, how would you do it? What do you expect to see at this stage?? Don't program it, just design it!

Handling Events

After the previous steps, you will have designed code that should run and create something that looks like a Scribbler. The definition so far, though, doesn't do much in the way of drawing. The most important problem here is in the SmartCanvas: it doesn't handle any Events. You will need to to fix this. Your next pre-lab task is to figure out what events to handle and how to handle them.

[CHECK-IN]

Design this code and bring it with you to lab. This does not mean that you program it at home, but that you plan how you will be doing it! You may wish to have several stages here, too. (Some suggestions are included in the lab section, below.)

In order to run this code, how will you need to change your startup code?

 

Development Plan

[CHECK-IN] [TURN-IN]

The final thing that you should bring to lab is a plan of the stages in which you will write and test your code. Be very wary of a design plan in which you write more than a few lines of code without testing them. You should never build too much without seeing it run. This design plan should include an explicit (written) list of steps in which you will build and test your code. You may want to read through the additional suggestions in the In the Lab section, in Lab 13.Note that a plan is NOT the code! It is just a plan of what you want to try in the lab. You will write the code next week.

Laboratory

What to Bring to Lab

When you arrive at lab, you should have answers to the questions in the lab preparation, your finger exercises, a copy of your code design and your development plan ready so that I can check you into lab.

 


This course is an adaption for the Fachhochschule für Technik und Wirtschaft , Berlin by Prof. Dr. Debora Weber-Wulff of a part of Prof. Dr. Lynn Andrea Stein's Rethinking CS101 project produced while she was at the MIT AI Lab belonging to the Department of Electrical Engineering and Computer Science (EECS) at the Massachusetts Institute of Technology. She is now with Franklin W. Olin College of Engineering. The copyright for all materials belongs to Lynn Andrea Stein, this adaptation is used by permission. All rights reserved.
A textbook is in preparation by Morgan Kaufmann Publishers


Questions or comments: <weberwu@fhtw-berlin.de>