Tuesday, July 29, 2008

Rotate an image in Java

In Python, many complex image functions are made simple using the Python Imaging Library (PIL). For example, I can load an image from file, rotate it any number of degrees, and display the image in just four lines of Python code.

from PIL import Image
pic = Image.open("wire.png")

Performing the same task in Java, however, requires the involvement of several classes and a slightly deeper understanding of graphics processing. I feel that Sun's decision to break everything up into hundreds of classes offers great flexibility for me to combine classes to come up with my own solution to a given problem. This approach may be verbose, but it does lead to a better understanding of the underlying algorithms involved.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.net.URL;

public class RotateImage extends Applet {

private Image image;

AffineTransform identity = new AffineTransform();

private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
catch(Exception e){}
return url;

public void init() {
image = getImage(getCodeBase(), "image.jpg");

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
AffineTransform trans = new AffineTransform();
trans.rotate( Math.toRadians(45) );
g2d.drawImage(image, trans, this);

This looks a lot worse than it is. There are 22 lines of code here (not counting white space and curly braces), but the majority of them are spent on importing Java libraries and loading the image. Only six of them are spent rotating and displaying the image.

The most complicated part of the code is the AffineTransform object. According to Sun's AffineTransform API, "The AffineTransform class represents a 2D affine transform that performs a linear mapping from 2D coordinates to other 2D coordinates that preserves the "straightness" and "parallelness" of lines." If you experiment a little with this class (or just continue reading the API), you'll see that it can be used not just to rotate, but also to scale, flip, and shear an image as well.

Monday, July 21, 2008

Hello, World!

This is a programming blog written by Bill Cruise, a professional computer programmer. I've started this blog to publish a number of essays I've written covering a range of topics in Computer Science, Programming, Software Engineering, Design, and Mathematics. I've always felt that I don't really understand a topic unless I can explain it to someone else on a whiteboard. This blog is my whiteboard. I write the essays in an attempt to organize my thoughts and better understand a given topic. Publishing them is an invitation to others to share their thoughts and join in a discussion.

Hope you enjoy it!

Why "Hello, World!"?

A "Hello, World!" program is a traditional first program written by software developers when learning a new language. It's purpose isn't so much to learn anything about the syntax of the language, but more to test that the programming environment is set up correctly. It's simple enough that syntax mistakes can be easily avoided, thus making it a good choice for a first program.

The tradition of using some variation of the phrase "Hello, World!" seems to have its origin in an example program first published in The C Programming Language, written by Kernighan and Ritchie (K&R).
main() {
printf("hello, world");

Further Reading
List of "Hello, World!" Programs