An image is a set of pixels, a picture saved in binary form. The Java language supports the 3 main image file formats of GIF, JPEG, and BMP. The Java language's image processing functionality is encapsulated in the image class.
image loading and output
In a Java program, an image is also an object, so when you load an image, you first declare it, and then use the GetImage () method to associate the image object with the picture file. There are two ways to load an image file:
Image getImage (URL URL) that specifies the location and file name of the image.
Image getImage (URL url,string name), the URL indicates the location of the image, name is the filename.
For example, the following code declares the image object and uses the GetImage () object to connect to the image file:
Image img = getImage (GetCodeBase (), "family.jpg");
The URL (Uniform Resource Location Uniform Resource Locator) object identifies the name and address of the resource and is used when the WWW client accesses resources on the Internet. There are two ways to determine the location of an image: absolute position and relative position. The methods for taking relative positions are:
URL GetCodeBase (), taking the location of the small application file.
URL getdocumentbase (), taking the location of the HTML file.
For example, code:
URL Picurla = new URL (getdocumentbase (), "Imagesample1.gif"),
picurlb = new URL (getdocumentbase (), "pictures/ Imagesample.gif ");
Image Imagea = GetImage (Picurla), Imageb = GetImage (PICURLB);
The methods for obtaining image information (properties) are:
GetWidth (ImageObserver observer): Take width;
GetHeight (ImageObserver observer): Take height.
The code for the output image is written in the paint () method, and there are 4 ways to display the image:
Boolean drawImage (Image Img,int X,int y,imageobserver observer)
Boolean drawImage (Image Img,int X,int Bgcolor,imageobserver observer)
Boolean drawImage (Image img,int x,int y,int width,int height,imageobsever )
Boolean drawImage (Image img,int x,int y,int width,int Height,color bgcolor,imageobsever observer)
Parameter img is an Image object, X,y is the position of the upper left corner of the picture rectangle, observer is the image observer when loading the image, bgcolor is the background of the display image, width and height is the rectangular area of the display image, when this area is different from the size of the image, The display image will have zoom processing.
The Applet class also implements the ImageObserver interface, which is commonly used as an argument. See the following code and comments:
(1) g.drawimage (image1,0,0,this)//original artwork display
(2) G.drawimage (image2,10,10,color.red,this)//graphics plus background display
Note: If the size of the original image is different from the given range, the system will automatically scale
(3) G.drawimage (labimag,0,0,this)//original artwork display
(4) G.grawimage (labimag,0,120,100,100,this)//Zoom display
(5) G.grawimage (labimag,0,240,500,100,this)//Zoom display
The "Example" applet uses the init () or Start () method to download (get) The image, using the paint () method to display the resulting image.
Import Java.applet.*;import java.awt.*;
public class Example7_5 extends applet{
Image Myimag;
public void Start () {
Myimag = GetImage (GetCodeBase (), "mypic.jpg");
}
public void Paint (Graphics g) {
g.drawimage (myimg,2,2,this);
}
}
Because the GetImage () method is not provided in classes such as frame, jframe, and JPanel, loading an image requires the use of the Toolkit abstract class in Java.awt.Toolkit, which has the method of loading an image file:
- Image.getimage (String name): Loads an image file by the specified file name.
- Image.getimage (URL URL): The Uniform Resource Locator loads an image file.
In this way, the various components can get the Toolkit object using the Gettoolkit () method, and then display the image through the Toolkit object in the component's paint () method. The following code signals this usage:
Toolkit tool = Gettoolkit ();
URL url = new URL (http://www.weixueyuan.net/image.gif);
Image img = tool.getimage (URL);
The component can also use the static method provided by Toolkit Getdefaulttoolkit () to obtain a default Toolkit object and load the image with it. At this point, the code that loads the image is often written like this:
Image img = Toolkit.getdefaulttoolkit (). GetImage (URL);