JavaApplet programming skills
1. Java applet overview
The Java AppleT program is classified into two categories: Applets and Java Application, these two categories are zone. The Java Applet is embedded in the page of WWW, as a component of the page is downloaded, and can be run in a web browser in real Java virtual machine (JVM). Java's safety machine system can prevent the procedure to use the problem of local documents or other security. And a Java should be operated outside the web browser, there is no multi-limit system when there is no applet run. In addition, the largest part of the two program settings is in: Java Applet does not have the main program, and Java should be used to have a main program. Each applet in Java is now realized by the subclass of the applet. The APPLET of the Friendly Customed Applets Through the Renewal Applet The initialization, painting and operation of the small application program. These functions are init (), paint (), start (), stop (), and Destory ().
An applet's life cycle is related to the web page. When the page contains an Applet is added first, the viewer calls the init () method, and it is initialized by applet. The post () or START () method is then modulated or started. When the user leaves the page, the browser calls the STOP () method stops the small program operation. If the user closes the browser will stop the Applet, the browser calls the Destory () method to end, so that the small application program organically releases the resource that is locked during the period. Only by user does not turn off the browser, renew the loading page, the browser only calls the start () method and the Paint () method to re-draw and run a small program. The Applet lifecycle is shown in Figure 1.
2. Extension Little Square Category Support Multi-thread Tongli, Designing a small program is to add interactions to the home page, so that the page is more active. Therefore, there is a truth that the truth is prone, such as moving, real-time update Internet server information, and generally entertaining, etc., the logical main body of the small program is in the unlimited cycle. This logic main body should not be placed in several browser calls, such as init (), Paint (), start (), etc. This is because of hidden circumstances, the small routine has the main logger to use a single thread. When the browser is called, the program control is not returned. If the skillet is still running after replacing another page, it will waste the resource. You can do an experiment, use a small program to operate the small program, you will hang a full-stop process, because it is necessary to wait until the call function returns to return, it will continue to handle other things (such as response menu). Obviously, this logical main body must be placed in other places. The solution is to add a thread in a small program frame, and put the logical main body in the thread RUN () method. The thread is generated only in the browser call square START (), and the thread is running in STOP (), which does not occupy the resource source when the user does not check the read. Figure 2 shows the relationship between the browser, the small program, and the thread. By the browser / small program / thread is the model used by all views, and the java.applet.applet class does not support the thread. In actual use, for easy operation, we can derive the Applet class to generate a subclass that supports threads. When writing Applets in the future, you can derive it directly from subclasses, and the program logic main body is placed in the Run () method, will thread Control is hidden. This subclass is as follows:
import Java.applet *;. import Java.awt *;. class MultiThreadApplet extends Applet implements Runnable {Thread thisThread; Public void start () {if (thisThread == null) {thisThread = new Thread (this); thisThread.start ( ); {} public void stop () {if (Thisthread! = null) {thisthread.stop (); Thisthread = null;}} PUBLIC VOID RUN () {}} Due to Run () is an abstract method of interface runnable, so It is also necessary to define in the MultithreadAdapplet class, but the content is empty. The following example shows how to use this class. The program contains an unlimited loop accumulation operation and display the result on the screen: import java.awt. *; Public class counter extens Multithreadapplet {int counter; public void run () {while (true) {counter ; repaint ();} } Public void Paint (GRAPHICS G) {g.drawstring (string.valueof (counter), 10, 10); {} Obviously, this small program has been hidden in the Multi Threadapplet.
3. Solution of some of the questions in the picture is the motion of the moving figure, and a series of continuously displayed still-stop images will give me a fantastic image of continuous movement. Just just moving the speed enough, the scattered still diagram will be combined into a continuous operation.
Painting the braking is simple, just follow three steps; (1) Wipe the entire sub-order plot zone; (2) Re-draw the dynamic painting; (3) in the new position plotting the drawing. However, when the movement and color of the continuous frame are not continuous, the flash is generated (excluded). Question The screen refreshes in the small program area, there are two ways to prevent the stop. The first method is to do only the local brush, that is, only the changed part is erased. For example, if you want to draw a "star-flying star" moving, each time you brush the new operation, first wipe the first star star, and then draw one in the new position. However, if the motion ratio is more complex, the moving portion has more overlapping, and the local brush is very popular and the speed of the row. In this case, it can be solved with another method (dual cache). Its nuclear thinking is to finish all frames of one frame outside the screen, then display the frames of the most afterward in the sub-program screen. The process is shown in Figure 3. Examples are as follows:
import java.awt *;. public class Ball extends MultiThreadApplet {Image ball = null; Image applet = null; Graphics appletG, ballG; Public void run () {If (ball = = null) {Applet = createlmage (bounds (). Width, bounds (). height); ball = crete image (70, 70); appletg = applet.getgraphics (); ballg = ball.getgraphics (); ballg.setcolor (color.gray); ballg.draworal (0, 0, 70, 70); {for (int x = 0; x <400; x ) {double angle = "((Double) x) / 20;" int y = "(math.abs (Math. SIN (ANGLE) * 80); "Appletg.clearRect (0, 0, Bounds () .width.bounds () (); DrawBackground (appletg.drawImage); Appletg.drawImage (Ball, X.80-Y, this) THIS.GETGRAPHICS () .drawImage (Applet, 0, 0, this); try {thread.sleep (25);} catch (exception ignored) {}}}} G) {for (int i = "0; I" <1; i ) {g.drawline (0.i * 10, 400, i * 10);}}} The small program first uses CreateImage () to obtain the exterior graphics cache that is exactly the same as the small program size. Give the variable applet, then get the cached drawing object Appletg. The rear of the backfraction of the frame is the needle to Appletg. This includes clear frames, draws back views, and draws the drawing in the new position. Finally, use the DrawImage () method to copy the cache to the small program. Run this small program, you will send it to the current painting is not often smooth, and there is no flash in flash. In addition to flashing, if the motion is more than complex, the frame rate can be lowered. If the drawing is reduced to 20, the frame is a motion that will show an exemplary effect. Therefore, it is necessary to optimize the animation speed, and there are some feasible solutions; (1) draw as little as possible; (2) use a faster drawing algorithm; (3) use a color cycle and other techniques to paint some part; (4) The detail is expressed by the pattern; (5) Use the fixed point integer to perform three-dimensional calculation; (6) Handle the complex algorithm by the pre-calculated result of the query.
4. Regeneration Short-proofing 装 装 时 真 小 程 程 身 身 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时 时With small programs, the more the more INTERNET band is used to transmit these small programs from all local servers to the customer web browser, so the response time will be dropped. For shorter shortcuts and installation time, the following three methods can be taken. Make the execution file as small as possible, although the Internet (TCP protocol) can accept the data stream of any length, but the network device (for example, a router) responsible for point-to-point, there is a strict limit on the upper limit of the transfer packet package, usually this limit is 256 Entarity. Therefore, when the small program is transmitted online, it is divided into a dry package on the upper limit it, and the last package can only be only a few bytes. These packages are single-oriented, and you need to have new components to the original minus. When the last package delay is late, you can use a few seconds to wait for the first few bytes to arrive. The only solution is to keep the class documents. Developers can try the following scenarios without affecting the logical subject: (1) Delete the error code; (2) Reduce String direct quantity; (3) Select the Java compiler to optimize the compilation option; (4) Delete Method, etc.
Class scattered holds in the controlled range, there is no small program is made by a class. Tongmong, the developer is a high-level sub-program that is a strong manner, and has a stem class from the standard Java base class, and combines it. Therefore, if you want a small program, the Java virtual machine is to deliver and install these classes. If there are many more than the number of modern classes, the time to download is longer. We all know that downloading 20 independent data blocks take more time than downloading a big data block equivalent to 20 data blocks, which is because downloading each independent data block is re-connected, and It takes time to take time for each joint. Therefore, when the developer is set up, the number of classes should be selected, so that the structure can be guaranteed clear, but also to ensure that the programs are simple, and no redundancy is added.