An image is a picture composed of a group of pixels and stored in binary form. Java language supports three main image file formats: GIF, JPEG and BMP. The image processing function of java language is encapsulated in the Image class.
Image loading and output
In a Java program, images are also objects, so when loading an image, you must first declare the Image object, and then use the getImage() method to associate the Image object with the image file. There are two ways to load image files:
Image getImage(URL url), url specifies the location and file name of the image.
Image getImage(URL url,String name), url specifies the location of the image, and name is the file name.
For example, the following code declares an Image object and uses the getImage() object to associate it with the image file:
Image img = getImage(getCodeBase(),"family.jpg");
The URL (uniform Resource Location Uniform Resource Locator) object is used to identify the name and address of the resource and is used when the WWW client accesses Internet resources. There are two ways to determine the position of an image: absolute position and relative position. Methods for obtaining relative positions include:
URL getCodeBase(), get the location of the applet file.
URL getDocumentBase(), get 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);
Methods to obtain image information (attributes) are:
getWidth(ImageObserver observer): Get the width;
getHeight(ImageObserver observer): Get the height.
The code for outputting images is written in the paint() method. There are 4 ways to display images:
boolean drawImage(Image img,int x,int y,ImageObserver observer)boolean drawImage(Image img,int x,int y,Color bgcolor,ImageObserver observer)boolean drawImage(Image img,int x,int y,int width,int height ,ImageObsever observer)boolean drawImage(Image img,int x,int y,int width,int height,Color bgcolor,ImageObsever observer)
The parameter img is the Image object, x, y are the upper left corner positions of the image rectangle, observer is the image observer when loading the image, bgcolor is the background color used to display the image, width and height are the rectangular area where the image is displayed. When this area When the size of the image is different, the displayed image will be scaled.
The Applet class also implements the ImageObserver interface, and this is often used as an actual parameter. See the following code and comments:
(1) g.drawImage(image1,0,0,this);//Original image display
(2) g.drawImage(image2,10,10,Color.red,this);//Graphic plus background color 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 image display
(4) g.grawImage(labImag,0,120,100,100,this);//zoom display
(5) g.grawImage(labImag,0,240,500,100,this);//Zoom display
[Example] A small application uses the init() or start() method to download (obtain) an image, and uses the paint() method to display the obtained 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); }}Since there is no getImage() method provided in classes such as Frame, JFrame and JPanel, they need to use the Toolkit abstract class in java.awt.Toolkit to load images, which has methods for loading image files:
In this way, various components can use the getToolkit() method to obtain the Toolkit object, and then display the image through the Toolkit object in the component's paint() method. The following code illustrates such usage:
Toolkit tool = getToolkit(); URL url = new URL(http://www.weixueyuan.net/image.gif); Image img = tool.getImage(url);
Components can also use the static method getDefaultToolkit() provided by Toolkit to obtain a default Toolkit object and use it to load images. At this time, the code to load the image is often written like this:
Image img = Toolkit.getDefaultToolkit().getImage(url);