Libraries
Library functions are imported
External Java classes, fields and methods are imported into the HolonJ program. Define a local word, which represents the class, field or method. You can then address the item locally.
importClass: List java.awt.List
imports class java.awt.List, which is now accessible as the local word List.
importMethod: AddList List add ( string -- )
imports the method add from the class java.awt.List. The class is indicated here by its local name List, therefore the class must be imported first.
static importField: InStream System in InputStream
The word InStream represents the variable in of type InputStream in the class java.lang.System.
Collection of imports
This macro view presents the collection of Java imports that have accumulated to date. You probably find the Java functions that you need here. Otherwise just add them to the library.
Typical uses
Frame
The frame is a window that can be moved and resized, but has no actions yet. For example the close button is not active.
This class defines a close action, which simply ends the program.
class: wAction extends WindowAdapter
i: wa.<init> ( -- ) initsuper () ;
i: wa.windowClosing ( windowevent e -- )
0 getRuntime runExit ;
Here is a frame for a game of TicTacToe.
class: TicTacToe extends Frame
i: ttt.<init> ( -- )
" TicTacToe" initsuper ( string -- )
300 300 this setSize
this showWindow
new wAction () this addWindowListener ;
Graphics
The display part of the frame is its Graphics context.
Graphics myGraphics
: PaintImage ( -- )
" image.gif" getImageFile
10 10 this this getGraphics drawImage ;
x1 y1 x2 y2 myGraphics drawLine
Color myColor
green red blue new myColor ( int int int -- )
Font myFont
" font" style size new myFont ( string int int -- )
font = TimesRoman etc, style = bold, italic, plain
myFont myGraphics setFont
myColor myGraphics setColor
Threads
1. Clock as a subclass of Thread.
class: Clock extends Thread
\ The thread action, overwrites run
i: Clock.run ( -- )
begin this repaint 1000. sleep again ;
: Clock.main ( string [] args -- )
clock myClock new myClock ()
myClock startThread ;
2. Clock applet implementing Runnable.
class: Clock extends Applet implements Runnable
Thread myThread
\ The thread action, overwrites run
i: Clock.run ( -- )
begin this repaint 1000. sleep again ;
\ starting the thread
i: Clock.start ( -- )
myThread null? if this new myThread ( runnable -- ) then
myThread startThread ;
Output to console
." This text is printed" \ prints the text
" Text string" type \ prints a string
55 . \ prints a number
7 ##. \ prints a leading zero if the number is only one digit
Strings
" string1" " string2" "+
" test string" "= if ...
Stringbuf Text \ a text buffer
0 Text setLength \ empty the buffer
( char -- ) Text caddBuf \ add char to the buffer
Text stringOf \ convert the buffer contents to a string
Streams
InputStreams deliver bytes, Readers provide characters. HolonJ gets bytes and reads characters.
importMethod: getByte InputStream read ( -- int )
Files
importClass: File java.io.File
importMethod: exists File exists ( -- boolean )
: filetest ( string name -- )
file theFile name new theFile ( string -- )
theFile exists if ." File exists" else ." doesn't exist" then ;
Stringbuf Text
FileInputstream Input
i: getText ( file myFile -- )
myFile new Input ( file -- ) 0 Text setLength
begin Input getByte dup -1 <> while Text caddBuf drop repeat ;
Date
\ prints current date and time
: printDate ( -- )
new Date () toString type ;
Events
importMethod: addButtonListener
Button addActionListener ( ActionListener -- )
i: addButton ( string btext -- )
btext new Button ( string -- )
dup Controlpanel addComp
this swap addButtonListener ;
next
|