Hello, World!

Below is the output of the traditional "hello world" application written in Pivot:

This version of the application was written entirely in Java and is shown below; a WTKX implementation is demonstrated next:

package org.apache.pivot.tutorials;

import java.awt.Color;
import java.awt.Font;

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.HorizontalAlignment;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.VerticalAlignment;
import org.apache.pivot.wtk.Window;

public class HelloJava implements Application {
    private Window window = null;

    public void startup(Display display, Map properties) {
        Label label = new Label();
        label.setText("Hello World!");
        label.getStyles().put("font", new Font("Arial", Font.BOLD, 24));
        label.getStyles().put("color", Color.RED);
        label.getStyles().put("horizontalAlignment",
            HorizontalAlignment.CENTER);
        label.getStyles().put("verticalAlignment",
            VerticalAlignment.CENTER);

        window = new Window();
        window.setContent(label);
        window.setTitle("Hello World!");
        window.setMaximized(true);
        window.open(display);
    }

    public boolean shutdown(boolean optional) {
        if (window != null) {
            window.close();
        }

        return false;
    }

    public void suspend() {
    }

    public void resume() {
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(HelloJava.class, args);
    }
}

The program demonstrates some of the fundamental features of the Pivot platform: the Application interface, the Window class, and styles.

The Application Interface

The Application interface is the entry point into a Pivot application. It is similar to the main() method used in C and Java programming or the lifecycle methods used in traditional Java applet development. It defines the following four methods:

However, unlike main() or the applet lifecycle methods, which require a separate code base for each environment, Application defines a single interface that is used for both web deployment or desktop execution, allowing the same program to run unmodified in either environment.

A Pivot application can be run in the browser using the <applet> tag, as shown below; the class name of the Pivot application is specified by the "applicationClassName" applet parameter:

<applet code="org.apache.pivot.wtk.BrowserApplicationContext$HostApplet"
    archive="lib/pivot-core-@version@.jar,lib/pivot-wtk-@version@.jar,
    lib/pivot-wtk-@version@.terra.jar, lib/pivot-tutorials-@version@.jar"
    width="160" height="80">
    <param name="applicationClassName"
        value="org.apache.pivot.tutorials.HelloJava">
    <param name="image" value="">
</applet>

The same application can be run from the command line using the following syntax (minus the line breaks) on UNIX-based systems; the class name is the first argument to the DesktopApplicationContext loader application:

java -cp pivot-core-@version@.jar:pivot-wtk-@version@.jar:pivot-wtk-@version@.terra.jar:pivot-tutorials-@version@.jar org.apache.pivot.wtk.DesktopApplicationContext org.apache.pivot.tutorials.HelloJava

and the following on Windows systems:

java -cp pivot-core-@version@.jar;pivot-wtk-@version@.jar;pivot-wtk-@version@.terra.jar;pivot-tutorials-@version@.jar org.apache.pivot.wtk.DesktopApplicationContext org.apache.pivot.tutorials.HelloJava

The application itself defines a convenience main() method that delegates to DesktopApplicationContext.main(); this allows Pivot applications to be bundled as executable JARs and makes launching applications from within an IDE easier.

The Window Class

A window is the top-level entry point into an application's user interface. Almost all Pivot applications will use at least one window.

The window in the sample application is an instance of Window, the most basic window type. It is simply an undecorated area of the screen into which other components may be placed. Other window types, such as Dialog and Frame, add additional features and behaviors such as title bars and modality.

The window used by "Hello World" is "maximized": it automatically fills the entire area of the display. A maximized, decorationless window is commonly used as a top-level application window, particularly for applications that will be primarily run in a web browser. However, windows can also be given an explicit size, can be resized by the user, or can take on the default size of their content. Windows are discussed in more detail in the Windows section.

Styles

Styles are a means of customizing a component's appearance. Style properties are defined by a component's skin and are accessed via a component's styles collection. For example, the example application sets the font, color, and alignment styles on the "Hello World" label. Though skins are not required to provide styling support, most will provide similar capabilities.

Next: Hello, WTKX!