|
RHS |
Exercise 01
This assignment is a little "warm up" to introduce you to the area of game programming.
As usual you should create a new folder for an assignment to hold all the files. As we'll change some of the files from question to question I recommend that you create a sub-folder for each question!
Question 1
Create a simple form (JFrame) with a button and a label - initially the label shows "0 clicks". Each time the button is clicked the number shown in the label should be incremented. So after first click it should show "1 clicks.
Question 2
Create a very simple class, Value, which only holds an int value as data. the following methods should also be present:
Now change your form from question 1 so it instantiates an object of type Value. Now each time the button is clicked the setValue() method for the Value-object should be activaded and the label should show the value from the object.
Question 3
Now change the Value class so it extends the class Observable and change the form so it implements the Observer interface.
Adapt the Value class so it will notify any observer each time the value changes.
Also change so a click on the button only calls the setValue() method - the change of the label should be done in the update(Observable, Object) method.
Question 4
Extend the form so it has two buttons and instantiates two Value-objects - but still only one label.
A click on the first button should change the value of the first object.
The second button changes the second object.
Each time one of the objects changes the label should show that objects value.
Question 5
Create a Ball class that extends JPanel, holds a x- and a y-coordinate
(int) and a Color.
x and y should be initialised to 0 (zero) and the Color to Color.blue;
The method setPosition(int x, int y) should set new coordinates and you'll
need getters for both x and y.
Finally you need to override the paintComponent(Graphics) method so it erases the background (this is done by a call to super.paintComponent()) and draws a circle at the coordinate (x, y) (use fillOval() in the Graphics class).
Now create a JFrame form (with a main-method to initialise and show the frame) that holds and shows an instance of the Ball class.