Showing posts with label Resources. Show all posts
Showing posts with label Resources. Show all posts

Sunday, March 20, 2011

Cleaning Out the Sun References

Today I'm going to start cleaning out the references in old posts to Sun Microsystems. Sun is gone now, except for a page that tells you it's gone and points toward Oracle. Oracle bought Sun, and now owns Java.

Many of my old articles point toward the previous Sun website. I'll be changing those to point to current locations for the same resources, mostly to be found on java.net.

If you come across something I appear to have missed, feel free to email me with the article title so that I can fix it. Thanks!

One More Thing

It's worth noting that since some of these articles were written, the official online resources have improved. I still think there are some major gotchas for new learners in Java, but things are definitely better than when I started this blog.
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

Tuesday, July 1, 2008

Mini-Review: Beginning Programming in Java for the Absolute Beginner, 2nd edition, by John P. Flynt, Ph.D.




For non-programmers looking to make the transition to becoming a programmer, this book is a fine place to start. It works well as both a self-study text, and as a classroom textbook.

There aren't a lot of Java books out there written to teach the language to non-programmers. In many ways the Java language was created for C programmers, and most of the available books are written for the same audience--programmers familiar with C or C++. To my knowledge, there are presently only two books that are generally available intended to teach Java to the non-programmer. Edit: since this was written, several more books of this type have come out. The situation is far better now!

The First Step is Always the Hardest

As appropriate for a beginner's book, this book starts with a fair amount of detail on how to get set up and get started. It's challenging to strike the right balance with this material, some beginners get up and running with a minimum of effort, others run into trouble from unanticipated sources--leaving them in a position where almost no amount of information seems like enough. I think this book strikes a good balance, referring to external sources for supplemental material. Best of all, it maintains a platform-agnostic approach. You would think this is a given in a book teaching a platform-agnostic language, but it's not.

As appropriate for a beginner's book, the book teaches the principles of programming first, and Java second. The exercises of the seven chapters take the form of console, or command-line applications. Graphical applications are introduced about 2/3rds of the way through the book, in chapter 8. Most of the examples take the form of simple games.

The book teaches the basics of programming--variables, operators, flow control and such--in the first four chapters. The pacing of this material is very good, it teaches enough to not only cover the absolute core but some of the extra twists, and moves through it before it becomes tedious. Chapters five through seven get into object-oriented programming. The placement of this material in the book is great, going any further without introducing the material would be a bad idea, and any sooner would have been confusing to non-programmers.

Comprehensive Coverage

Overall the book covers a lot of ground. Someone working through this book from beginning to end is going to come out with a strong knowledge of a lot of Java. Each chapter ends with a number of exercises, which are essential additions to the examples in the text for the student to have the ability to really synthesize the information and go from program copier to a writing their own programs.

The breadth of the coverage is one of the books' strongest points. The material is well-paced, and the many complete examples of working programs are a huge advantage over the snippets that many books rely on. Exception handling, class creation, and many other subjects are covered. About the only thing I could ask for that isn't here is a brief introduction to threads, but I wouldn't even consider the idea of adding this if the book wasn't already so comprehensive.

Errata

One of the problems this book has is the typos in the program text. They are not numerous, and often even beginners can see the flaws once they know to look for them. They aren't common, only a few of the programs have errors in them. But they can be frustrating when they occur for those who are learning on their own.

Reaching Too Far?

Some of the examples try to do a bit too much, too. Rather than concentrating on one key point they add several new items at once. The centerpiece of each program is always the subject of the lesson, but additional little items get slipped into the examples. It's also worth noting that the examples that use special characters don't account for the configuration of the Terminal in Mac OS X that interferes with using these characters in the same fashion as on other platforms.

The later examples in the book also tend to get quite long. This results in a lot of typing by the beginner before they see any results. Likewise, this ties into the problem with many exercises not concentrating on the specific subject of the lesson. Particularly when graphics are introduced, I would have liked to see very short, focused examples that do only the absolute minimum to open a window and do some drawing, then add other interaction (such as the mouse, keyboard shortcuts, widgets, etc.) piecemeal. I ended up using my own example programs in my class to introduce graphics, and it went considerably smoother than I have had it go with students that followed the book alone. (I will be posting these examples in this blog when I get into graphics.) I would also like to have seen graphics introduced earlier in the book, allowing more examples to use graphics. This would also start the student making what look like "real applications" in the modern environment earlier in the learning process.

Summary

I feel this is a great book for a new programmer. Further, Java is a great language for a new programmer. Groovy may be better, but the resources available for a beginning programmer are still slim at this point, and I haven't actually had the experience of teaching it to beginners yet. Edit: There are many other languages that run on the JVM now, the others that can access the graphics and sound facilities natively (rather than through special calls to access Java syntax) would also be good candidates.

Final Grade: 85%, B

Pros:
Good material and pacing, (mostly) fun examples.
Covers lots of Java, and does it well.
Platform-agnostic.

Cons:
Typos in examples.
Discussion gets dry in places.
Examples in the later parts of the book are long and do too much at once.

Recommendation:
Buy if you're a non-programmer looking to become a programmer and want to do it with just one book. If you're willing to work out of more than one book, you may also want to consider Beginning Programming with Java for Dummies by Brad Burd (I'll be posting a mini-review of this book soon.) This also makes a good textbook for teachers looking for a class textbook thanks to the organization of the book and the chapter exercises.

StumbleUpon

Monday, June 30, 2008

Primitive Variables: Sticks and Stones

In Java there are two types of variables. The simpler type are called "primitive" variables. This makes it sound like you'd only want to use them in programs for, say, starting fires or skinning cave bears. They're actually used in almost every program, and are very powerful.

There are eight types of primitive variable, each one holds a different kind of information. The types are:

boolean--holds a true or false value.
byte--holds an integer value from -128 to 127.
short--holds an integer value from -32768 to 32767.
int--holds an integer value from about -2 billion to 2 billion.
long--holds integer values too big for int.
float--holds a normal-precsion floating point number.
double--holds a high-precision floating point number.
char--holds one character (one letter or digit or punctuation symbol.

Two Steps to Making A Variable



There are two parts to creating any type of variable; declaration and initialization. In declaration you're giving the variable a name, and telling Java what type of variable it is:

int bugs;

In this statement, we've created a variable named "bugs" and told Java that it's an int (normal integer) variable. The name can be whatever you want it to be so long as it's not one of Java's keywords. You can look up Java's keywords in the Java Language Manual available online at Oracle's Java reference site.

A variable is initialized by giving it some value. In the case of a primitive variable, it's simple:

bugs=0;

You can get away without initializing primitive variables in your program, but it's a bad practice. By putting a specific value in the variable in your program, you know what's in your variable before you start using it.

The declaration and initialization can be combined into one statement, and most programmers do this most of the time:

int spiders=0;

You can also declare multiple variables in one statement by putting the declarations (and initializations) in a comma-separated list:

int frogs=0, salamanders=100, newts=3;

You can add linebreaks and whitespace to make longer lists more readable:

char
cow='c',
horse='h',
pig='p',
duck='d',
goose='g',
snake='s';

Many methods can work with primitive types as arguments. For example, println() can have any of the primitive types in it, and it will print them out appropriately:

System.out.println(cow);

Prints:

c

System.out.println(salamanders);

Prints:

100

You can define new colors for graphics in the color class using int variables:

Color amphibianColor = new Color(frog, salamander, newt);

or float variables:

float red=0.95, green=0.98, blue=0.0;
Color lightAmber = new Color(red, green, blue);


(These Color statements are a preview of the declaration and initialization of the other type of variable, reference variables.)
StumbleUpon

Mini Review Posted: Head First Java

I've posted a mini-review of the fine book Head First Java, 2nd ed. by Kathy Sierra and Bert Bates over on my "Cats On Keyboards" blog. I don't recommend it as a first book for the beginner, but it makes a fine supplemental book.

The best part of this book is some of the explanations it gives for how Java goes about doing things. The explanations give your brain something to hang on to that makes dealing with Java's approach to object-oriented programming that much easier.

StumbleUpon