Applications and Applets
You can create Java applications and applets in HolonJ. Here are some observations.
An application is a class with a main method.
class: Hello
: main ( String [] args -- )
." hello world" ;
If an object of the class is created, the method <init> is called for initializing. At the very least <init> initializes the super object.
class: MyApp extends Frame
i: MyApp.<init> ( -- )
" My window" initsuper () ;: main ( String [] args -- )
MyApp m new m ()
300 300 m setSize
m showWindow ;
If an identifier contains a dot, only the part of the identifier following the dot is used in the class file. Thus HolonJ sees "MyApp.<init>", Java sees "<init>".
An applet is a class that extends the class Applet and runs in a browser. The browser loads the applet, creates an object and calls the init method of the applet.
class: MyApplet extends Applet
i: MyApplet.<init> ( -- )
initsuper () ;i: MyApplet.init ( -- ) ;
class: MyApplet extends Applet implements Runnable
Thread Clock
i: MyApplet.start ( -- )
this new Thread ( runnable -- ) is Clock
Clock startThread ;i: MyApplet.paint ( Graphics g -- )
TimeString 40 100 g drawString ;i: MyApplet.run ( -- )
begin this repaint 1000. sleep again ;i: MyApplet.stop ( -- )
Clock stopThread ;
class: MyApplet extends Applet implements MouseListerner
\ callback function
i: paint ( Graphics g -- ) ;i: mouseReleased ( mouseEvent e -- ) ;
i: mousePressed ( mouseEvent e -- ) ;
i: mouseClicked ( mouseEvent e -- ) ;
i: mouseEntered ( mouseEvent e -- ) ;
i: mouseExited ( mouseEvent e -- ) ;
The applet can run as an application, if we provide a context with a window. The java interpreter does not provide a full applet context (e.g. no sound), but it serves well for the initial tests.
i: InFrame ( -- )
Frame f " Testing my applet in a frame" new f ( string -- )
this f addComponent drop
this this addMouselistener
300 300 f setSize f showWindow ;: main ( string [] args -- )
MyApplet m new m ()
m InFrame ;
The window close button is not active. Close the window with Ctrl+C in the console window.