The Source for Java Technology Collaboration
User: Password:



Chet Haase's Blog

January 2007 Archives


Sunday Math

Posted by chet on January 28, 2007 at 03:13 PM | Permalink | Comments (8)

I just want to point out that these equations:

	f(x) = x >> 1, for all x
	f(y) = !(y >> 1), for all y
are just another example of the halves and halve-nots.

But I should add that I'm divided; this seems rather shifty and I don't want to subtract from the end product.



Filthy Rich Clients: A Subtle Teaser

Posted by chet on January 24, 2007 at 09:21 PM | Permalink | Comments (24)

Romain Guy and I are nearing the end of a book on Swing, Java 2D, animation, and how to write better, and cooler, Swing applications. It's called Filthy Rich Clients.  I know we haven't mentioned it before, except for here, and here, and here, and here, and then here. And there's always here, and here, and then here. We've obviously been pretty quiet about it.

Now the book is actually nearing completion; we have just a tad more writing to go (right, Romain?), some more editing, then some more editing, and reviews, and then editing, and some more reviews, and then we'll probably want to edit it some more. And theoretically, all of this gets done in time to publish for JavaOne 2007 (second week of May).

So I thought I'd take a quick break from editing Chapter 12 (a fun chapter about a really cool utility ... which I have to completely rewrite in the next 2 days) and post the outline, now that we're fairly sure what it'll be:

Part I: Graphics and GUI Fundametals

  1. Desktop Java Graphics APIs: Swing, Java 2D, and AWT
  2. Swing Rendering Fundamentals
  3. Graphics Fundamentals
  4. Images
  5. Advanced Graphics Rendering
  6. Advanced Swing Rendering
  7. Performance

Part II: Animation

  1. Animation Fundamentals
  2. Smooth Moves
  3. Timing Framework: Fundamentals
  4. Timing Framework: Advanced

Part III: Effects

  1. Animated Transitions
  2. Static Effects
  3. Dynamic Effects
  4. Wrap-Up

At least, that's our take on it before the reviewers rip it apart.

It should be a fun, and hopefully quite useful, book.  The first section, Part I, is fairly huge and is intended to be educational to a broad audience about how Swing and Java 2D do what they do and how you can take advantage of various features for richer applications. So while the whole book is oriented toward making-Swing-apps-cooler, there is a lot of material in there that is also intended to simply help Swing programmers understand Swing better. Lots of code. Lots of screenshots. Occasional math equations just to act a little crazy.

Now, back to my dark little editing corner....



Font Hints for Custom Components

Posted by chet on January 10, 2007 at 02:40 PM | Permalink | Comments (12)

I just got finished writing a small section on text-anti-aliasing for Romain's and my Filthy Rich Clients book (did you notice how smoothly I slid that teaser into the discussion?). It seemed like such a bite-sized and useful bit of info that I thought it would go well here, so here it is:

Setting text anti-aliasing hints can be confusing, especially with new RenderingHints added in Java SE 6 for LCD text. What text quality do you want? What is appropriate for the application? What is appropriate for the platform? What does your user prefer?

A better solution than setting hints directly in most situations is for your application to figure out what the user’s native desktop settings are for fonts and to do something similar for their Java application. This, in fact, is what Swing does in some of its look and feels (Metal, Windows, and GTK in particular); it queries desktop properties for how text is rendered by native applications and sets RenderingHints appropriately.

Custom components which perform their own text operations do not get this Swing behavior; the Graphics object you get in paintComponent is set up with defaults that do not know anything about the desktop properties. So if you want your strings to look like Swing’s strings, or to look like native applications' text for that matter, you’ll need to mimic Swing.

I wrote a simple app that shows how to do this, by rendering one string with the default Graphics object in paintComponent (for comparison), setting RenderingHints appropriately to match desktop settings, and then rendering another string with this modified Graphics object. Here is the result on my test system (Windows Vista, with ClearType enabled):

fontscreenshot.png

First, note that the image may look a lot better on my display than it does here for you; LCD text rendering is optimized to look good on the runtime display, not whatever display you may be using to view this screenshot. But the important thing to note here is not how good it looks here, but that the two strings are different because the second string was drawn after setting the appropriate desktop property hints on the Graphics object.

Here is the simple code that produced this output:

g2d.drawString("Unhinted string", 10, 20); 
if (desktopHints == null) { 
    Toolkit tk = Toolkit.getDefaultToolkit(); 
    desktopHints = (Map) (tk.getDesktopProperty("awt.font.desktophints")); 
}
if (desktopHints != null) { 
    g2d.addRenderingHints(desktopHints); 
} 
g2d.drawString("Desktop-hinted string", 10, 40); 
        

Here, we first check to see whether desktopHints is null; this keeps us from re-creating it every time through paintComponent. Desktop properties will probably not change during the lifetime of your Java application, but to be completely correct, we should establish a listener on the desktop properties to get notified if they do change, and to recreate desktopHints at that time; the JavaDocs on this subject tell you how you can do this. To get the desktop properties we do a query on awt.font.desktophints, which returns a Map of all of the properties. Then we simply add all of those hints through a call to Graphics2D.addRenderingHints(). Now our Graphics object is set up to render text just like native applications.

For more information on desktop properties, check out the article “AWT Desktop Properties” in the JavaDocs (the document name is DesktopProperties.html, but it might be easier to find it by clicking on the link in the JavaDoc for Toolkit.getDesktopProperty).

Thanks to Phil Race for helpful pointers on this one



Tagged

Posted by chet on January 08, 2007 at 03:40 PM | Permalink | Comments (5)

I got tagged - does that mean I'm IT? I've been waiting my whole life for this...

I only found out what blog tagging was last Friday, so imagine my surprise when I realized that Romain tagged me a week ago.

Now that I know what tagging is, I'm not sure I'm happy about it. I agree with Chris that it's like a blog version of chain mail, which I've made a point of not propagating my whole life (which, come to think of it, could explain my luck). But, like Chris, I also cave to pressure. And when the pressure is getting me to talk more about Chet, well, I don't have to be pushed off that cliff.

And heck, maybe tagging is a good thing. The web is far too useful with so little junk content out there, right?

First, some clarification. It's a bit tough to come up with five things that people don't generally know about me already.  It's not like I'm shy. So I'll reinterpret this goal and aim my blog at this subject instead:

Five things about me that aren't patently obvious to people meeting me for the first time

  1. Book: I'm writing a book with Romain Guy, entitled Filthy Rich Clients. We are (pleaseopleaseopleaseoplease) nearly done with the draft and are in the process of sending it out for reviews.  Expect to see the book or our corpses (or possibly both) at JavaOne 2007. I'll try to post an outline here soon to whet your appetite.
  2. Comedy: I just can't pack enough geek jokes into a tech article to satisfy the beast in me, so I've been trying my hand at pure humor lately. This has come out in the form of both writing (see my personal blog at http://chetchat.blogspot.com) and (oddly) stand-up. The stand-up attempt is on a bit of a breather, however; my wife gave me the choice of two out of three non-work activities for the winter:  (1) finish the book (see #1 above), (2) stand-up, and (3) marriage. It was a difficult decision, but my wife's way too scary for me to cross willingly, so stand-up drew the short straw.
  3. Out of the Loop: I know what you're thinking; you're thinking "Chet's so hip; he must be on the top of the Web Wave." Well, perhaps not, but you might assume that anyone with two blogs must know something about the blogosphere.  You'd be wrong. I didn't even know what tagging was until I bumped into Chris posting his response last week. I feel like such a web dinosaur, you're lucky I don't post my blogs IN ALL CAPS.
  4. Output-Only Device: Again with the blogs; you might assume that with a couple of regularly-updated blogs of my own, I must read a lot of blogs myself. Nope. Not at all.  It's not a hard and fast rule; I do sometimes find myself reading a blog.  But I generally just don't have the time for it. I sincerely hope that people still keep reading my tech blog. And my other blog. And I'll sincerely try to make both worthwhile. But that comes at the cost of not getting around to reading other blogs in the meantime. In the finite amount of time in life, some things just gotta go (see #2 above). I made a similar point at a dinner party one time, where the guests were all either leads from various web technologies or bloggers or (a position I still don't get) blog aggregators. I said "Is anyone out there actually reading all of these blogs?" The room went silent, you could almost hear the needle scratching the album as everyone turned to look at me like I had slugs coming out of my nose. Then they resumed their conversations like I hadn't said a word. Which, come to think of it, was kind of the point I was making about blogs; everyone's talking, is anyone listening?
  5. Color Theory: I was once an expert in 'color theory' (or at least I got an advanced degree in it). Terms like 'tristimulus values' and 'realistic image synthesis' and 'green' would come bounding out of my mouth like my dog afer the mailman. Now it's takes a supreme effort to bother using more than a single color in any demo application I write.  It's all about the performance with me now, and my days and nights running 24-hour renderings to produce color-correct static images just didn't cut it for my inner Graphics Geek.

Well, I caved completely and came up with my five things. I feel so soiled. But the chain mail stops here; no more derefs to others. Maybe I have no friends. Maybe I have friends, but can't afford to lose any. Maybe I'm hypocritically sticking to my principles after completely giving in and writing this posting. But for whatever reason, you'll have to search elsewhere to find more victims.





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds