Exercise 2: An Image Display

Now that you know everything about Java, you should be able to transform my ideas into classes and implementation of these classes (two different steps).

For that second exercise, I will still give you some directions by suggesting the organisation and interface of the classes, but you do not need to strictly follow that organisation as long as you get to a good result.

Main Idea

I would like to get a program that will open a window and show a jpeg, PNG or GIF picture in the middle of the frame.

The area of the frame should be four times the area of the picture so long as it remains equal to or less than half the area of your screen display. The dimension of the frame should be kept homothetic(*) to that of the picture.

If the picture is to big regarding these restrictions, it should be uniformly scaled down in order to fit the restrictions.

What restriction of that sort should be added here to be sure that nothing bad could happen running the program ?

Of course, the image will be initially drawn in the middle of the frame.

More about the interface

You could have a class, ShowImage, with three constructors :

Lab2 Screenshot

Tips - useful classes and methods

some more tips online...

Next Step exercise 3

Eventually, the aim of the program will be to be able to drag the image around the JPanel via the mouse.


As well as showing...

There should be a text output (to the terminal) of the screen, frame and image dimensions, area and Width/Height ratios.

Exercise 2 second part

Write a little program that will get an integer as an argument and that will use recursion to compute factorial value of that integer. Try to find a graphical way to explain recursion

Example, display :

Input  : 4
Output :
4! = 4 x 3!
= 4 x 3 x 2!
= 4 x 3 x 2 x 1! (recursion stops here because detection of the trivial case of 1!)
= 4 x 3 x 2
= 4 x 6
= 24

(*) Uniform Scaling (strectching or shrinking) possible from one to the other : go from one the other trough a homothetic transformation. A homothetic map preserves within a global scaling factor relative distances and angles.