Creating image _jsp Programming in Java applications

Source: Internet
Author: User
Tags time interval
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.

If it is a true object-oriented approach, you should define a class to represent a child shape, possibly as a subclass of BufferedImage, but since we are exploring the technique of using BufferedImage objects, we use a createsprite () method to draw The bufferedimage on the object will be more appropriate for our purposes. Because the method is only a member of our applet class, we will add data members to the applet to store any required data. You can insert the data members that we will use into the applet class as follows:

Double Totalangle; Current angular position of sprite
Double Spriteangle; Rotation angle of Sprite about its center
Imagepanel Imagepanel; Panel to display animation

BufferedImage Sprite; Stores reference to the sprite
int spritesize = 100; Diameter of the Sprite
Ellipse2d.double Circle; A Circle-part of the Sprite
Line2d.double Line; A Line-part of the Sprite

Colors used in Sprite
color[] Colors = {color.red, color.yellow, Color.green, Color.Blue,
Color.cyan, Color.pink, Color.magenta, color.orange};

Java.util.Timer Timer; Timer for the animation
Long interval = 50; Time interval msec between repaints

The general purpose of these members can be clearly seen from the comments. Let's take a look at how they were used when developing the code.

The first thing the Createsprite () method needs to do is create the BufferedImage object Sprite, and then we need a graphics2d object to paint on the sprite image. The following is the code that completes 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 width and height values of the sprite objects are spritesize, and the type of the image is Type_int_argb, that is, the alpha and color components of each pixel are stored in a separate INT value, and the color is stored in 8-bit red, green, and blue components. This means that our sprite image will take up to 40,000 bytes, which is just a small fraction of the memory consumed by browsing a Web page. This does not affect the download time of the Web page, because this part of memory is allocated on the local machine when the applet is executed. In addition to the content of the HTML file that is the Web page itself, download time depends on the size of the applet's. class file and the images or other files that are downloaded when it executes.

     Create a transparent background

In the sprite image, the alpha channel is important because we want the background to be completely transparent. In the painting process, only the Sprite object itself should be visible, not the entire 100x100 rectangular image. We can easily do this by starting by making the entire sprite image area transparent (that is, Alpha is 0.0f), and then drawing the graphic we want to draw on top to make it opaque (alpha value is 1.0f). Here's the code that makes the entire image transparent:

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);

We first use the Alphacomposite object to set Alpha compositing values according to the clear rule, set the color component to zero, and make it transparent by setting the alpha value to 0.0f. Then we populate a rectangle that covers the entire image area. We do not have to set the color values because the foreground and background color of each pixel is zero based on the clear rule, so neither of them participates in pixel generation. But we still have to populate the rectangle, because this will determine the pixel of the image being manipulated.

Here, we can learn a little bit about how to control the quality of the image.

   Coloring Fine Tuning

For many aspects of coloring operations, there is a problem of choosing between quality and speed. Coloring operations are like most things-quality comes at a cost, and the price here is processing time. All shading operations have a default setting, where there is a choice, the default setting is platform-specific, but you can choose by calling the Setrenderinghint () method of the Graphics2D object for coloring. Although there are only a few tweaks, these tweaks will not work if your computer does not support the shaded operation options that correspond to the fine tuning you specify.

By adding the following call to the Createsprite () method, you can ensure that you get the best results that can be generated by our alpha compositing 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 ...
}

The Renderinghints class defines a variety of shader tweaks that are stored in a Graphics2D object in a mapping set. The parameter of the Setrenderinghint () method is a key and the corresponding key value. In our code, the first parameter is a key that represents Alpha compositing fine-tuning, and the second parameter is the value of the spinner. Other possible values for this fine-tuning are Value_alpha_interpolation_default, representing the platform defaults, and Value_alpha_interpolation_speed, which represents the pursuit of speed rather than quality.

You can also provide fine-tuning for the following keys:

Key description
Key_antialiasing Determines whether anti-aliasing is used. When you color a line with a slanted angle, you usually get a set of stepped pixel permutations that make the line look uneven and often called jagged shapes. Anti-aliasing is a technique that sets the pixel brightness of a line with a slanted angle to make the line look smoother. As a result, this fine-tuning is used to determine whether it takes time to reduce jagged shapes when coloring angled lines. Possible values are value_antialias_on, _off or _default.
key_color_rendering controls how colors are colored. Possible values are value_color_render_speed, _quality or _default.
key_dithering controls how jitter is handled. Jitter is the process of synthesizing a larger range of colors with a limited set of colors by coloring adjacent pixels to produce new color illusions that are not in the group's colors. Possible values are value_dither_enable, _disable or _default.
The quality of the key_fractionalmetrics text. Possible values are value_fractionalmetrics_on, _off or _default.
Key_interpolation determine how to do interpolation.

When a source image is distorted, the deformed pixel is rarely able to correspond to the target pixel position exactly. In this case, the color value of each deformed pixel has to be determined by the surrounding pixels.

Interpolation is the realization of the above process. There are a number of technologies available. Possible values, by processing time from up to the least, are value_interpolation_bicubic, _bilinear or _nearest_neighbor.

Key_rendering determines the coloring technology and balances the speed and quality. Possible values are value_rendering_speed, _quality or _default.

Key_text_antialiasing determines whether anti-aliasing is rendered to the text. Possible values are value_text_antialiasing_on, _off or _default.

We've gone far enough. Let's go back to the drawing sprite ...

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.