CSC

 

Lab assignment 1 - A small button component

Checkin

The first thing to do is to register for the course. Log in at CSC and write in a terminal window: res checkin prutt10. Each member in a lab group must register from his/her own account.

Now write course join prutt10 This command will improve your environment a little and allow the course leader of prutt10 to give you log-in-messages. res checkin and course join should only be performed once for each course.

Optional-share directory with your lab partner

The following (optional) command will create a common directory prutt10 for you and your lab partner.

course labdir prutt10 loginNameOfLabPartner

The labpartner must then log in and give the command with your login name last. If you don't want a common directory, just create a directory for the course, e.g.: mkdir prutt10. Of course, you may fix a joint directory accessible to both group members using symbolic links and access codes, without using course labdir.

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. Sima is the name of our computer program which manages a queue for this. After the command course join prutt10, if you log out and then log in again, sima is available as the short command sm in a terminal window. Otherwise, first write module add sima and then sm. 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.

Write a Java application and a Java applet!

Connect to the directory prutt10, create a subdirectory lab1 and in lab1 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 must change its state and the change must be visible on the button. At least two visible changes must be present, e.g. colour and text. You may choose to have n different states, in which case n button clicks must 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. It is nice but not required that a user can choose the appearance of the states through parameters to the constructor of the button:

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 on creation of the buttons.

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 subdirectory myPackage and add the proper lines to all java files so that the application and the applet both use the component from the package myPackage (you may of course use any name of you choice for the 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.

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


    Published by: <ann@nada.kth.se>
    Updated 2010-03-17
    Technical support: <webmaster@nada.kth.se>