Basic program Slice (Java)

Source: Internet
Author: User
Tags inheritance

Libraries are often grouped according to their capabilities. Some libraries, such as those used, are interrupted and shelved. The standard Java library string and vector class is one such example. Other libraries are specially designed, such as building blocks to build other libraries. Some classes in the library are the framework of the application, designed to help us build applications that produce the basic activity status of each particular application in the context of providing a class or class set. Then, for us to customize the status of the activity, we must inherit the application class and discard the benefits of the program. The default control structure of the application framework will invoke our deprecated program at a specific time. The application framework is a good example of "separating, changing, and aborting events" because it always tries to focus on all the special pieces of the program that are discarded.

The application framework is used to build the patch. We inherit the program from the class and discard the particular program. Most of the time we have to consider some of the important ways that we have to run to create and use the pieces on a Web page. These methods are:

Method

Operation

Init ()

Called when the applets are the created to perform first-time initialization of the applet

Start ()

Called every time the applets moves into sight on the Web browser to allow the applets to start up their normal operations (es Pecially those that are shut out by stop ()). Also called after init ().

Paint ()

Part of the base class Component (three levels of inheritance up). called as part of a update () to perform special painting in the canvas of an applet.

Stop ()

Called every time the applets moves out of sight on the Web browser to allow the applets to shut off expensive operations. Also called right before destroy ().

Destroy ()

Called when the applets is being unloaded the "page to perform final release of" The applet is no longer Used

The


Method action

Init () program is created for the first time, and the initial run of the initialization program calls
Start () called whenever the program slice enters a Web browser and allows the program to start its general operation (a special piece of the program is closed by stop) , and also after Init (), calls part of the
Paint () base class component (up to three levels in the inheritance structure). Called as part of update () to make a special depiction of the canvas of a piece of paint
Stop () calls each time a piece of the program leaves from the view of the Web browser, allowing the program to close costly operations; call
Destroy () before calling Destroy () The patch is no longer needed and is called when it is unloaded from the page to perform the last cleanup of the resource

now look at the paint () method. Once component (currently a program) decides that it needs to be updated, it calls this method-probably because it turns the screen again, first on the screen, or because other Windows temporarily overwrite your Web browser. The program then calls its update () method (defined in the underlying class component), which restores everything that is recoverable, while invoking paint () is part of the process. There is no need to overload the paint (), but building a simple piece of the program is no doubt a convenient way, so we start with the paint () method first. When
Update () calls Paint (), it passes a handle to the graphics object that represents the surface to be depicted (plotted) on. This is very important because we are limited by the appearance of the project components and therefore cannot be drawn outside the area, which is a good thing, otherwise we will draw outside the line. In the case of a program slice, the appearance of the patch is the area defined. A
graphic object also has a series of actions that we can perform on it. These operations are related to drawing on the canvas. So most of them involve images, geometric dishes, arcs, and so on (note that if you are interested, you can find more detailed descriptions in the Java documentation). Some methods allow us to draw characters, and the most common one is drawstring (). For it, you need to indicate the string that you want to portray, and specify its starting point in the drawing area of the program slice. This position is represented in pixels, so it looks different on different machines, but at least it can be ported.
Based on this information, you can create a simple piece of the program:
 

: Applet1.java
//Very Simple applet
package C13;
Import java.awt.*;
Import java.applet.*;

public class Applet1 extends applets {public
  void paint (Graphics g) {
    g.drawstring ("The" the "the", ",");
  }
} ///:~

Note that this slice does not need to have a main (). All content is encapsulated into the application framework; We put all the startup code in init ().
You must put this program on a Web page to run, and you can only see this page in a Java-enabled web browser. In order to place a piece of a program into a Web page, you need to set a special tag (note ①) in the Code of that Web page to indicate that the Web page loads and runs the program slice. This is the applet tag, which looks like the following in Applet1:

<applet
code=applet1
width=200
height=200>
</applet>

①: This book assumes that the reader has mastered the basics of HTML. This knowledge is not difficult to learn, there are many books and online resources can provide help.

Where the code value specifies the name of the. class file, and the program resides in that file. Width and Height Specify the initial dimensions of this piece of the program (as described previously, in pixels). You can also put something else in the applet tag: a special identifier (name) that is used to find other. class files on the Internet (codebase), alignment and arrangement information (align), and a specific identifiers (a) that enable the pieces of the program to communicate with each other, as well as parameters that provide information that the The parameters take the following form:
<paramname= identifier value = "Info" >
You can set any number of such parameters as needed.
The only thing we need to do in a simple piece of the program is to set up a patch tag (applet) in the Web page as described above to load and run the program slice.

Related Article

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.