Home Java javaTutorial Learn more about java applets

Learn more about java applets

May 18, 2017 am 10:59 AM

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); 
                        } 
        }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles