Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Java applets employ many of the same programming concepts as JavaScript. Applet writers can determine which variables and methods are accessible to calls from outside the applet. All public items can be accessed from JavaScript. For example, instead of defining an instance variable like this:

int speed;

a Java programmer would make the variable accessible by JavaScript with the following variable declaration:

public int speed;

The same applies to methods in a class, so a method such as the following could be invoked by a JavaScript script:

public void start() {
 if(thread == null) {
thread = new Thread(this);
thread.start();
 }
}

Variables and methods which are not declared with the public keyword are not accessible by JavaScript, and thus play no role in LiveConnect. In this section, we’ll base our discussion on the nervous class. Here is the source code for that class:

import java.awt.Graphics;
import java.awt.Font;
import java.applet.Applet;

public class nervous extends Applet implements Runnable {
 // array declarations
 char separated[];

 // thread declarations
 Thread killme = null;

 // font declarations
 Font displayFont;

 // integer declarations
 int i;
 int xCoordinate = 0, yCoordinate = 0;
 int speed;
 int counter = 0;
 int fontSize;
 int fontStyle;

 // string declarations
 String num;
 String fontName;
 String text = null;

 // Boolean declarations
 boolean threadSuspended = false;

 public void init() {
   getAttributes();
   separated = new char[text.length()];
   text.getChars(0, text.length(), separated, 0);
   resize(460, 60);
   displayFont = new Font(fontName, fontStyle, fontSize);
 }

 public void start() {
   if (killme == null) {
   killme = new Thread(this);
   killme.start();
   }
 }

 public void stop() {
   killme.stop();
   killme = null;
 }

 public void run() {
   while (killme != null) {
 try {
 Thread.sleep(speed);
 } catch (InterruptedException e) {}
 repaint();
   }
   killme = null;
 }
 public void paint(Graphics g) {
   g.setFont(displayFont); // set current font
   for (i = 0; i < text.length(); ++i) {
  xCoordinate = (int) (Math.random() * 10 + 15 * i);
  yCoordinate = (int) (Math.random() * 10 + 36);
  g.drawChars(separated, i, 1, xCoordinate, yCoordinate);
   }
 }

 /**************************************
  private methods for internal use
 ***************************************/

 private void getAttributes() {
   // get text parameter
   text = getParameter("text");
   if (text == null)
  text = "JavaScript"; // default value

   // get speed parameter
   String speedString = getParameter("speed");
   if (speedString == null)
   speedString = "100"; // default value
   speed = Integer.parseInt(speedString);

   // get font parameter
   fontName = getParameter("font");
   if (fontName == null)
   fontName = "TimesRoman"; // default value

   // get size parameter
   String temp = getParameter("size");
   setFontSize(temp); // invoke function to set size

   // get style parameter
   temp = getParameter("style");
   setFontStyle(temp); // invoke function to set style
 }

 private void setFontSize(String size) {
   try {
 fontSize = Integer.parseInt(size);
   }
   catch (Exception e) {
   fontSize = 36;
   }
 }

 private void setFontStyle(String style) {
   try {
   if (style.equalsIgnoreCase("Plain"))
fontStyle = Font.PLAIN;
   else if (style.equalsIgnoreCase("Italic"))
fontStyle = Font.ITALIC;
   else
fontStyle = Font.BOLD;
   }
   catch(Exception e) {
fontStyle = Font.BOLD;
   }
 }
}

Example 31-3a (nervous.java). The source code for a simple “nervous text” applet.

If you are a Java programmer, you’ll immediately notice that the preceding Java code is not perfect, but it’s good enough to demonstrate LiveConnect features.

The public method start() launches the thread that allows the applet to run. The public method stop() halts the applet from running, by terminating the current thread.

Since both of these methods are public, we can invoke them from JavaScript. Figure 31-2 shows a very simple way of implementing LiveConnect: Invoking Java statements from JavaScript when the user triggers an event.


Figure 31-2.  The start and stop buttons call the applet’s start() and stop() public methods, respectively.

Take a look at the HTML document that lays out the Web page as seen in Figure 31-2:

<HTML>
<HEAD>
<TITLE>NervousText #1</TITLE>
</HEAD>
<BODY>
<APPLET CODE="nervous" WIDTH=460 HEIGHT=60 NAME="nervousText">
<PARAM NAME="text" VALUE="Nervous Text">
<PARAM NAME="speed" VALUE="100">
<PARAM NAME="font" VALUE="Arial">
<PARAM NAME="size" VALUE="36">
<PARAM NAME="style" VALUE="plain">
</APPLET>
<FORM NAME="attributes">
<INPUT TYPE="button" VALUE="start"
onClick="document.nervousText.start()">
<INPUT TYPE="button" VALUE="stop"
onClick="document.nervousText.stop()">
</FORM>
</BODY>
</HTML>

Example 31-3 (ex31-3.htm). Two HTML buttons controlling a Java applet.

Now we’ll see how those buttons can invoke the applet’s methods.

The applet Object

You are probably familiar with the object concept of JavaScript. Frames, documents, forms, and form elements are all different types of JavaScript objects. Any applet loaded in your document becomes an object, contained by a document object.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us