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:
Drawing on Components
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:
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. |