Mini Appletviewer : Applet « Development « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Development » Applet 
6. 28. 8. Mini Appletviewer
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.JFrame;

public class MinAppletviewer {
  public static void main(String args[]) throws Exception {
    AppSupport theAppSupport = new AppSupport();
    JFrame f = new JFrame();
    URL toload = new URL(args[0]);
    String host = toload.getHost();
    int port = toload.getPort();
    String protocol = toload.getProtocol();
    String path = toload.getFile();
    int join = path.lastIndexOf('/');
    String file = path.substring(join + 1);
    path = path.substring(0, join + 1);

    theAppSupport.setCodeBase(new URL(protocol, host, port, path));
    theAppSupport.setDocumentBase(theAppSupport.getCodeBase());

    URL[] bases = theAppSupport.getCodeBase() };
    URLClassLoader loader = new URLClassLoader(bases);
    Class theAppletClass = loader.loadClass(file);
    Applet theApplet = (Applet) (theAppletClass.newInstance());

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.add(theApplet, BorderLayout.CENTER);

    theApplet.setStub(theAppSupport);

    f.setSize(200200);
    f.setVisible(true);
    theApplet.init();
    theApplet.start();
  }
}

class AppSupport implements AppletStub, AppletContext {
  private URL codeBase;

  private URL documentBase;

  public AppSupport() throws Exception {
    URL url = null;
    String urlpart = System.getProperty("user.dir");
    urlpart = urlpart.replace(File.separatorChar, '/');
    url = new URL("file://" + urlpart + "/");
    codeBase = documentBase = url;
  }

  public void appletResize(int w, int h) {
  }

  public AppletContext getAppletContext() {
    return (AppletContextthis;
  }

  public URL getDocumentBase() {
    return documentBase;
  }

  public void setDocumentBase(URL url) {
    documentBase = url;
  }

  public URL getCodeBase() {
    return codeBase;
  }

  public void setCodeBase(URL url) {
    codeBase = url;
  }

  public String getParameter(String s) {
    return null;
  }

  public boolean isActive() {
    return true;
  }

  public Applet getApplet(String name) {
    return null;
  }

  public Enumeration getApplets() {
    return new Enumeration() {
      public boolean hasMoreElements() {
        return false;
      }

      public Object nextElement() {
        return null;
      }
    };
  }

  public AudioClip getAudioClip(URL url) {
    return Applet.newAudioClip(url);
  }

  public Image getImage(URL url) {
    return Toolkit.getDefaultToolkit().getImage(url);
  }

  public void showDocument(URL url) {
  }

  public void showDocument(URL url, String target) {
  }

  public void showStatus(String status) {
    System.err.println(status);
  }

  public void setStream(String key, InputStream streamthrows IOException {
  }

  public InputStream getStream(String key) {
    return null;
  }

  public Iterator<String> getStreamKeys() {
    return null;
  }
}
6. 28. Applet
6. 28. 1. Applet
6. 28. 2. A real applet
6. 28. 3. Add JButton to JApplet
6. 28. 4. Access applet parameters
6. 28. 5. An Swing-based applet skeleton
6. 28. 6. Swing Applet
6. 28. 7. A simple Swing-based applet
6. 28. 8. Mini Appletviewer
6. 28. 9. Tic Tac Toe game
6. 28. 10. WeatherWizard JApplet
6. 28. 11. Change an applet background color
6. 28. 12. Read an applet parameters
6. 28. 13. Calling methods of an Applet from JavaScript code
6. 28. 14. Passing Parameters to Java Applet
6. 28. 15. Display message in browser status bar
6. 28. 16. System properties accessible to applets
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.