bild
Skolan för
elektroteknik
och datavetenskap

Lab assignment 1

Before you start

Sima for help and lab demonstration during the lab sessions

If you have not used Sima before, ask one of the teachers about it.

Subdirectory for this course

Should be obvious for all: Create a subdirectory for the course and under this a separate subdirectory for each lab assignment.

Assignment 1 - A small button component

The task is small but for a Java beginner it may be hard to grasp all the novelties. The lessons during the first weeks of the course cover what you need.

Goal

To achieve basic understanding for Javas graphical components, graphical applications in Java, events, listeners, applets and packages.

Lab contents

  • Create and run small graphical applications.
  • Use graphical libraries.
  • Create your own graphical component by extending a component from the graphical library.
  • Make a program react to "button pressing", make something happen when a button is pressed.
  • Create and run a small applet.
  • Basics about creating a Java package.
The instructions are detailed, suited for Java beginners. If you find the task simple, it is not necessary to do alla the subtasks. You need only demonstrate from task 4B. However, you must be prepared to answer questions about all subtasks.

1 – A Java application

Write an applications that opens a framed window on you computers screen by writing a class that inrerits from JFrame. All initializations of the windows must be made in the constructor of the new class. If you put the following method call in the constructor, the program will stop when you close the window:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Without this method call, how do you stop the program?

2 – An extended button component with two states.

Write a class MyButton for a new graphical component which is an extended button, i.e.a class that inherits from JButton. The extended button must have two different states. Each state is characterized by a text and a colour. Therefore you need two texts and two colours for each button. The buttons current state must be visible on the button. This is a proper head of the constructor for the button:

MyButton(Color c1, Color c2, String s1, String s2)

Also, define a method toggleState() in MyButton. The method must change the state, "toggle" usually means alternating between two states. Creating two MyButton – objects may look like this:

new MyButton(Color.white, Color.cyan, "On", "Off")
new MyButton(Color.green, Color.red, "Run", "Stop")

3 – Buttons in a window

Extend the task1–program so that a button is added to the window. Use method add(...) from JFrame. Run the program and press the button! Unfortunately, nothing happens!

4 – Press the button

4A: To make something happen when you press a button, there must be a listener object, "listening" to the button. The class of the listener objects must implement the interface ActionListener which implies that the class has the method

public void actionPerformed(ActionEvent e) { ... }.

Let the "frame class" (the class that inherits from JFrame) implement ActionListener and let its actionPerformed() call toggleState(). Remember to connect the listener object to the button!. Make sure it works!

4B: Remove the listener property from the JFrame-class (or make a copy and retain both versions of the program). Let the button component listen to itself, i.e. let MyButton implement ActionListener and define the method public void actionPerformed(ActionEvent e) which calls toggleState(). Create at least two buttons and add them to the window. Run the program! Make sure the buttons change their states independently!

5 – An applet

Write an Applet-program with the same functionality as 4B, i.e. shows two buttons that change their states independently. The same class MyButton must be used here. To run an applet from a terminal, you need a html file (running applets from an IDE is different). Minimal html for an applet:

<applet code = Labb1.class width = 300 height = 150></applet>

Labb1.class is the compiled version of Labb1.java The program will run in a window, 300 pixels wide and 150 pixels high. Appets can run on a web page or with appletviewer which is started from a terminal window:

appletviewer Labb1.html

6 – A package

A package is a subdirectory. Make a subdirectory packe-name (choose your own name!) an put your class MyButton there. Write the appropriate package and import lines at the top of the files so that the both the applet and application run again. How do you make a package? Read here (so far only in swedish)!

7 – Draw a UML class diagram

including the button component, applet and application. All classes (new and from the libraries) must be there but class names and interface names only are sufficient, variables and methods may be left out.

To demonstrate and explain

  • Button component according to 4B in its own package.
  • At least two objects of MyButton in a window.
  • At least two objects of MyButton in an applet.
  • UML class diagram with names of classes and interfaces only.
Demonstrate how pressing the buttons change their states independently.

Ask the teacher to sign your signature sheet when he/she is satisfied with your work.

Extra assignment

Goal: To reach a better understanding of events and listeners, to understand how pressing a button can effect the rest of the graphical interface and how to give input to a program on the command line in a terminal window.

What to do

Write a program with a subclass to JFrame containing objects of your own button componennt. Give the number of buttons on the command line when you start the program, e.g. for six buttons:

java labb1X 6

Buttons must have different unique text but may have the same colours. The program must be able to handle an arbitrary number of buttons but in your demonstration it is sufficient with three or four. Give all data on the command line, e.g. like this:

java labb1X 3 Mama Mia On Off Run Stop

The line above should create three buttons with texts pairs as follow.

All data from the command line will be available to the program as String values in the String[] argument of method main().

The method Integer.parseInt() in class Integer converts a String value to an int value. Read the documentation in the Java API!

Functionality of the program

When pressing one button, all other buttons must change their states. The state of the pressed button remains the same. In tasks 4A and 4B the listening is done by different objects. Which kind of listener construction is most suitable here? Remember that the program must be able to handle any number of buttons. Use an array, i.e. a structure MyButton[], or use a Collection.

To demonstrate for the extra assignment

  • Program as specified above.
  • UML-diagram with class and interface names only
Copyright © Sidansvarig: Ann Bengtsson <ann@nada.kth.se>
Uppdaterad 2014-03-30