Synthetic Images You do not have to read all images from the file - you can create your own images. To create your own image, the most flexible method is to use a BufferedImage object, which is a subclass of the Image class, which stores the image data in a buffer that can be accessed. It also supports methods of various storage pixel data: using or does not use Alpha channels, different types of color models, and various accuracy of color components. The ColorModel class provides a flexible way to define a variety of color models to use with the BufferedImage object. In order to understand the basic knowledge of the color model, we will use only one default color model that consists of RGB values and a buffer type (stored in 8 bits of RGB color value 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 used to use an int value. The value of each pixel is stored in an 8-bit byte form plus an RGB color component. We can create this type of bufferedimage object with a given width and height, the code statement is as follows:
INT width = 200; bufferedimage image = new bufferedimage (width, height, bufferedimage.type_int_argb); this code creates a bufferedimage object that represents a 200 pixel wide image. To apply this image, we need to have a graphical context, and the CreateGraphics () method of the BufferedImage object returns a graphics2D object related to the image:
INT width = 200; graphics2d g2d = image.creategraphics (); Use the G2D object to modify the pixel of the BufferedImage object image. With this object, you are now fully capable to apply the BufferedImage object. You can draw shapes, images, generalpath objects, or anything else, and you can set an Alpha combination object for the graphic context. You also have all affine deformation capabilities provided by Graphics2D objects. If you want to get a single pixel from the BufferedImage object, you can call its getRGB () method, and provide the x, y coordinates of the pixel as the parameter of the INT type. This pixel will be returned in the Type_INT_ARGB format with an int type, which consists of a 32-bit word by four 8-bit values (represents the alpha value and the RGB color component). At the same time, getRGB () has an overloaded version that returns a pixel array from some image data. You can also set a single pixel by calling the setRGB () method. The first two parameters are the coordinate values of the pixel, and the third parameter is the value to be set, the type INT. This method also has a version that can set the value of the pixel array. At this time, we have completed the learning of pixel operations. Below we want to create an applet, which makes the BufferedImage object on the Wrox logo background. Our example will also demonstrate how to let the image transparent. The basic content of the applet is 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 .. ................................................................................................. ...} Creating a sub-graphic is a small graphic image that can draw it in a still image to create an animation. To create an animation effect, you only have the mapping in different locations and directions over time. Of course, the deformation of the coordinate system can be simplified. Games often use sub-graphics - Since you only need to draw sub-graphics on a static background, you can reduce the processors occupied by the animation. Our interest in using the bufferedimage object means that we will no longer cost effort to reduce the best technologies for processor time, but put your attention to understand how to create and use images inside one program. Our bufferedimage object looks as shown in Figure 1: Figure 1. BUFFEREDIMAGE graph
This image is a square in SPRITESize as a side length. The dimensional values of the other parts of the image are associated with this edge length. In fact, there are only two geometry entities, one line and a circle, all repeatedly in different positions and directions. If we create a line2d.double object representative line, create an Ellipse2D.double object representative circle, then we can draw the entire image through the mobile user coordinate system and one or other objects in the two objects. If it is a method of truth-to-object object, you should define a class representing a sub-graph, which may be a subclass of bufferedimage, but because we are exploring the skills using the bufferedimage object, therefore draws bufferedImage with a createsprite () method. The sub-graphics on the object are more suitable for our purposes. Because this method is just a member of our Applet class, we will add data members to the applet to store any data required. You can insert us into the data member into the Applet class, as shown below:
double totalAngle; // Current angular position of spritedouble spriteAngle; // Rotation angle of sprite about its centerImagePanel imagePanel; // Panel to display animationBufferedImage sprite; // Stores reference to the spriteint spriteSize = 100; // Diameter of the spriteEllipse2D.Double Circle; // a circle - part of the spriteLine2d.double line; // a line - part of the sprite // colors buy in spritecolor [] colors = {color.red, color.yellow, color.green, color.blue , Color.cyan, color.pink, color.magenta, color.range}; java.util.tima}; // Timer for the animationlong interval = 50; // Time Interval Msec Between Repaint The general purpose of these members can be from comments See it clearly. Let's take a look at how they are used when developing code. The first thing you need to do is to create a BufferedImage object Sprite, then we also need a Graphics2D object to draw on the Sprite image. Below is the code to complete these operations:
BufferedImage createSprite (int spriteSize) {// Create image with RGB and alpha channel BufferedImage sprite = new BufferedImage (spriteSize, spriteSize, BufferedImage.TYPE_INT_ARGB); Graphics2D g2D = sprite.createGraphics (); // Context for buffered image // plus the REST OF THE METHOD ...} The wide and high values of the Sprite object are spritesize. The type of image is type_int_argb, that is, the alpha value and color components of each pixel are stored in a separate int value, and the color is Stored in the form of 8 bits of red, green, and blue components. This means that our Sprite image will take 40,000 bytes, which is only a small part of the memory that will occupy a web page. This does not affect the download time of the web page, because when executing Applet, this part of the memory is assigned on the local machine. In addition to the contents of the HTML files of the web themselves, the download time also depends on the size of the Applet's .class file, and images or other files downloaded when it executes. Creating a transparent background In the Sprite image, Alpha channel is important because we hope that the background can be completely transparent. During the painting process, only the Sprite object itself should be visible instead of the entire 100 × 100 rectangular image. We can easily achieve this, as long as the entire Sprite image area is transparent (ie, the alpha value is 0.0F), then draw the graphics we want to draw on top, make it opaque (Alpha value is 1.0F ). The following is a code that makes the entire image transparent:
// Clear image with transparent alpha by drawing a rectangleg2D.setComposite (AlphaComposite.getInstance (AlphaComposite.CLEAR, 0.0f)); Rectangle2D.Double rect = new Rectangle2D.Double (0,0, spriteSize, spriteSize); g2D.fill ( RECT); We first use the alphacomposite object to set the alpha synthetic value according to the CLEAR rule, set the color component to zero, and it is transparent by setting the Alpha value of 0.0F. Then we fill a rectangle covering the entire image area. We don't have to set color values, because according to CLEAR rules, each pixel's foreground and background colors are zero, so both do not participate in pixel generation. But we still have to fill this rectangle because this will determine the image pixels operated. Here, we can learn how to control the quality of the image. The colored fine-tuning is a problem between quality and speed. Coloring operations are like most things - quality is the cost, and the cost here is processing time. All coloring operations have default settings, which exist, the default settings are platform-specific, but you can choose from the setRenderingHint () method you can use by calling the setRenderingHint () method for the coloring graphics2D object. Although there are only some fine-tuning, if your computer does not support coloring operation options corresponding to the fine tuning you specify, these fine-tones cannot take effect. By adding the following to the CreateSprite () method, you can ensure the best effects that can be generated by our Alpha synthesis operations.
BufferedImage createSprite (int spriteSize) {// Create image with RGB and alpha channel BufferedImage sprite = new BufferedImage (spriteSize, spriteSize, BufferedImage.TYPE_INT_ARGB); Graphics2D g2D = sprite.createGraphics (); // Context for buffered image // Set best alpha interpolation quality g2D.setRenderingHint (RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); // Clear image with transparent alpha by drawing a rectangle g2D.setComposite (AlphaComposite.getInstance (AlphaComposite.CLEAR, 0.0f)); Rectangle2D.Double rect = new Rectangle2d.double (0, 0, spritesize, spritesize); g2d.fill (Rect); // Plus the rest of the method ...} RenderingHints class defines a variety of coloring fine tones, which stored in a map set Graphics2D object in. The parameter of the setRenderingHint () method is a key and the corresponding key value. In our code, the first parameter is a key representing the fine tuning of Alpha, and the second parameter is the fine-tuning value. The fine-tuning other possible values have value_alpha_interpolation_default, representation of the platform default; and value_alpha_interpolation_speed, representing the pursuit speed rather than quality. You can also provide fine-tuning for the following keys: