The operation principle and life cycle of Java applet
The 1.Applet is embedded in the HTML page and executed by the applet's container (appletviewer or Web browser).
The 2.Applet operation is controlled by the browser and is not controlled by the code in the applet. When the browser loads a Web page containing the applet, it generates an object for the applet class and then uses the The five public void methods of the Applet class object control the execution of the applet, which is as follows: Init,start,paint,stop,destory;
3.Applet has 5 public void methods (Applet lifecycle):
Init ()
When you start the applet, the browser always invokes the default constructor of the applet class to generate the object, and then invokes the Init () method to initialize the row. Typically in this method, you build the objects that the applet will run and initialize all the data members of the Applet class.
Start ()
Called by the browser. Start or restart the applet. When the applet first starts, the Start method is invoked by the browser immediately after the init () method. If the user leaves the current HTML page, the start () is returned to the current HTML page. Method also invokes the. Start () method, which is typically used to start the people and additional threads needed by the applet
Paint (Graphics g)
This method is called to paint after the init () method completes and the start () method is started. In addition, this method is called each time the applet needs to be redrawn. Typical applications of this method include the use of applet containers to pass to paint () Method Graphics Object G drawing.
Stop ()
This method is called by the browser when the user leaves an HTML page that contains the applet. When the Stop method is invoked, all actions initiated in the start () method are immediately stopped.
Destory ()
When the applet is run, the Destory () method is invoked to release any system resources that the applet occupies, managed by the local operating system. The Stop () method is always called before this method executes.
Second, Java applet programming
The creation of the Graphics object G in the 1.paint method is the responsibility of the applet container (appletviewer or Web browser).
2. In the Paint (Graphics g) method, the first sentence is often written in super.paint (g), which is used to invoke the paint method of the parent applet. In general, there is no such statement to run, but in a complex applet that has a large number of drawing components and GUI components, Ignoring this statement can cause serious errors. Therefore, it is a good programming habit to set this statement on one line of the paint method when writing applet programs.
import java.awt.Graphics;
import javax.swing.JApplet;
public class DrawMultiStringApplet2 extends JApplet{
// 在applet上绘制文本
public void paint(Graphics g){
super.paint(g);
g.drawString("Java TM Applet", 25, 25);
}
}
The origin of the 3.Java coordinate system is in the upper-left corner, in pixels. The pixel is the smallest display unit on the computer screen.
4. When drawing text in Java, adding "\ n" line breaks is not a wrap, sometimes it shows a black box representing unknown characters, or simply does not appear.
The 5.Image class is an abstract class, so? An applet cannot directly create an object of the image class, and the applet must call a method to have the Applet container load and return the object of the Inmage class to which the program is to be used. JApplet Applet provides a method called GetImage, This method loads the image into the applet by receiving two parameters---the location and file name of the image file. For example; logojpg = GetImage (Getdocumentbase (), "logo.jpg");
When the 6.repaint () method is invoked, the entire background needs to be cleared before the paint method is invoked to display the drawing. This is the flicker that is seen by the user during a short interval of time to clear the background river. The following two methods can obviously eliminate or weaken the flicker:
Overload update () method
When AWT accepts a request to be redrawn by the applet, the Update method of the applet is invoked. By default, the Update method clears the background of the applet and then calls the Paint method. Overload Update method, You can include the drawing code in the Paint method previously in the applet method to avoid clearing the entire area each time you redraw the test.
Double Buffering Technology
Double buffering technology is used in many animation applets. The main principle is to create a background image, to draw each frame into the image, and then call the DrawImage method, Draw the entire background image to the screen one at a time. The advantage of this method is that most of the drawing is done in the background. Draws the background-drawn image to the screen one time. Before creating a background image, you first generate the appropriate back buffer by calling the CreateImage method, and then get the drawing environment in the That is, the graphics class object).
Summary: To sum up, to improve the drawing of the idea is: not directly in the paint method to invoke a variety of rendering methods, but using the overload Update method and double buffering technology to generate an image of the buffer, the buffer in the drawing environment, Reads the drawing environment into memory. The Paint method is no longer responsible for drawing work on the image, that is, the paint method no longer loads any image-drawing code. In the Paint method, we call the Update method directly to draw the image in the memory buffer drawing environment, When all the image drawing work is finished, the contents of the buffer are written to the applet at once and displayed directly in the Applet window. This method cleverly solves the problem that the image loses the river flicker.
Deep learning of Java applets
Learning Java applet data is too easy to get, after you installed the JDK, there is a demo directory inside the JDK, which has the applet boutique source code, all of them are classic, run these applet code, you will find that the applet is so powerful, the implementation of three-bit graphics , colorful animations, clocks and so on.
Iv. Application Fields of applets
Now, with the popularity of Flash, applets have faded from the realization of colorful web animation stage. Now applets are generally used in complex dynamic web graphics, human-computer interaction and so on. For example, you can use the applet to achieve the stock code of the dynamic curve rendering, displayed in the page, you can also use the applet to do some browser-based complex real-time web monitoring system, such as through internate or intranet to achieve the factory machine operating parameters of the test, etc. These are other web technologies that are hard to implement.