Friday, February 27, 2009

Mini-Review: Learning Java by Patrick Niemeyer and Jonathan Knudsen

Once upon a time there was a neat little book called Java in a Nutshell. It described the Java language, feature by feature, and did a great job of teaching the language. It was targeted at people who were already programmers in some other language, but at the time there weren't a lot of people who'd consider using a new and somewhat narrowly-defined language like Java as a first programming language.

The first edition was pretty compact, true to the "Nutshell" name. Then Java grew, and the book grew with it. Now the current edition is huge, and that's even after they've moved out a lot of the examples and more detailed explanations to make room for everything else.

For those looking to learn Java, O' Reilly, the publishers of Java in a Nutshell, have a different book. It's appropriately named Learning Java. Like the original Java in a Nutshell, it's targeted at people who already know how to program. The title and cover design of this book don't suggest that, however, my feeling is they suggest that this is a beginner's book. Which it isn't, and the text of the book itself never says that it is.

The book is a good overview of Java for programmers learning Java. Beyond assuming that the reader is a programmer, it also assumes familiarity with object oriented language concepts. It briefly sketches them out, but if you're not already familiar with C++, Smalltalk, Objective-C, or some other object oriented language this book is not the place to start.

Learning Java is amazingly comprehensive. Java is a huge language with a lot of features. In some places the book has to cover things very briefly, in others it takes time to delve in a bit deeper. Those places where things are covered briefly often give short shrift to the subjects covered. Several related features will all get covered together, and instead of each one getting its own code example, several features will be demonstrated in a single piece of code that mashes them all together in one omnibus program. This makes understanding each individual subject much harder. There are also a lot of code snippets through the book in places where a short, complete program would provide a much better illustration than a few isolated lines of code.

In compensation for this, there are some code samples in the book that are excellent demonstrations of Java's power as a programming language. For example there are complete programs for a basic web server and a basic web browser in the book. These are both good demonstrations of Java features and a good jumping off point for the reader to start their own program. Threads are likewise covered with useful examples.

These examples make the book worth owning. It makes a great second or third book for someone learning Java. I wouldn't recommend it as the primary book for learning Java, however, only as an adjunct to some other book. My inclination would be to use this as a "third book." The first book should be a good teaching book for the level of the learner. The second book should be a strong, focused reference book for the areas of the language that are most important to that person's learning level. The third book should be a backfill with a broader range of examples and be a source of explanations with a different perspective from the first book to help out when the first book falls short of being clear for the learner.

This book is one of my favorites of those I own for that "third book" role. I draw examples from it for my classes, and look to it for supplementary explanations of features of Java. I've used it with students having trouble with concepts in other texts, having them work through the related portions of this book. It's been a great help that way.

Final Grade: 75%, C

Pros:
Some wonderful example programs.
Very comprehensive.
No fluff.

Cons:
Crowds too much into too few stand-alone example programs.
Does a poor job of teaching object oriented concepts.
Many sections are rushed.

Recommendation:
Get it to fill out your Java library, the excellent examples and good descri1ptions of areas of Java often not covered well elsewhere makes it worth having on the shelf. Don't buy it as a primary learning text, however, no matter what your level as a programmer.
StumbleUpon

Friday, February 20, 2009

Rotating an Image with Java

In an earlier lesson we learned to load an image file into Java and display it. We also scaled the image in the program given in that lesson. Now we're going to get just a tiny bit more sophisticated, and rotate the image, as promised in the lesson introducing Java2D.

The basic graphics functions of Java allow for positioning an image in a window and scaling that image. But that's about all. If you want to rotate the image, skew it (make it look like it's leaning over) or do other things to the image you display you need to use Java2D.

What we'll do in this program is load the image as before. But before the image is drawn we will create a Java2D version of our graphics object that we're drawing to. Then we'll rotate that graphics object and draw the image. The rotation results in everything that gets drawn to the graphics area being rotated by the amount specified.

The program uses the same image of Duke as the previous one. It needs to be in the same directory as your program.

Here's the program. More discussion of the program follows the listing.
// Import the basic graphics classes.
import java.awt.*;
import javax.swing.*;
/**
* Simple program that loads, rotates and displays an image.
* Uses the file Duke_Blocks.gif, which should be in
* the same directory.
*
* @author MAG
* @version 20Feb2009
*/

public class RotateImage extends JPanel{

// Declare an Image object for us to use.
Image image;

// Create a constructor method
public RotateImage(){
super();
// Load an image to play with.
image = Toolkit.getDefaultToolkit().getImage("Duke_Blocks.gif");
}

public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g; // Create a Java2D version of g.
g2d.translate(170, 0); // Translate the center of our coordinates.
g2d.rotate(1); // Rotate the image by 1 radian.
g2d.drawImage(image, 0, 0, 200, 200, this);
}

public static void main(String arg[]){
JFrame frame = new JFrame("RotateImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);

RotateImage panel = new RotateImage();
frame.setContentPane(panel);
frame.setVisible(true);
}
}

Most of this program is the same as our previous one. There are a couple of significant changes, however.

One is the following line:
Graphics2D g2d=(Graphics2D)g;
In this line we're doing what's known as a "cast". We're declaring a new Graphics2D object, naming it g2d, then setting its value to our present Graphics object, g. But since g is a Graphics object, and not a Graphics2D object, we're telling it to treat g as a Graphics2D object for the purposes of this assignment. We can get away with doing this because Graphics2D is a subclass of Graphics, which makes the two classes compatible enough to allow the cast to be successful.

By doing this, we "magically" make all the Graphics2D functions available for drawing to our window.

What we want to do is rotate our image. Since we don't want to have the image go outside our graphics area when we rotate it, I've moved our "point of origin" of our graphics area using
g2d.translate(170, 0);
This moves the center point around which the drawing area is rotated from the upper left corner of the window's display area to a point 170 pixels to the right of that.

Then we use the rotate() method of our Graphics2D object, which will cause everything drawn into it to be rotated:
g2d.rotate(1);
The amount we're rotating is 1 radian, or about 57 degrees, noted by the 1 inside the parentheses.

Then we use the drawImage method of our Graphics2D object to actually draw the image into our display area.

Experiment with different values for the rotate() method to see rotation at different angles. If you lose part of the image off the edge of the window, try to use different offsets in the drawImage() method to change the location within the window at which the image gets drawn.

This is a fairly simple method for rotating an image in Java. It has some value for creating static images that are rotated, but for something like sprites in a game it's not very useful. The best method for that would be to create rotated images in a graphics program, not in your Java program. It's possible to rotate your images in Java, but it gets complicated because you have to worry about a lot of things that your graphics program does for you, like the area your new image will fill relative to the original, the background of the image, and so on.

Once you have image files of your sprite in all its possible rotated angles, load all these images to different Image object in a Java program (or better yet, an Image array or Collection), and change between them to show the image rotating.
StumbleUpon

Saturday, February 14, 2009

Better 2D Graphics: Java2D

In our last program we loaded and displayed an image using Java. Not only that, but we scaled it to be a specific size. We did this using the Image class from the java.awt package.

Unfortunately, this pretty well covers the abilities of the Image class. But there's so much more that we'd like to do with images in our programs, especially if we're writing games. Fortunately, there's more that Java can do with images thanks to Java2D.

If you go to the Java API you won't see a specific package called something like java.java2d or javax.swing.java2d. "Java2D" is a whole bunch of packages and classes spread through Java's libraries of stuff. The java.awt.image package is part of Java2D, as is the java.awt.Graphics2D class inside the java.awt package. For a list of all the scattered components of Java that are part of Java2D, have a look at the Java2D API Specification.

Like so much of Java, the presentation of Java2D is confusing. It's made to sound like a single thing when it's a bunch of interrelated things worked into Java. Fortunately, like many such problems in understanding Java, it's only a problem of how the language is presented, not a problem with the language itself. Once you get to working with the elements of Java2D, you find that they're fairly naturally positioned in the places you would expect them in Java by their function. Which is how they got spread out in the first place (that and some history--it would have been even more natural to have there be one Graphics class that does everything, rather than a Graphics and Graphics2D class.)

Also, it's very easy to take items that are non-Java2D classes and use them with Java2D. Usually it's nothing worse than "casting" them as Java2D objects. That means you basically tell Java "pretend this object is the right sort of thing" in a way that works.

So have a look at some info on Java2D, and if it looks confusing, remember it's not you that's the problem. Sun does an amazing job of presenting great things in awful ways by trying to say way too much to too many different audiences at once. And they like to start in the middle of the story.

The Java2D tutorials do a decent job of showing things off without a lot of mind-numbing prose (well, not too much, anyway.)

Next I'll post a short, simple program we can use that rotates our images.
StumbleUpon