Java Applet Programming

Source: Internet
Author: User

I. Java Applet running principle and lifecycle
1. the Applet is embedded in the HTML page and executed by the applet container (appletviewer or Web browser.

2. the running of the Applet is controlled by the browser rather than the code in the Applet. when the browser loads a Web page containing the Applet, it generates an Applet Class Object and uses the five public void methods of the Applet Class Object to control the Applet Execution, the five methods are as follows: init, start, paint, stop, destory;

3. the Applet has five public void methods (the life cycle of the Applet ):
Init ()
When the Applet is started, the browser always calls the default constructor of the Applet Class to generate the object, and then calls the init () method to initialize it. generally, this method generates the objects required to run the Applet and initializes all the data members of the Applet Class.
Start ()
Called by the browser. start or restart the Applet. when the Applet is started first, the start method is called by the browser immediately after the init () method. if the user leaves the current HTML page and returns to the current HTML page again, the start () method is also called. the start () method is generally used to start the people and additional threads required by the applet.
Paint (Graphics g)
After the init () method is executed and the start () method is started, this method is called to draw images. in addition, this method is also called every time the Applet needs to be re-drawn. A typical application of this method, including the use of the Applet container to pass the painting () method Graphics object g drawing.
Stop ()
When the user leaves the HTML page containing the Applet, the browser calls this method. After the stop method is called, all operations started in the start () method will be immediately stopped.
Destory ()
When the Applet is terminated, call the destory () method to release any system resources occupied by the Applet managed by the local operating system. the stop () method is always called before this method is executed.
 
Ii. Java Applet Programming
1. the Graphics object g in the painting method is created by the Applet container (appletviewer or Web browser.
 
2. in the painting (Graphics g) method, the first sentence is often written as super. paint (g) is used to call the painting method of the parent Applet. in general, this statement can be run without it, but in a complex Applet with many drawing components and GUI components, ignoring this statement may cause serious errors. therefore, when writing an Applet, you must set this statement on the line of the paint method. This is a good programming habit.
Import java. awt. Graphics;
Import javax. swing. JApplet;
Public class DrawMultiStringApplet2 extends JApplet {
// Draw text on the applet
Public void paint (Graphics g ){
Super. paint (g );
G. drawString ("Java TM Applet", 25, 25 );
}
}
3. The origin of the Java coordinate system is in the upper left corner, in pixels. pixels are the smallest display units on the computer screen.
 
4. When you draw text in Java, you cannot wrap a line break by adding "". Sometimes, a black box is displayed to indicate unknown characters, or the text cannot be displayed.
5. The Image class is an abstract class, so? The Applet cannot directly create an Image Class Object. the Applet must call a method to load the Applet container and return the Inmage class object to be used by the program. the super-class Applet of JApplet provides a method named getImage, which loads the Image into the Applet. The method receives two parameters-the location and file name of the Image file. for example, logoJPG = getImage (getDocumentBase (), "logo.jpg ");
 
6. when the repaint () method is called, you need to clear the entire background before calling the paint method to display the painting. in this way, what users see in the short interval of clearing the background river image is flashing. the following two methods can be used to obviously eliminate or reduce flickering:
Overload update () method
When AWT receives the Applet re-drawing request, it calls the update method of the Applet. by default, the update method clears the background of the Applet and then calls the paint method. reload the update method to include the drawing code in the paint method in the Applet method. This prevents the entire area from being cleared every time you redraw the method.
Dual-buffer technology
The Double Buffer technology is used in many animated applets. the main principle is to create a background image, draw each frame into the image, and then call the drawImage method to draw the entire background image to the screen at a time. the advantage of this method is that most of the painting is performed in the background. draw the background image to the screen once. before creating a background image, call the createImage method to generate an appropriate background buffer, and then obtain the drawing environment (Graphics class Object) in the buffer ).
 
Conclusion: To sum up, the idea of improving the drawing is: instead of calling various painting methods directly in the painting method, the reload update method and double buffering technology are used to generate an image buffer, after obtaining the drawing environment in the buffer, read the drawing environment into the memory. the painting method is no longer responsible for image painting, that is, the painting method no longer loads any image painting code. in the paint method, we directly call the update method to draw images in the drawing environment of the memory buffer. After all the images are drawn, finally, write the buffer content to the Applet at a time and display it directly in the Applet window. this method cleverly solves the problem of image loss and flickering.
 
Iii. Deep Learning of Java Applet
It is too easy to learn the Java Applet materials. After you install JDK, there is a demo directory in JDK, which contains the excellent source code of the Applet, Which is classic and runs the Applet code, you will find that the Applet features are so powerful that it can implement three-digit graphics, a variety of animations, clock and so on.
 
Iv. application fields of Applet
Now, with the popularity of Flash, the Applet has faded out to implement a rich and colorful web page animation stage. Currently, Applet is generally used in complex and dynamic Web applications, such as graphics and human-computer interaction. For example, you can use an Applet to draw a dynamic curve of the stock code and display it on the page. You can also use an Applet to build a browser-based complex real-time web monitoring system, for example, internate or intranet is used to detect the operation parameters of factory machines, which are difficult to implement by other web technologies.

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.