Java Swing - Graphics
TOPIC 05 – IMAGE FILES PIXEL
BY PIXEL
LESSON WORK
Your teacher will inform you as to how many marks you need to earn.
QUESTION 1 – REMOVE COLOURS (3.5 marks)
Make an application that displays the same image
four times. Here is what each
version needs to be:
- Original
image
- Image
with no red in it.
- Image
with no green in it.
- Image
with no blue in it.
|
QUESTION 2 – GRAYSCALE (4.5 marks)
Making an image grayscale can be done in many
different ways. The method that we looked
at in the lesson is called the average method. It consists of adding the original colour's red, green and blue colour
values together and dividing by three – taking the average of the three.
Gray
Amount = (R + G + B) / 3
Another method to create a grayscale image is
called the lightness method. In
this case, we actually take the average of the highest of the three colours and the lowest of the three colours.
Gray
Amount = ( max(R,G,B) + min(R,G,B) ) / 2
A third method, called the luminosity method,
accounts for the fact that humans are more sensitive to green than to other
colours.
Gray
Amount = 0.21R + 0.72G + 0.07B
Create an application that displays a single
image four times side by side. One
image is the original in full colour and the other
three are in grayscale using each of the different methods described above.
<NO IMAGE AVAILABLE>
|
QUESTION 3 – REDSCALE,
GREENSCALE, BLUESCALE (4 marks)
Recall the method to grayscale an image (using
the average method). Instead of
applying the calculated amount to all colours in
order to get a gray, you will now apply it to just one colour
(and zero to the others).
So, for a redscale, we
use:
New Red = (R + G + B) / 3
New Green = 0
New Blue = 0
The same idea applies for GreenScale and BlueScale.
Create an application that is similar to the
following below:
|
QUESTION 4 – ROTATE
& MIRRORS (5 marks)
Create an
application that will do a horizontal mirror, vertical mirror and 180
degree rotation. You should achieve
all of these by switching the colours of
different pixels with colours of other pixels.
|
QUESTION 5 – INVERT (5
marks)
In this
question, you will invert the colours of an
image. To invert a colour, we simply use the following:
New
Red = 255 – Old Red
New Green = 255 – Old Green
New
Blue = 255 – Old Blue
One can
also add a little more complexity to the invert process. One can specify the number to subtract
from instead of using 255. For
example, invert192 will use the following:
New
Red = 192 – Old Red
New Green = 192 – Old Green
New
Blue = 192 – Old Blue
*Values below zero must be set to zero.
Create an
application that shows an image inverted from 255, 192 and 128 like below.
|
QUESTION 6 – NEAREST
COLOUR (6 marks)
The amount
that two colours differ from each other can be
called the distance between the two.
The
distance between any two colours is calculated by
adding up all of the absolute differences between the two reds, greens and
blues. Note that absolute difference
simply means that we take the absolute value of the differences.
So, the
distance between blue (0, 0, 255) and gray (128, 128, 128) would be
128+128+127= 383.
And the
distance between rgb colours
(125, 16, 181) and (145, 120, 240) is 20+104+59=183.
To create
a black and white image, we consider every pixel and figure out if it's
closer to white or to black.
You will
create an application that will examine each pixel's colour
and replace it by the nearest colour available.
Image 2 colours:
·
Black (0,0,0)
·
White (255,255,255)
Image 3 colours:
·
Black (0,0,0)
·
White (255,255,255)
·
Gray (128,128,128)
Image 4 colours:
·
Black (0,0,0)
·
Red (255,0,0)
·
Green (0,255,0)
·
Blue (0,0,255)
·
Yellow (255,255,0)
·
Purple (255,0,255)
·
Teal (0,255,255)
·
White (255,255,255)
Here is an
example:
|
QUESTION 7 – ROUND
DOWN (5 marks)
One way to
lower the number of colours in the image is to
round individual red green and blue values to the nearest multiple of
10. So, the rgb
colour (132,45,193)
becomes (130,50,190).
You will
write an application that will do this rounding effect at three different
levels. Image 2 will round down to
the nearest multiple of 25. Image 3
will round down to the nearest multiple of 50. Image 4 will round down to the nearest multiple
of 100.
Here is an
example:
|
OTHER IDEAS
Colour mapping
Fading in and out
Random rectangles with
effect applied to that rectangle only
|
|