Creating images in a Java application

Source: Internet
Author: User

Synthetic images

You don't have to read all the images from the file-you can create your own images. The most flexible way to create your own image is to use a BufferedImage object, which is a subclass of the image class that stores the images data in a buffer that can be accessed. It also supports a variety of methods for storing pixel data: Using or not using alpha channels, different kinds of color models, and the various precision of color components. The ColorModel class provides a flexible way to define a variety of color models to work with BufferedImage objects. To understand the basics of color model work, we will only use a default color model, whose color component consists of an RGB value and a buffer type (8-bit RGB color values plus an alpha channel). This buffer type is specified by the constant TYPE_INT_ARGB in the BufferedImage class, which means that each pixel is to use an INT value. The value of each pixel is to store an alpha component in 8-byte form plus an RGB color component. We can create a BufferedImage object of this type with the given width and height, with the following code statement:

int width = 200;
int height = 300;
BufferedImage image = new BufferedImage(width,
height,BufferedImage.TYPE_INT_ARGB);

This code creates a BufferedImage object that represents a 200-pixel-wide, 300-pixel-high image. In order to apply this image, we need to have a graphics context, and the BufferedImage object's CreateGraphics () method returns a Graphics2D object associated with the image:

int width = 200;
Graphics2D g2D = image.createGraphics();

Actions that use the G2d object modify the pixels of the BufferedImage object image. With this object, you are now fully capable of applying the BufferedImage object. You can draw shapes, images, generalpath objects, or anything else, and you can set Alpha group objects for the graphics context. You also have all the affine deformation capabilities provided by the Graphics2D object.

If you want to get a single pixel from a BufferedImage object, you can call its getRGB () method and provide the x,y coordinates of that pixel as parameters of type int. This pixel is returned in TYPE_INT_ARGB format with the INT type, consisting of four 8-bit values (representing alpha values and RGB color components) to form a 32-bit character. getRGB () also has an overloaded version that returns an array of pixels from the image data. You can also set a single pixel by calling the Setrgb () method. The first two parameters are the coordinates of the pixel, and the third parameter is the value to be set, and the type is int. This method also has a version that can set the value of a group of pixels.

So far we have completed the study of pixel operation. Here we'll build an applet that animates the BufferedImage object on the Wrox logo background. Our example will also demonstrate how to make the image partially transparent. The basic contents of the applet are as follows:

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
public class ImageDrawDemo extends JApplet
{
 // The init() method to initialize everything...
 // The start() method to start the animation...
 // The stop() method to stop the animation...
 // The ImagePanel class defining the panel displaying the animation...
 // Data members for the applet...
}

Create an image

A child shape is a small graphic image that can be drawn in a static image to create an animation. To create an animation effect, you only have to draw the child shapes in different positions and orientations over time. Of course, the use of coordinate system deformation can simplify many. Games often use sub graphics-because you only need to draw a child graph on a static background, you can reduce the amount of processor time that the animation takes up. Our interest in using BufferedImage objects means that we will no longer expend the effort to study the best technology to reduce processor time, but instead focus on understanding how to create and use images within a program.

Our BufferedImage object looks like the image in Figure 1:

Figure 1. BufferedImage Child Graphics

This image is a square with a long spritesize edge. The dimensions of the other parts of the image are related to this side length. In fact, there are only two geometric entities, a line and a circle that are repeated in different positions and directions. If we create a Line2d.double object that represents a line and create a Ellipse2d.double object that represents a circle, then we can draw the entire image by moving the user's coordinate system and drawing one or more of these two objects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.