Join Regular Classroom : Visit ClassroomTech

JAVA – codewindow.in

Related Topics

JAVA Programing

What is the difference between the getAppletInfo and getParameterInfo method in an applet?

getAppletInfo() and getParameterInfo() are methods of the Applet class that are used to provide information about the applet to the browser or applet viewer.

getAppletInfo() method returns a string that describes the applet, including its name, author, version, and a brief description. This information is typically displayed in the browser or applet viewer when the applet is loaded.

getParameterInfo() method returns an array of Parameter objects that describe the parameters that the applet expects to receive from the HTML page or applet viewer. Each Parameter object contains the name, type, and description of the parameter. This information is typically used by the browser or applet viewer to generate a user interface for entering parameter values when the applet is loaded.

How do you run an applet in a standalone mode?

To run an applet in standalone mode, you can follow the steps below:

  1. Create a new Java application that includes a main method.

  2. Create an instance of the applet class in the main method.

  3. Call the init(), start(), and stop() methods of the applet to simulate the lifecycle of an applet.

  4. Create a new Frame object and add the applet instance to the frame using the add() method.

  5. Set the size and visibility of the frame using the setSize() and setVisible() methods.

Here is an example code to run an applet in standalone mode:

import java.awt.*;
import java.applet.*;

public class MyApplet extends Applet {
   public void init() {
      // Initialization code here
   }

   public void start() {
      // Start code here
   }

   public void stop() {
      // Stop code here
   }

   public void paint(Graphics g) {
      // Paint code here
   }
}

public class MyStandaloneApplet {
   public static void main(String[] args) {
      MyApplet applet = new MyApplet();
      applet.init();
      applet.start();

      Frame frame = new Frame("My Standalone Applet");
      frame.add(applet);
      frame.setSize(400, 300);
      frame.setVisible(true);

      applet.stop();
   }
}

In this example, we first create an instance of the MyApplet class and call its init() and start() methods to initialize and start the applet. Then we create a new Frame object, add the applet instance to the frame, and set the size and visibility of the frame. Finally, we call the applet’s stop() method to stop the applet when the frame is closed.

Note that not all applets can be run in standalone mode, as some applets rely on the web page or applet viewer for certain functionality, such as accessing parameters or communicating with the server. In addition, running an applet in standalone mode may not be secure, as applets are designed to run within a sandbox environment that limits their access to the local system.

How do you sign an applet?

To sign an applet, you need to obtain a digital certificate from a trusted certificate authority (CA), such as VeriSign, Thawte, or Comodo. The digital certificate contains information about your identity, such as your name and email address, and is used to verify the authenticity and integrity of your applet.

Once you have obtained a digital certificate, you can sign your applet using the jarsigner tool that comes with the Java Development Kit (JDK). Here are the steps to sign an applet:

  1. Compile your applet source code into class files.

  2. Create a JAR file that contains the class files and any other resources needed by the applet, such as images, audio files, or configuration files.

  3. Use the jarsigner tool to sign the JAR file with your digital certificate. You can use the following command:

jarsigner -keystore keystore-file jar-file alias

Here, keystore-file is the path to your keystore file, jar-file is the path to your JAR file, and alias is the alias name of the certificate that you want to use to sign the JAR file.

  1. Verify the signature by running the jarsigner tool with the -verify option:

jarsigner -verify jar-file

This will display information about the signed JAR file, including the signer’s identity and the date and time of the signature.

  1. Optionally, you can include the digital certificate in your JAR file by using the keytool utility that comes with the JDK. You can use the following command:

keytool -export -alias alias -file cert-file -keystore keystore-file

Here, alias is the alias name of your certificate, cert-file is the path to the output certificate file, and keystore-file is the path to your keystore file.

When an applet is signed, it can perform certain actions that are normally not allowed by the Java security model, such as accessing the local file system, making network connections to other hosts, or accessing user-specific data, such as the user’s name or email address. However, the user must explicitly grant permission for these actions by accepting the security dialog that appears when the applet is first run.

What is the use of the showDocument method in an applet?

The showDocument() method is a built-in method in Java applets that is used to display a new web page or document in the user’s browser. This method allows the applet to communicate with the web browser and navigate to a new web page or resource.

The showDocument() method takes a single argument, which is a URL object that specifies the location of the new document. The method opens the URL in a new window or tab, depending on the user’s browser settings.

Here is an example of using the showDocument() method to navigate to a new web page:

import java.applet.*;
import java.awt.*;
import java.net.*;

public class MyApplet extends Applet {
   public void init() {
      // Create a button to navigate to a new web page
      Button button = new Button("Go to Google");
      add(button);
   }

   public void actionPerformed(ActionEvent event) {
      // When the button is clicked, navigate to the Google homepage
      try {
         URL url = new URL("http://www.google.com");
         getAppletContext().showDocument(url, "_blank");
      } catch (MalformedURLException e) {
         e.printStackTrace();
      }
   }
}

In this example, the applet creates a button that, when clicked, navigates to the Google homepage using the showDocument() method. The getAppletContext() method is used to get the applet’s context, which is used to display the new document in the user’s browser. The second argument to the showDocument() method is the target window for the new document. In this case, we specify "_blank" to open the new document in a new window or tab.

What is the use of the getCodeBase and getDocumentBase method in an applet?

The getCodeBase() and getDocumentBase() methods are built-in methods in Java applets that are used to obtain the URL of the directory containing the applet and the URL of the HTML document that contains the applet, respectively.

Here is a brief explanation of each method:

  • getCodeBase(): This method returns a URL object that represents the base directory for the applet. This directory contains the class files and any other resources required by the applet. This method is typically used to load resources such as images or other applets that are located in the same directory as the applet. The URL object returned by this method ends with a forward slash (/) to indicate that it is a directory.

  • getDocumentBase(): This method returns a URL object that represents the directory containing the HTML document that contains the applet. This method is typically used to reference resources that are located in the same directory as the HTML file that contains the applet. The URL object returned by this method ends with the name of the HTML file that contains the applet.

Here is an example of using these methods to load an image from the same directory as the applet:

import java.applet.*;
import java.awt.*;
import java.net.*;

public class MyApplet extends Applet {
   private Image image;

   public void init() {
      // Load the image using the applet's code base
      try {
         URL imageURL = new URL(getCodeBase(), "myimage.jpg");
         image = getImage(imageURL);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      }
   }

   public void paint(Graphics g) {
      // Draw the image on the applet
      g.drawImage(image, 0, 0, this);
   }
}

In this example, the init() method of the applet uses the getCodeBase() method to obtain the URL of the directory containing the applet, and then constructs a URL for the image file "myimage.jpg" in that directory. The getImage() method is then used to load the image into memory. Finally, the paint() method of the applet draws the image on the applet using the drawImage() method of the Graphics object.

Note that the getDocumentBase() method could be used in a similar way to load resources that are located in the same directory as the HTML file that contains the applet.

What is the use of the resize method in an applet?

The resize() method in an applet is used to resize the applet window. It is an inherited method from the java.awt.Component class and can be overridden in the applet to specify the desired size for the applet.

The resize() method takes two parameters: the width and height of the applet window in pixels. When called, it resizes the applet window to the specified dimensions. If the new size is larger than the original size, the applet window is expanded to the new size, and the newly exposed area is filled with the applet’s background color. If the new size is smaller than the original size, the applet window is shrunk to the new size, and any content that falls outside the new window size is clipped.

It’s important to note that the resize() method is not always reliable for resizing applets, especially when the applet is embedded in a web page. This is because the size of the applet window is ultimately controlled by the web browser, and different browsers may handle resizing differently. In addition, some browsers may restrict the size of the applet window for security reasons. As a result, it’s generally best to design applets that can adapt to different window sizes rather than relying on the resize() method to control the applet’s size.

Questions on Chapter 21

Questions on Chapter 21

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories