Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Antialiasing, images, and alpha compositing in Java 2D

Java 2D adds support for antialiased rendering, image transforms, and alpha compositing

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 4 of 5

Here is how I manipulated the image.

001   /**
002    * Creates an Image, then does multiple manipulations on
003    * it to demonstrate Java 2D image manipulation features.
004   **/
005   public void paint(Graphics g) {
006     Graphics2D g2d = (Graphics2D) g;
007 
008     //Again, let's set the antialiasing rendering hint on.
009     g2d.setRenderingHints(Graphics2D.ANTIALIASING,
010        Graphics2D.ANTIALIAS_ON);
011 
012     //Load the Image using the loadImage() helper method.  For this 
013     //example, I copied my JavaWorld column photo from:
014     //  http://www.javaworld.com/javaworld/icons/a-day.jpg
015     //for the image bits.  I assume that this image is on your local
016     //machine, in the directory from which you are running Example07.
017     //If you would like to use a different image or directory, load the
018     //image from another machine, or use a different access protocol, 
019     //change the following variables accordingly.
020     String imageProtocol = "file";
021     String imageMachine = "localhost";
022     String imageFilepath = "day.jpg";
023     Image myImage = loadImage(imageProtocol,imageMachine,imageFilepath);
024 
025     //Let's display a copy of our image at x=25, y=75.
026     AffineTransform at = new AffineTransform();
027     at.translate(25.0f,75.0f);
028     g2d.transform(at);
029 
030     //Note that we pass in an identity matrix as the default Image
031     //Space transform (the second parameter to drawImage).  The last
032     //parameter refers to the observer for this drawing operation, 
033     //in this case our instantiated Example07 object.
034     g2d.drawImage(myImage,new AffineTransform(),this);
035 
036     //Now, let's scale the image and display it to the right 
037     //of the original for comparison.  We first use setToTranslation
038     //so that we can reset the transform before adding our new 
039     //translation matrix, then we apply the scaling directly to the 
040     //Image Space, rather than scaling the entire User Space.
041     at.setToTranslation(100.0f,0.0f);
042     g2d.transform(at);
043     AffineTransform atImageSpace = new AffineTransform();
044     atImageSpace.scale(1.2f,1.2f);
045     g2d.drawImage(myImage,atImageSpace,this);
046 
047     //Now let's shear the image and re-display it so you can compare
048     //the sheared version with the previous two.  We re-use our
049     //previous User Space transform, while we re-set the Image Space
050     //transform to shear instead of scale myImage.
051     g2d.transform(at);
052     atImageSpace.setToShear(0.2f,0.1f);
053     g2d.drawImage(myImage,atImageSpace,this);
054   }


And here is the output of these manipulations.

Three images of Bill in various states of transformation.

Notice how I am careful to clear each AffineTransform by calling the setToxxx() methods. For instance, in line 027 I use translate() to set up the first translation, but in line 041 I use setToTranslation() so that the AffineTransform is reset to the identity matrix before the new translation matrix is concatenated.

The loadImage() method uses the standard Java 1.0/1.1 mechanisms to create a URL and use it in turn to create an Image object. Please review the source listing if you need more information on this method.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

Try thisBy Anonymous on March 5, 2009, 11:37 amimport java.awt.RenderingHints; // Anti Aliasing RenderingHints rh = g.getRenderingHints(); rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ...

Reply | Read entire comment

Compilation errorsBy Anonymous on March 3, 2009, 7:05 amImport, import java.awt.RenderingHints; and change: g2d.setRenderingHints(Graphics2D.ANTIALIASING,Graphics2D.ANTIALIAS_ON); to: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

Reply | Read entire comment

Compilation errorsBy Anonymous on November 16, 2008, 9:40 amExample05.java:67: cannot find symbol symbol : variable ANTIALIASING location: class java.awt.Graphics2D g2d.setRenderingHints(Graphics2D.ANTIALIASING, ...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • An introduction to drawing text in JDK 1.0 and 1.1 environments. Figure 4 examines imported concepts related to text placement. If you have ever wondered what a typographer meant by advance and descent, this article can help. http://www.javaworld.com/javaworld/jw-01-1997/jw-01-howto.html
  • Information on using TextLayout for internationalization support in the 1.2 Java platform. Accompanied by excellent illustrations that help to simplify the more convoluted concepts in the text. http://www.ibm.com/java/education/international-text/
  • Everything you wanted to know but were afraid to ask about fonts and internationalization in Java. This site includes separate sections on Java 1.0, 1.1, and 1.2 platforms, for those of us that have to work in multiple releases (a great many of us, I suspect). Includes information on the dreaded Font.properties file. http://www.alumni.caltech.edu/~dank/javafont.htm
  • Charles Poynton's A Technical Introduction to Digital Video covers a variety of analog and digital video topics. It includes a very good chapter on signal filtering and sampling, which gives in-depth information on aliasing effects. http://www.amazon.com/exec/obidos/ASIN/047112253X/billday/
  • The article "Put This in Your Pipe" walks through the process by which Graphics2D actually renders out 2D graphics. It discusses the pipeline nature of Graphics2D, and how this affects the order of operations in Java 2D code. http://java.oreilly.com/news/knudsen/java_0498.html
  • Another one of Knudsen's Java 2D articles, "The Truth about Antialiasing," gives more details about how and why aliasing occurs, and another example showing how to deal with aliasing in Java 2D. http://java.oreilly.com/news/knudsen/java_0598.html
  • Sun API specification for Java 2D. This javadoc-generated documentation is updated with each new release of the Java 1.2 platform. http://java.sun.com/products/jdk/1.2/docs/guide/2d/index.html
  • "How Do I Process Images with Java," by Aaron Michael Cohen, Dr. Dobb's Journal, July 1998. This article discusses the producer-consumer model from Java 1.0 and 1.1 in more depth. Source code is available online. http://www.ddj.com/ddj/1998/1998_07/index.htm
  • JavaOne 1998 presentation materials for "Developing Imaging Applications Using the Java 2D API and Java Advanced Imaging API." Provides a good comparison of JDK 1.0 and 1.1's Push Model supported by ImageProducers and ImageConsumers to the Immediate Mode image buffer model supported by Java 2D BufferedImages and the Pull Model to be supported by Java 2D and Java Advanced Imaging. This session can show you how all three models fit together and what trade-offs you should consider when you decide which to use. http://java.sun.com/javaone/javaone98/sessions/T606/index.html
  • "The Care and Handling of Color," another nice Java 2D article by Knudsen. Gives a good explanation of how difficult color management is in general, then goes on to explain how Java 2D provides a uniform way to manage color spaces for display as well as hard copy output. http://java.oreilly.com/news/knudsen/java_1097.html
  • This Java Developer Connection article on color management in the Java 1.2 environment, "New Color ClassesA Programmer's Palette of Color," complements Knudsen's article fairly well. It contains a list of the types of color spaces supported in the JDK 1.2. (Accessing this article requires a free username and password.) http://developer.java.sun.com/developer/technicalArticles/monicap/2DGraphics/Color/color.html
  • If you are having problems and are beginning to wonder if you might have a Java 2D bug on your hands, you can search the Java bug database at Sun. Select "java bugs" from the pull down menu, then search for "general:classes_2D". (Accessing this page also requires a free username and password.) http://developer.java.sun.com/developer/search.shtml
  • Bill's other Media Programming columns /javaworld/topicalindex/jw-ti-media.html
  • Bill archives Media Programming resources on his Web site. This archive contains the up-to-date media.jar file with code fixes for all of the examples in the column. http://reality.sgi.com/bday/Work/index.html