Java
  Home arrow Java arrow Page 4 - Getting Started with Java 2D
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Getting Started with Java 2D
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 3
    2007-05-08

    Table of Contents:
  • Getting Started with Java 2D
  • Two-dimensional drawing
  • Image Processing
  • Using Java 2D API Step By Step

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Getting Started with Java 2D - Using Java 2D API Step By Step


    (Page 4 of 4 )

    Since Java 2D API addresses both drawing on components as well as drawing on images, the following items need to be addressed:

    1. Drawing on Components
    2. Drawing on Images

    Both require almost the same steps but the details vary. Here are the details.

    Drawing on Components

    Drawing on a component is one of the basic aspects of even working with images. However, it also means creating new components, which is an advanced usage of drawing on components. The following are the steps required to use Java 2D API in components:

    • Override the paint method.
    • Pass Graphics2D object.
    • Call methods of Graphics2D object to draw.

    The steps are very similar to how one would work with prior versions of Java (since the time of JDK 1.1). The step that deviates from it is the second one. Now I will explain each of the steps in more detail. 

    The first step is to override the paint method. Every AWT component capable of rendering itself has the paint() method. Similarly every component visible on the screen has the paintComponent() method. To draw on a component either paint() or paintComponent() has to be overridden based on which toolkit is being used. For example if AWT is being used, the paint() method has to be overridden thus:

    public void paint(Graphics g) {

    }

    The second step is to pass a Graphics2D object. This is where using Java 2D API differs from using drawing methods provided by the core library in Java 2. In Java 2 based drawing, paint() or paintComponent() is passed an object of the Graphics class. However, with Java 2D, instead of a Graphics object, an object of Graphics2D is passed. The twist in the tale is that paint() or paintComponent() still uses a Graphics object as an argument. But inside the method it is type cast into a Graphics2D object before invoking any drawing methods. In code it would be:

    public void paint(Graphics g) {
       Graphics2D g2 = (Graphics2D)g;
    // Now we can do cool 2D stuff.
    }

    The third step is to call methods of the Graphics2D object to draw. Graphics2D provides a wide array of drawing methods corresponding to the functionalities discussed in the first section. These methods will be discussed in detail in a future article. One of the examples of the drawing method is translate() which displaces an on-screen object such as a shape or image by specified as arguments.

    Drawing on Images

    Images can be of two kinds: one created by Java 2D API itself and one that has been imported such as a digital image or photograph. Even though drawing on an image doesn't require any component specific method to be overridden, the aforementioned distinction plays a major role. In order to manipulate an image, you must first obtain the Graphics2D object, then call the methods of the Graphics2D object to draw. Note that the second step is the same as that of the third step of drawing on components.

    Let's look at obtaining the Graphics2D object. Since there are two types of images, there are two ways to obtain the Graphics2D object. First, you can call the getGraphics() on Image object. Image object represents the first kind of image that is the one created by using Java API itself. In order to obtain the Graphics2D object, we must call the getGraphics() method of Image class and typecast it to Graphics2D. The following code block illustrates the point.

    public void drawOnImage(Image i) {
       Graphics2D g = (Graphics2D)i.getGraphics();
    // Now draw on the image using g.
    }

    Second, you can call createGraphics() on the BufferedImage object. BufferedImage class represents the imported image type. It can be used to represent a photograph or a digital image in an application. The way to obtain the Graphics2D object is to call createGraphics() which returns a Graphics2D object. In code it would be:

    public void drawOnBufferedImage(BufferedImage bi) {
       Graphics2D g2 = bi.createGraphics();
    // Now draw on the image using g2.
    }

    Finally we're up to the second step of drawing on images, which involves calling the methods of the Graphics2D object to draw and otherwise manipulate the image. The methods that work on shapes also work on images. One such method is translate() that displaces the image.

    In the next article we will look at an application that will show the use of the Java 2D API in the real world.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this article I have discussed the basics of Java 2D graphics. Please do...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek