Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 24 - Pages: 2   [ 1 2 | Next ]
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Ultimate Java Image Manipulation

URL: Javalobby

At 4:00 AM on Sep 17, 2007, Michael Urban wrote:

Images are the staple of any graphical application, whether on the web or on the desk, images are everywhere. Having the ability to control and manipulate these images is a crucial skill that is absolutely necessary for any graphical artist, designer, or Game Engineer. This article will get you, the aspiring artist, professional designer, or amateur hobbyist, the foundations to be able to manipulate any image to your will.

Read the full article here.
1 . At 8:50 AM on Sep 17, 2007, Jan Erik wrote:
  Click to reply to this thread Reply

0x007300

Wow.. written by a high school senior. The mask and sprite code is really cool. So I made a Javascript "mashup" in Darkstar FX .

One thing that is strange. My color picker ( Colorspace ) reports that the green in the Mario tile is #008500 in Firefox but #007300 when I paint the same image in Java 2D or open the file in Photoshop.
http://labs.teppefall.com
2 . At 8:58 AM on Sep 17, 2007, Rick Ross wrote:
  Click to reply to this thread Reply

Re: 0x007300

> Wow.. written by a high school senior.

Amazing, isn't it! Josiah did a fabulous job, and I hope we'll see more like this from him. This is great stuff!

Rick
www.dzone.com - fresh links for developers
bestuff.com - the best stuff in the world
3 . At 9:54 AM on Sep 17, 2007, David wrote:
  Click to reply to this thread Reply

Re: 0x007300

Yes, that's a great article - thanks. The author obviously knows his stuff!!

As someone who normally lives in the EE arena, its good to read about desktop Java and how things like graphics manipulation are achieved.
4 . At 10:33 AM on Sep 17, 2007, Jochen Bedersdorfer DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Ultimate Image Manipulation

Nice article, although it doesn't cover painting with Graphics2D in an orderly fashion, namely in the paintComponent method.
You don't call drawImage outside the event thread.
There is no guarantee whatsoever that anything will be drawn.
The correct time to draw something on the screen is when paintComponent is called, taking into account any clipping rectangle.

Also, I wouldn't dream on using ResizeListener to redraw. AWT handles this part quite nicely.
5 . At 11:00 AM on Sep 17, 2007, Romain Guy DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

Nice article. However, if you need more information about resizing images, take a look at Chris Campbell's article: http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

It explains in great details why getScaledInstance() is a lot slower than the method described by Josiah.

Also, flipping can be achieved by using the Graphics2D.scale() method, similarly to how rotation is done. It might be a bit easier to read.

I also HIGHLY recommend reading Chet's article on BufferedImages:

http://weblogs.java.net/blog/chet/archive/2004/08/toolkitbuffered.html
http://weblogs.java.net/blog/chet/archive/2003/08/bufferedimage_a.html
http://weblogs.java.net/blog/chet/archive/2003/08/bufferedimage_a_1.html
http://weblogs.java.net/blog/chet/archive/2003/09/volatileimage_n.html

Those are quite necessary to get a better understanding of Java images. More importantly, those articles shed the light on some performance issues you may encounter. Be sure to read about the managed images.
Romain Guy
Romain Guy's Java Weblog, #ProgX, Jext
6 . At 11:14 AM on Sep 17, 2007, Anthony Goubard wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

You can also read the Chapter 8 of Filthy Rich Client while waiting for the book to arrive ;-).

http://www.informit.com/articles/article.aspx?p=1013851&rl=1
Anthony Goubard
JLearnIt, Ant Commander, XINS, japplis.com.
7 . At 12:47 PM on Sep 17, 2007, Romain Guy DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

You can also do that :)
Romain Guy
Romain Guy's Java Weblog, #ProgX, Jext
8 . At 4:04 PM on Sep 17, 2007, Chet Haase wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

Definitely check out the resources that Romain pointed to, especially Chris's article on why getScaledInstance() is horribly slow (and what better alternatives exist).

I would also point out that at a minimum, you should set the rendering hint to scale using Bilinear filtering when using the drawImage() call. This will give you much better quality than the default scale, at reasonable performance (nowhere near the hit of getScaledInstance()).

So where the article has this code:

Graphics2D g = dimg.createGraphics();
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);

I would recommend this instead:

Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);

This will result in a decent-quality scale when down-scaling to an image that's greater than half the size of the original image. When downscaling by a larger factor, you'll want to use the approach outlined in Chris's article.

Chet.
9 . At 5:08 PM on Sep 17, 2007, Kirill Grouchnikov wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

Quite a lot of bad advice here, especially for drawing images on frames. Getting the Graphics of the root pane directly is *not* how it should be done. Of course you will have problems on resizing the frame - you're not doing the painting in paintComponent. You will also have problems on hiding and reshowing the frame.

The solution is not to add resize listeners. The solution is to load the image and add a panel to the frame. The panel will override the paintComponent method and paint the image using the passed graphics. This will provide complete support for resizing, reshowing and double buffering.

Hopefully this article will be corrected so it won't mislead future Google referees.

Kirill
10 . At 2:51 AM on Sep 18, 2007, Josiah wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

Chet

That makes sense, thanks, Ill add that in.
I read some of your articles by the way, their really great, and informative.

-Josiah
11 . At 3:14 AM on Sep 18, 2007, Josiah wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

You're right, I was merely demonstrating a way to display the images. The point was to give them image functions and let the reader apply them, wrapped in their own applications. But I see your point, so I will update the article to use a sub-class of JPanel to put the image in. That will solve the resizing, buffering and threading problems I hope.

Thanks for your help!

-Josiah
12 . At 10:39 AM on Sep 18, 2007, Kirill Grouchnikov wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

Thanks, Josiah

Main thing is that many of the intended audience will simply copy-paste the code they find on the Internet, so you should be very careful in writing code under such a title a "Ultimate Java Image Manipulation".

Looking forward to seeing next articles.

Kirill
13 . At 12:13 PM on Sep 18, 2007, Dmitri Trembovetski DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

> Main thing is that many of the intended audience will simply copy-paste the code they find on the Internet,

Which is why I always sneak in some code that sends money to my paypal account in all sample code I publish! =)

Dmitri
Java2D Team Sun Microsystems, Inc
14 . At 1:18 PM on Sep 19, 2007, Peter wrote:
  Click to reply to this thread Reply

Re: Ultimate Java Image Manipulation

Unfortunately an exception is thrown loading image coded in CMYK colorspace (javax.imageio.IIOException - Unsupported Image Type). Anyone knows workaround for this?

thread.rss_message