bild
Skolan för
elektroteknik
och datavetenskap

Checkin

The first thing to do is to register for the course. Choose Rapp in the course menu and register. It is required that you log in to the KTH system first. Also, in a terminal window, write course join prutt11.

The Sima queueing system

During a lab session, students will need help from lab assistants and/or wish to demonstrate their lab work for approval. SimaManager is the name of our computer program which handles a queue for this. To find SimaManager, choose Applications followed by CSC. Sima will start in a separate window, displaying a number of course abbreviations. Choose prutt and press the Login-button. When you wish to queue for assistance from a teacher, press the Queue-button.

Directory for the course

If you use the computers at CSC, create a directory for the course and a subdirectory for each lab assignment.

Lab assignment 1 - A small button component

The task at hand is small but for a Java-beginner there are many new things to learn. The lab is about the following:
  • Create and run a simple applet program.
  • Create and run a simple graphical application program.
  • Use awt or swing libraries for graphics.
  • Create your own graphical component based on a library component.
  • Create a basic package.

Write a Java application and a Java applet!

Connect to the directory for the first lab assignment. Create two java source code files, where one must be an application that opens a window (Frame or JFrame) and the other must be an applet to be shown in a web browser or with appletviewer. The only thing required of these first programs is that they display the name(s) of their author(s) in the window.

Remember that to run an applet, you need a html-file, e.g. Happ.html. While testing your applet, it is more convenient to run it with appletviewer instead of using the browser:

appletviewer Happ.html
First time you use appletviewer it takes some time to start it but henceforth it will be faster than the browser. A minimal html-file may contain <applet code = Happ.class width = 300 height = 150></applet> Here we are running the applet Happ.class (which is the compiled version of Happ.java) in a window which is 300 pixels wide and 150 pixels high.

Create an extended button with different states

Make a class for a new grafic component which is an extension of (inherits from) one of the classes Button or JButton. The new component must have at least two different states. One click on the button changes its state and the change must be visible on the button. Second click goes back to the original state (in case of only two different states). Subjects for change are e.g. background color of the button and/or the text on the button. You are welcome to include other data in the visible state of the button. You may choose to have n different states, and n button clicks must always bring you back to the initial state.

Important: Please note that the button component must manage its states by itself! The surrounding program (applet or application) is only a host for the component, without interfering with it. The host may not have any knowledge of the components inside!

Through the constructor of the class, parameters of the internal states are set. This is an example, the constructor of your class need not look exactly like this, fewer paramters are OK.

new MyButton(Color.white, Color.cyan, "On", "Off");
new MyButton(Color.green, Color.red, "Drive", "Stop");
The two states of each object are shown as different colours and different texts, chosen when the buttons are created.

Buttons in windows

Add objects of the new extended button in the application and in the applet. To emphasize the self-contained nature of the objects, put more than one object in a window and observe their different independent states.

A package for the button class

Finally, put the code of the new component in a package. Add the proper lines to all java files so that the application and the applet both use the component from its package

Draw a UML class diagram

for the component, applet and application. All classes used (new and from libraries) must be represented in the diagram but it is sufficient to show only the class names and no internal details (variables and methods).

Lab demonstration

  • Show the button component in its package and explain its functionality.
  • Run the applet with at least two extended buttons. The name(s) of the author(s) must be displayed in the applet window.
  • Run the application with at least two extended buttons. The name(s) of the author(s) must be displayed in the frame window.
  • Show and explain your UML class diagram.

When the teacher approves, ask for his/her signature on your personal signature sheet!
The signature sheet (swedish: kvittensblad) is available from the course home page under Laborationer

Copyright © Sidansvarig: Ann Bengtsson <ann@nada.kth.se>
Uppdaterad 2011-03-22