Java and Math: Coding for Graphics

Hits: 75


In my computer class, a rotation of a graphic of Sonic the Hedgehog was attempted. The graphic was in a jpg format, and the method chosen was to write code in Java which read the graphic into a BufferedImage object. The idea is to copy the graphic into another BufferedImage object, reversing the order of pixel reading to obtain what amounts to a rotation in the new object. The image object is then written to a new JPG file. After the run, the new file is inspected.

This article is a bare-bones know-nothing introduction to how to think of reading and writing graphics that avoid the implementation details as much as possible. Documentation on the details of BufferedImage are plentiful on the internet, as well as information on computer graphics generally.

In a learning activity, students in grades 10 and 11 were asked to modify the statements:

int w2 = x;
int h2 = y;

such that (w2, h2) becomes the new placement for the pixel (x, y) when the graphic is rotated. Students were instructed not to modify any other part of the program, which performed object declarations, file opening and closing, try/catch statements and other concepts considered too advanced for this stage of their course. Students were encouraged to use trial-and error and view the consequences of various general formulas they were to try out. width and height were already declared and given values in the code.

The tutorial below was given to the students.

The graphic in question
In general terms, for any graphic of dimensions a\times b, x can have any value in the domain of 0 to a-1, while y can have values in the range of 0 to b-1.

Graphics are made of little units of colour information which end up on your computer screen as pixels. Think of each unit of information as belonging to a part of that graphic on a Cartesian coordinate plane. Much like the coordinate plane you learn about in grade 9 and 10, x and y represent the position of the pixel horizontally and vertically.

If the graphic is 100 units across by 200 units in the vertical direction, then the graphic is said to have dimensions 100\times 200, in terms of pixels. This means all pixels in a graphic will have some location (x, y). Because x has to start from 0, it can take on values between 0 to 99 for this graphic, while y can take on values between 0 and 199.

In our code, that means that the dimensions of the graphic is heightxwidth, and while x and y are fine (for the original graphic), a formula must be applied so that h2 and w2 (the x and y values for the rotated graphic) become appropriate for a rotation. With the statements written as they are, all you will get are two copies of the same graphic.

It is expected that the formula should be simple, but beware of values going beyond the range of the graphic dimensions. That will result in a crash. The dimensions of the graphic are given as heightxwidth, and it shouldn’t matter all that much what their real values are, you might want to insert printf statements where you can trace h2 and w2 against the values of height and width to see what is going on.

It is also noteworthy that you can only carry the Cartesian plane analogy so far. (0, 0) represents the pixel in the top left corner of the graphic, while for a graphic of dimensions hxw, the coordinate (w-1, h-1) will represent the pixel on the bottom right. The first difference is that no negative values are possible. The second difference is that the y-axis is upside-down, because it increases downward. The x-axis is still fine, however, increasing from left to right as usual.

What are your thoughts?