VJ6.0 usage (5) Applet compilation and debugging process

Source: Internet
Author: User

 

OneCompilation and debugging of Applet
Although the Applet and Application are quite different, the operation is basically the same as the Application operation, but there are still some differences in editing. Therefore, here is an example. This example is a little complicated. I hope I have learned some basic Java knowledge. The purpose of this example is to implement a Java Applet program for running a horse lamp. The effect of this example is as follows:

1. Select "new" from the "new" menu, and then select the "new" option: Applet on HTML. After setting the name and path, click "OK, the system will automatically generate one or two files, one is the initial code file of the Applet and the other is the HTML webpage file. For example:

2. In the project browsing window, double-click the Applet1 file to open the code file. The code file is relatively long, which is not listed here. Double-click the page1.html file to open the HTML file, as shown in:

There are three options in this window. If you have used Frontpage, you will be very familiar with this. Like Frontpage, It is a Web Editor, And the Toolbox also contains the control you want to edit the web page. You don't need to explain it more. Check the Source code. After opening the Source code, some may display the content of the Applet. Some may display the code, which is related to the initial setting of VJ6. Right-click the Applet and select "Always view as text" to view the code, the Code is as follows:
<Applet
Code = Applet1.class
Name = Applet1
Width = 320
Height = 200 VIEWASTEXT>
<Param name = label value = "This string was passed from the HTML host.">
<Param name = background value = "008080">
<Param name = foreground value = "FFFFFF">
</Applet>

You don't need to explain this much. Just change it according to your own requirements. You cannot click Quick View for Quick preview because the Applet has not been compiled into a Class file.

3. Run the Applet and press the F5 key to run it directly. VJ6 will automatically compile the program and open the HTML file in the default browser and display it.

4. At this time, we need to add code. The expected goal can be achieved only after necessary code is added! Open the source code file Applet1 and double-click it!

The first step is to set variables. The variables are placed in the main class as follows:

Font ft; // text Font object instance
String theString, font, style; // The moving String, font type, and style.
Int size, speed; // text size, movement speed
Int xSet, Max, Min; // x coordinate of the Text Location, right boundary, and left boundary

After the variables are defined, we need to add some initial code like the source code given.

Private final String param_string = "string ";
Private final String param_font = "font ";
Private final String param_style = "style ";
Private final String param_size = "size ";
Private final String param_speed = "speed ";

What we need to do now is to get various parameters from the HTML of the webpage file, just like getting parameters from the initial code, which can be written by referring to the initial code! As follows.

String param;
Param = getParameter (param_string );
If (param! = Null) theString = param; // obtain the string parameter
Param = getParameter (param_font );
If (param! = Null) font = param; // obtain the font Parameter
Param = getParameter (param_size );
If (param! = Null) size = Integer. parseInt (param); // retrieves the size parameter
Param = getParameter (param_style );
If (param! = Null) style = param; // obtain the font style parameters.
Param = getParameter (param_speed );
If (param! = Null) speed = Integer. parseInt (param); // get the text moving speed Parameter
Int vice_style = Font. PLAIN;
If (style. inclusignorecase ("BOLD "))
Vice_style = Font. BOLD;
If (style. inclusignorecase ("ITALIC "))
Vice_style = Font. ITALIC; // The resulting Font parameter is case-insensitive.

Ft = new Font (font, vice_style, size );
FontMetrics fm = getFontMetrics (ft); // generate a FontMetrics class object instance
Min =-fm. stringWidth (theString );
Max = getSize (). width;
Offset = Max;

The next step is to do the most important work. The main part of programming is the process of font scrolling. This part of the code is not explained in detail. I hope you have learned the Java syntax.

Public void paint (Graphics g)
{
Int ySet = getSize (). height; // yset is the y coordinate variable of the output string.
YSet = (ySet-size)/2;
G. setFont (ft );
G. setColor (Color. red );
G. drawString (theString, xSet, ySet + 40 );
// + 40 is used to adjust the height of the output.
XSet --;
If (xSet
)

Public Thread TextMoveRunnable = null;
Public void start ()
{
If (TextMoveRunnable = null)
{
TextMoveRunnable = new Thread (this );
TextMoveRunnable. start ();
}
}
Public void run ()
{
While (true)
{
Try {
Repaint (); Thread. sleep (speed );
} Catch (InterruptedException e ){}
}
}
Public void stop ()
{
If (TextMoveRunnable! = Null)
{
TextMoveRunnable. stop ();
TextMoveRunnable = null;
}
}

This part of the Code should be added in parallel with initForm (), either before or after. After the code is added, the program is complete. However, if you compile the program at this time, an error is displayed on the Task List and the error is "Class Thread doesnt have a constructor that matches Thread (Applet1) (J0082) "This is because the added Code involves the thread, but the main program does not know, there is no corresponding interface, (J0082) is the wrong serial number, find the corresponding tool, A detailed explanation of this error is provided. To solve this problem, you only need to add an interface after the main class. As follows:

Public class Applet1 extends Applet implements Runnable

Pay attention to the following "implements Runnable ". After compilation, the error has been removed, but the result displayed at this time is the same as that displayed at the beginning. This is because the settings have not been set in HTML.

ViewContent of Applet1.java:
Import java. awt .*;
Import java. applet .*;

/**
* This class reads PARAM tags from its HTML host page and sets
* The color and label properties of the applet. Program execution
* Begins with the init () method.
*/
Public class Applet1 extends Applet implements Runnable
{
/**
* The entry point for the applet.
*/
Font ft; // text Font object instance
String theString, font, style; // The moving String, font type, and style.
Int size, speed; // text size, movement speed
Int xSet, Max, Min; // the displacement of each text movement, right border and left border


Public void init ()
{
InitForm ();

UsePageParams ();

//

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.