Archive for October, 2005

Oct 31 2005

Making your custom objects raise Change Events

Published by Tony under Old Restored

If you create an object that inherits directly from Object, you’ll notice a distinct lack of any methods to handle events. That’s because event dispatching was meant mainly for AWT and Swing components. But you can use a similar method of detecting state change in your non-GUI objects. The first thing you’ll need in your object is a list of listeners – something like this:

Continue Reading »

No responses yet

Oct 25 2005

Turning your Graphics/Graphics2D Drawings into an ImageIcon

Published by Tony under Old Restored

I was recently tasked with an assignment that required me to draw several randomly generated colored circles and a target to a playing field. The user can choose the circle that he/she thinks will hit the target first and then bet on it. To make the choice as simple as possible I wanted a way to add my colored circles to a JComboBox. However the default ListCellRenderer of JComoBox only renders text or Image objects, so I needed a way to convert each of my circles into an Image. After some searching I found that java.awt.image.BufferedImage had a method called getGraphics() that returns a Graphics object. Actually, it returns a Graphics2D object cast as a Graphics, so you have to cast to get a Graphics2D. You can use this Graphics object to draw directly to this BufferedImage and then Create an ImageIcon from there.
Continue Reading »

One response so far

Oct 18 2005

Java Generics and erasure

Published by Tony under Old Restored

When I initially began reading about and using generics in Java, I heard the term “erasure”, but forgot it almost as soon as I’d read it. This is mostly because it didn’t really seem to affect my usage of generics at the time. Erasure is the process by which the compiler changes your parameterized generic code into plain old Objects. That’s right – generics are little more than syntactic sugar that give you a little added compile-time type checking. But behind the scenes it’s all being undone. So what effect does this have? Well, for one thing reflection will not work. You can’t check the specific type of something if it’s cast back into a non-parameterized type during compilation. So your ArrayList<Integer> will basically become an ArrayList<object>, which means that dynamic runtime checks and downcasts are still necessary. Sun did this mainly for backwards compatibility with previous versions of Java, which is understandable for the time being. Incidentally, version 2.0 of C# introduces generics for the first time and they chose to give them run-time representation from the beginning. Hopefully Sun will follow suit.

No responses yet

Oct 04 2005

Quick RMI tips

Published by Tony under Old Restored

Our class has just started learning about RMI. Many of the tutorials I’ve looked at are a bit out of date, however. For instance, many of them still mention generating stubs and/or skeletons using the rmic command, but this is no longer necessary as of jdk 1.5. All you have to do now is compile your classes and stub files are created dynamically at runtime (skeletons are obsolete in 1.5). Of course, if you are working with a pre 1.5 jdk, you’ll have to do things the old way. Something else I noticed was that most of the tutorials had me run rmiregistry from the directory where my remote classes resided. I had to do this before running my application. This seemed a bit clunky to me, so I investigated (read Googled) for a better solution. I found it. Rather than running this process from a command console, use java.rmi.registry.LocateRegistry.
Continue Reading »

No responses yet

Oct 01 2005

Java Technology Overload

Published by Tony under Old Restored

The more I look into the different third-party Java technologies/frameworks, the more overwhelmed I feel. The list just gets bigger and bigger. And just as I start to look into something I hear that something newer and better is coming to replace it. Here are the names of a few of the Java technologies I’ve heard a lot recently (in no particular order):
Continue Reading »

No responses yet