Graphics2D Applets

You can cast the graphics object (usually named g) to a Graphics2D object (named g2 in our applets) for much greater functionality. We have used Graphics2D already in our conversion of Ihsan Fazal's MultiDraw to Oxygene for Java. According to the Graphics2D reference, "This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout." All of the methods that worked with g should also work with g2. In this section we demonstrate how to draw slanting text and quadratic curves and how to use affine transformations to rotate, scale, shear, and translate (move) shapes. Also available via Graphics2D (but not covered here) are cubic curves, shapes filled with textures and image manipulation including clipping.

Affine transformations enable us to use matrices to modify coordinates of shapes so that parallel lines in these shapes remain parallel after each operation. Budding mathematicians amongst you will appreciate the possibilities offered by the AffineTransform class and should refer to this page. The full matrix (3 rows by 3 columns) can represent a combination of rotation, scale, shear and translation, but the class has convenience methods getRotateInstance, getScaleInstance, getShearInstance and getTranslateInstance that are easier to use. Also, you can modify an existing transform using the rotate, scale, shear and translate methods.

In order to draw an outline or a filled shape in Graphics2D, you pass the shape as an argument to the draw or fill method, respectively (instead of having lots of separate methods such as drawOval and fillRect).

In our demonstrations we save the existing transform and reinstate it afterwards. This is unnecessary in these simple programs, but is a good habit to form in preparation for developing real applets using several transforms. We provide the full code for the applets, but leave you to write/adapt your own .oxygene project files necessary for compilation and HTML files to display the applets. (We have supplied many examples of these already e.g. for the applet demonstrating the drawing of lines.)

See the Graphics2D applets with motion graphics here.

Programming - a skill for life!

Mouse and keyboard input, graphics (including Graphics2D), threads and widgets in Oxygene for Java applets