Join Regular Classroom : Visit ClassroomTech

JAVA – codewindow.in

Related Topics

JAVA Programming

import java.applet.Applet;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

public class MyFirstApplet extends Applet implements ActionListener {
    private Button button;

    public void init() {
        button = new Button("Go to Google");
        button.addActionListener(this);
        add(button);
    }

    public void actionPerformed(ActionEvent e) {
        String url = "https://www.google.com/";
        try {
            AppletContext context = getAppletContext();
            URL urlObj = new URL(url);
            context.showDocument(urlObj);
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }
}

In this example, the applet creates a button that, when clicked, navigates to the Google website using the showDocument method. The getAppletContext method is used to obtain a reference to the AppletContext object, which is then used to call the showDocument method with the URL of the Google website.

When the user clicks the button, the actionPerformed method is called, which constructs a URL object from the String URL of the Google website. This URL object is then passed as a parameter to the showDocument method of the AppletContext object, which displays the Google website in the user’s web browser.

// In the first Applet
public void sendDataToOtherApplet() {
    Applet otherApplet = getAppletContext().getApplet("secondApplet");
    if (otherApplet != null && otherApplet instanceof SecondApplet) {
        ((SecondApplet) otherApplet).receiveDataFromOtherApplet(data);
    }
}

// In the second Applet
public void receiveDataFromOtherApplet(Object data) {
    // process the data received from the first Applet
}

In this example, the sendDataToOtherApplet() method in the first Applet uses the getApplet() method to get a reference to the second Applet by its name, “secondApplet”. If the Applet is found and is an instance of the SecondApplet class, it calls the receiveDataFromOtherApplet() method in the second Applet to pass the data.

The getAppletContext() method is used to obtain a reference to the AppletContext object associated with the current Applet. The AppletContext provides a way to access and control other Applets on the same page.

 

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

public class MyApplet extends Applet implements AppletStub {
    
    private String parameter1;
    private String parameter2;
    
    public void init() {
        // Get the value of parameter1 from the HTML page
        parameter1 = getParameter("parameter1");
        // Get the value of parameter2 from the HTML page
        parameter2 = getParameter("parameter2");
        // Set the layout manager for the applet
        setLayout(new BorderLayout());
        // Add components to the applet
        add(new Label("Parameter 1: " + parameter1), BorderLayout.NORTH);
        add(new Label("Parameter 2: " + parameter2), BorderLayout.CENTER);
    }
    
    public URL getDocumentBase() {
        // Return the URL of the HTML page containing the applet
        return getCodeBase();
    }
    
    public String getParameter(String name) {
        // Return the value of the specified parameter from the HTML page
        String value = super.getParameter(name);
        if (value == null) {
            value = "";
        }
        return value;
    }
    
    public boolean isActive() {
        // Return true if the applet is currently active, false otherwise
        return true;
    }
}

In this example, we implement the AppletStub interface in the MyApplet class. The init method of the MyApplet class retrieves the values of parameter1 and parameter2 from the HTML page using the getParameter method. The getDocumentBase method returns the URL of the HTML page containing the applet, and the isActive method returns true since the applet is always active.

By implementing the AppletStub interface, the MyApplet class can provide additional runtime information to the applet, such as the URL of the HTML page and the values of any parameters passed to the applet.

import java.applet.Applet;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;

public class MyCustomApplet extends Applet implements AppletInitialPermission {

    public void init() {
        // Initialize the Applet
    }

    public PermissionCollection getPermissions() {
        Permissions permissions = new Permissions();
        permissions.add(new java.net.SocketPermission("localhost:8080", "connect"));
        permissions.add(new java.io.FilePermission("/tmp/*", "read"));
        return permissions;
    }
}

In this example, the getPermissions() method is overridden to grant the Applet permissions to access the localhost server on port 8080 and to read files from the /tmp directory.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories