Wainuiomata.com

Java: Creating an Applet / Application hybrid

Location: Forum - Programming - Java: Creating an Applet / Application hybrid

New Comment

  1. Author: robvdl
    User type: Administrator
    Posts: 130
    Date: July 1, 2006, 7:39 p.m.

    Here is a neat, yet very simple method I have discovered, in which you can create a Java Applet, which can also be run as an Application. Should this be called an Appletcation? :-) Anyway, Swing is required for this method to work.

    I was going to show this method to a friend the other day, who is also into Java programming, but I though why not throw it up in the Programming section in the Ynui Forums, for other Java programmers to learn from.

    The trick is understanding how Java Applets and Applications start, as they start quite different to each other.

    Applets:

    Applets must be derived from the Applet class, or the JApplet class. The JApplet class is provided by the Swing API and allows you to insert Swing controls. The JApplet class is actually derived from the Applet class itself. When Applets start, the browser creates an instance of the main class, and calls the init() method, then followed by the start() method. The applet actually remains resident. If the web page that contains the Applet is reloaded, the stop() method is called, but the Applet is not unloaded. On a restart, the init() method will not be called again, only the start() method is called this time.

    Applications:

    When a Java application starts, an instance of the main class is NOT created, instead it statically calls the main() method similar to a C program, hence the main method in a Java application must always be declared as static. Usually the Java application itself would then create an instance of a class derived from JFrame (any class, it could be the main class or another class) to open the first window.

    Combining the two together:

    Once you understand that both start quite different to each other, it is actually really easy to combine them together. All you really need to do is derive your main class from JApplet and provide the start() and init() methods required by the Applet version. The next step is to provide a static main() method, which the Application version will use. We are going to place all the contents of our GUI application in a new class, derived from the JPanel class. When the Applet version starts and the start() method is invoked, we simply insert the JPanel with the contents. When the Application version starts and the main() method is statically called, we create a new class, derived from JFrame, and insert the JPanel with the contents into that frame. Confusing? Let's just get straight to the code, it should self explanatory if you know Java.

    Appletcation.java


    import java.applet.Applet;
    import java.awt.*;
    import javax.swing.*;

    public class Appletcation extends JApplet
    {
    private AppPanel appPanel;

    public void init()
    {
    setSystemTheme();
    }

    public void start()
    {
    setJMenuBar(new AppMenuBar()); // Optional menu bar, but cannot be in JPanel
    appPanel = new AppPanel();
    getContentPane().add(appPanel);
    }

    public static void main(String[] args)
    {
    setSystemTheme();
    AppFrame appFrame = new AppFrame();
    }

    private static void setSystemTheme()
    {
    String theme = UIManager.getSystemLookAndFeelClassName();
    try
    {
    UIManager.setLookAndFeel(theme);
    }
    catch (UnsupportedLookAndFeelException e)
    {
    System.err.println("Warning: UnsupportedLookAndFeel: " + theme);
    }
    catch (Exception e)
    {
    System.err.println("Error loading " + theme + ": " + e);
    }
    }
    }


    AppMenuBar.java


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class AppMenuBar extends JMenuBar implements ActionListener
    {
    private JMenu fileMenu;
    private JCheckBoxMenuItem fileCheck;

    public AppMenuBar()
    {
    fileMenu = new JMenu("File");
    fileCheck = new JCheckBoxMenuItem("Check Me");
    fileMenu.add(fileCheck);
    add(fileMenu);
    }

    public void actionPerformed(ActionEvent e)
    {
    }
    }


    AppFrame.java


    import java.awt.*;
    import javax.swing.*;

    public class AppFrame extends JFrame
    {
    private AppPanel appPanel;

    public AppFrame()
    {
    super("Appletcation Demo!");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(new AppMenuBar()); // Optional menu bar, but cannot be in JPanel
    appPanel = new AppPanel();
    getContentPane().add(appPanel, BorderLayout.CENTER);
    Dimension dim = getToolkit().getScreenSize();
    // Set size to 80% x 80% of screen and center window
    setSize((dim.width / 100) * 80, (dim.height / 100) * 80);
    Rectangle bounds = getBounds();
    setLocation((dim.width - bounds.width) / 2, (dim.height - bounds.height) / 2);
    // Maximize window (Maximize through code currently not supported on Linux)
    setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
    setVisible(true);
    }
    }


    AppPanel.java


    import java.awt.*;
    import javax.swing.*;

    public class AppPanel extends JPanel
    {
    public AppPanel()
    {
    setLayout(new BorderLayout());
    }
    }


    Compile the Appletcation, and package it as a JAR. You should now be able to run the JAR as a standard Application. To run it as an Applet, use the following HTML file to run as a full page Applet! The HTML file has been tested to work with IE, Firefox and Opera without problems, however the HTML 4 Transitional DTD must remain.

    index.html


    <!DOCTYPE html public "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>

    <head>
    <title>Appletcation Demo!</title>
    <style type="text/css">
    html, body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    }
    </style>
    </head>

    <body>
    <applet code="Appletcation" archive="Appletcation.jar" width="100%" height="100%"></applet>
    </body>

    </html>


    Conclusion:

    I've used this method for a while and it works well. I don't know if it has been done before, but cannot see any reason why there should be any problems using this method. You can use this method in your own productions if you like, however there are no warranties with the code... pretty much the usual. ;-)

    Profile

  2. Author: Mega Byte
    User type: Standard User
    Posts: 17
    Date: July 2, 2006, 10:04 p.m.

    HMMM JAVA I LIKE JAVA COFFEE IS NICE TOO LOL JOKES :)

    WHAT IS A GOOD SITE FOR JAVA APLETS?

    Profile

  3. Author: robvdl
    User type: Administrator
    Posts: 130
    Date: July 3, 2006, 12:47 p.m.

    Hi, you're wondering about a good site for Java applets. Did you mean in source code form to learn from, or just cool applets to check out?

    If you or anyone else is interested in seeing what Java can do, there are some cool applets over at, Mandrixx.net, the guy is very good at doing Java effects and stuff.

    http://www.mandrixx.net/

    As for learning Java, probably the best is Google, maybe checkout the developer section at Sun.com, the makers of Java. I sort of learned myself by reading a book "Java 2, J2SE 1.4 Complete", $39.99 at Whitcoulls, which covers practically every aspect of Java programming and is aimed at Intermediate/Beginner level. The book is more a reference though, but since I had previous programming knowledge in other languages (I see you have experience in GML, which is awesome), I did pick it up very quick from reading that book. I'm not too sure about free online tutorials, but I'm sure there will be some available, check Google maybe.

    Profile

  4. Author: robvdl
    User type: Administrator
    Posts: 130
    Date: July 6, 2006, 5 p.m.

    Check out this Java Demo:

    http://www.mandrixx.net/sparks.php

    Cool as, shows you what Java is really capable of, awesome effects and it's all done without using a 3D card. I found the music a bit cheesey in places, but the demo itself was cool.

    You can watch the demo directly in your browser, don't need to download it. That is considering you already have Java installed off course. :mrgreen:

    Profile