Home  >  Article  >  Java  >  Learn more about java applets

Learn more about java applets

Y2J
Y2JOriginal
2017-05-18 10:59:082545browse

1. The operating principle and life cycle of Java Applet
1. The Applet is embedded in the HTML page and executed by the applet container (appletviewer or Web browser).

2.Applet The operation is controlled by the browser, not by the code in the Applet. When the browser loads the Web page containing the Applet, it will generate an object of the Applet class, and then use the five public void methods of the Applet class object to control the Applet. Execution, these five methods are as follows: init, start, paint, stop, destroy;

3. Applet has 5 public void methods (Applet life cycle):
init()
When starting an Applet, the browser always calls the default constructor of the Applet class to generate an object, and then calls the init() method for initialization. Generally, in this method, the objects required for the Applet to run are generated and all data members of the Applet class are initialized.
Start()
Called by the browser. Start or restart the Applet. When the Applet is started for the first time, the start method will be called by the browser immediately following the init() method. If the user leaves the current HTML page, When returning to the current HTML page, the start () method will also call the .start () method is generally used to start the people and additional threads needed by Applet (Graphics G)
at the init () method to execute the end of the execution. , after the start() method is started, this method is called to draw the picture. In addition, this method will also be called every time the Applet needs to be redrawn. Typical applications of this method include using the Applet container to pass the Graphics object g to the paint() method. Draw a picture.
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 stopped immediately.
destory()
When terminating the running of the Applet, call the destory() method to release any system resources occupied by the Applet and managed by the local operating system. Before executing this method, always call the stop() method.

2. Java Applet
Programming 1. The creation of the Graphics object g in the 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 super.paint(g), which is used to call the paint method of the parent class Applet. Under normal circumstances, it can be run without this statement, but in a system with many
In complex Applets with drawing components and GUI components, ignoring this statement may lead to serious errors. Therefore, when writing an Applet program, be sure to set this statement in a line of the paint method. This is a good programming Habit.

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

3. The origin of the Java coordinate system is in the upper left corner, in pixels. Pixel is the smallest display unit on the computer screen.

4. When drawing text in Java, add " " line breaks Characters cannot be line-wrapped, and sometimes a black box will be displayed to indicate unknown characters, or they may not be displayed at all.
5. The Image class is an abstract class, so Applet cannot directly create the Image class Object, the Applet must call a method to let the Applet container load and return the object of the Inmage class to be used by the program. JApplet's super class Applet 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, the entire background needs to be cleared , and then call the paint method to display the painting. In this way, what the user sees during the short time interval between clearing the background and drawing the image is flickering. The following two methods can significantly eliminate or weaken the flickering:
 ​ Overload update() method
When AWT receives a request to redraw the Applet, it calls the Applet's update method. By default, the update method clears the background of the Applet and then calls the paint method. By overloading the update method, you can include the drawing code previously used in the paint method in the Applet method, thereby avoiding the need to clear the entire area every time you redraw. Animation
Applet is used. 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 once. The advantage of this method is that most of the drawing It is performed in the background. The background-drawn image is drawn to the screen at one time. Before creating the background image, first generate a suitable back buffer by calling the createImage method, and then obtain the drawing environment (i.e. Graphics class object) in the buffer.
Summary: To sum up, the idea to improve drawing is: instead of calling various drawing methods directly in the paint method, use the overloaded update method and double buffering technology to generate an image buffer. After obtaining the drawing environment in the buffer, read the drawing environment into the memory. The paint method is no longer responsible for the image drawing work, that is, the paint method no longer loads any image drawing code. In the paint method, we directly call update Method, the image drawing work is performed in the drawing environment of the memory buffer. When all the image drawing work is completed, the contents of the buffer are finally written to the Applet at once and displayed directly in the Applet window. This method is very clever. It effectively solves the problem of image loss and flickering.
3. In-depth learning of Java Applet
It is too easy to obtain the information for learning Java Applet. After you
install
the JDK, There is a demo directory in the JDK, which contains high-quality Applet source codes, all of which are classics. Run these Applet codes, and you will find that the Applet function is so powerful, realizing three-dimensional graphics, colorful animations, clocks, etc. .
4. Application fields of Applet Now, with the popularity of Flash, Applet has faded out of the stage of realizing rich and colorful web page animation. Applets are now generally used in complex and dynamic Web application graphics fields,
human-computer interaction
, etc. For example, you can use Applet to draw dynamic curves of stock codes and display them on the page. You can also use Applet to build some complex browser-based real-time web monitoring systems, such as detecting factory machine operating parameters through internate or intranet, etc. , these are difficult to achieve with other web technologies.
【Related Recommendations】

1.

Special Recommendation

:"php Programmer Toolbox" V0.1 version Download2. Java Free Video Tutorial

3.

Teach you to use applet to create a system so that the browser can access the web Serve

4. Detailed explanation of an html tag embedded in java: applet

5. Teach you how to configure the Applet environment

The above is the detailed content of Learn more about java applets. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn