Archive for the ‘Fantom’ Category

  1. Hello, Java 3D(Fantom) World!

    Posted on 12月 30th, 2012 by cx20

    Java 3D

    Java 3D は Java による 3D グラフィックライブラリである。
    以下は Fantom による Java 3D の使用例となっている。

    ソースコード

    using [java] java.awt
    using [java] javax.swing
    using [java] javax.media.j3d
    using [java] javax.vecmath
    using [java] com.sun.j3d.utils.universe
     
    using concurrent
     
    class Hello
    {
        Void main()
        {
            frame := JFrame("Hello, World")
            {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
                setSize(640, 480)
            }
            canvas3D := createCanvas3D(frame)
            scene := createSceneGraph()
            connect(canvas3D, scene)
            frame.setVisible(true)
     
            Actor.sleep(Duration.maxVal)
        }
        Canvas3D createCanvas3D( JFrame frame )
        {
            frame.getContentPane().setLayout( BorderLayout() )
            config := SimpleUniverse.getPreferredConfiguration()
            canvas3D := Canvas3D( config )
            frame.getContentPane().add( canvas3D )
            return canvas3D
        }
     
        BranchGroup createSceneGraph()
        {
            objRoot := BranchGroup()
            mover := moveTextBack()
            spinner := createSpinner()
            rotator := RotationInterpolator(Alpha(-1, 4000), spinner, Transform3D(), 0.0f, 3.14159265f * 2)
            rotator.setSchedulingBounds( BoundingSphere() )
            spinner.addChild(rotator)
            objRoot.addChild(mover)
            mover.addChild(spinner)
            spinner.addChild( createTextShape())
            setLighting(mover)
            return objRoot
        }
     
        TransformGroup createSpinner()
        {
            spinner := TransformGroup()
            spinner.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE)
            return spinner
        }
     
        TransformGroup moveTextBack()
        {
            transform3D := Transform3D()
            transform3D.setTranslation( Vector3f(0.0f, 0.0f, -10.0f))
            return TransformGroup(transform3D)
        }
     
        Shape3D createTextShape()
        {
            textAppear := Appearance()
            textAppear.setMaterial(Material())
            font3D := Font3D( Font("MS Pゴシック", Font.PLAIN, 1), FontExtrusion())
            textGeom := Text3D(font3D, "Hello, Java 3D World!")
            textGeom.setAlignment(Text3D.ALIGN_CENTER)
            textShape := Shape3D()
            textShape.setGeometry(textGeom)
            textShape.setAppearance(textAppear)
            return textShape
        }
     
        Void setLighting( TransformGroup objMove )
        {
            light := DirectionalLight()
            light.setInfluencingBounds(BoundingSphere())
            light.setDirection(Vector3f(0.0f,0.0f,-1.0f))
            light.setColor(Color3f(1.0f, 1.0f, 1.0f))
            objMove.addChild(light)
        }
     
        Void connect( Canvas3D canvas3D, BranchGroup scene )
        {
            universe := SimpleUniverse(canvas3D)
            universe.getViewingPlatform().setNominalViewingTransform()
            universe.addBranchGraph(scene)
        }
    }

    実行方法

    C:¥> fan Hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 3D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, Java 2D(Fantom) World!

    Posted on 12月 29th, 2012 by cx20

    Java 2D

    Java 2D は Java による 2D グラフィックライブラリである。
    以下は Fantom による Java 2D の使用例となっている。

    ソースコード

    using [java] java.awt
    using [java] javax.swing
    using concurrent
     
    class Hello
    {
        Void main()
        {
            canvas := Canvas()
            frame := JFrame("Hello, World")
            {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
                add(canvas)
                setSize(640, 480)
                setVisible(true)
                canvas.createBufferStrategy(2)
                strategy := canvas.getBufferStrategy
                g2 := (Graphics2D)strategy.getDrawGraphics
                while(true)
                {
                    g2.drawString("Hello, Java 2D World!", 0, 16 )
                    strategy.show
                    Actor.sleep(200ms)
                }
            }
            Actor.sleep(Duration.maxVal)
        }
    }

    実行方法

    C:¥> fan hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 2D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, JavaFX(Fantom) World!

    Posted on 12月 28th, 2012 by cx20

    JavaFX(Jython)

    JavaFX は Java による RIA 向けの GUI ライブラリならびにプラットフォームである。
    類似の RIA プラットフォームとしては Adobe Flex や Microsoft Silverlight などがある。
    以下は Fantom による JavaFX の使用例となっている。

    ディレクトリ構成

    %FANTOM_HOME%
        /lib
            /java
               /ext         … ライブラリ配置場所
     
    1. %FANTOM_HOME%libjavaetc にライブラリ配置
       jfxrt.jar
     
    2. パスの確認
       C:¥> fan compilerJava::ClassPath
     
       <実行結果>
       --- ClassPath ---
       Packages Found:
              :
         com.sun.javafx [15]
              :
       ClassPath Files:
              :
         file:/%FANTOM_HOME%/lib/java/ext/jfxrt.jar

    ソースコード

    using [java] javafx.application::Application
    using [java] javafx.stage::Stage
    using [java] javafx.scene::Scene
    using [java] javafx.scene.layout::HBox
    using [java] javafx.scene.control::Label
     
    class Hello : Application {
        Void main(Str[] args) {
            Application.launch(Hello#->toClass, args)
        }
     
        override Void start(Stage? stage) {
            hbox := HBox()
            scene := Scene(hbox, 640.0f, 480.0f)
            label := Label("Hello, JavaFX World!")
            hbox.getChildren().add( label  )
     
            stage.setScene(scene)
            stage.setTitle("Hello, World")
            stage.show()
        }
    }

    実行方法

    C:¥> fan Hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, JavaFX World!                      |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. Hello, SWT(Fantom) World!

    Posted on 12月 27th, 2012 by cx20

    SWT(Fantom)

    SWT(Standard Widget Toolkit) は Java で GUI を扱うためのライブラリである。
    IBM により AWT や Swing を置き換える目的で作成された。
    以下は Fantom による SWT の使用例となっている。

    ソースコード

    using [java] org.eclipse.swt
    using [java] org.eclipse.swt.widgets::Display
    using [java] org.eclipse.swt.widgets::Shell
    using [java] org.eclipse.swt.widgets::Label
    using [java] org.eclipse.swt.layout::FillLayout
    using concurrent
     
    class Hello
    {
        Void main()
        {
            display := Display()
            shell := Shell(display)
            shell.setText("Hello, World")
     
            layout := FillLayout(SWT.VERTICAL)
            shell.setLayout(layout)
     
            label := Label(shell,SWT.BORDER)
            label.setText("Hello, SWT World!")
     
            shell.setSize( 640, 480 )
            shell.open()
     
            while (!shell.isDisposed())
            {
                if (!display.readAndDispatch())
                {
                    display.sleep()
                }
            }
     
            display.dispose()
        }
    }

    実行方法

    C:¥> fan hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  5. Hello, Swing(Fantom) World!

    Posted on 12月 26th, 2012 by cx20

    Swing(Fantom)

    Swing は AWT(Abstract Window Toolkit) を拡張したものであり Java で GUI を扱うためのライブラリである。
    J2SE 1.2 以降は AWT よりも Swing が使われることが多くなっている。
    以下は Fantom による Swing の使用例となっている。

    ソースコード

    using [java] javax.swing
    using [java] java.awt.event
    using concurrent
     
    class Hello
    {
        Void main()
        {
            label := JLabel("Hello, Swing World!")
            {
                setVerticalAlignment(JLabel.TOP)
            }
     
            frame := JFrame("Hello, World")
            {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
                getContentPane.add(label)
                setSize(640, 480)
                add(label)
                setVisible(true)
            }
     
            Actor.sleep(Duration.maxVal)
        }
    }

    実行方法

    C:¥> fan Hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Swing World!                       |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, AWT(Fantom) World!

    Posted on 12月 25th, 2012 by cx20

    AWT(Fantom)

    AWT(Abstract Window Toolkit) は Java で GUI を扱うためのライブラリである。
    J2SE 1.2 以降は AWT を拡張した Swing が使われることが多くなっている。
    以下は Fantom による AWT の使用例となっている。

    ソースコード

    using [java] java.lang
    using [java] java.awt
    using [java] java.awt.event
    using concurrent
     
    class Hello
    {
        Void main()
        {
            label := Label("Hello, AWT World!")
            frame := Frame("Hello, World")
            {
                addWindowListener( HelloWindowAdapter.make|WindowEvent e| {})
                add(label)
                setSize(640, 480)
                setLayout(FlowLayout(FlowLayout.LEFT))
                setVisible(true)
            }
     
            Actor.sleep(Duration.maxVal)
        }
    }
     
    class HelloWindowAdapter : WindowAdapter
    {
        new make(|WindowEvent| cb) { this.cb = cb }
        override Void windowClosing(WindowEvent? e) { System.exit(0) }
        |WindowEvent| cb
    }

    実行方法

    C:¥> fan hello.fan

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, AWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, JDBC Type4(Fantom) World!

    Posted on 10月 27th, 2012 by cx20

    JDBC Type4

    JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
    Type4 は DBMS のクライアントライブラリを使用しない Pure Java の DBMS ドライバである。
    以下は Fantom による JDBC ライブラリの使用例となっている。

    ディレクトリ構成

    %FANTOM_HOME%
        /etc
            /sql            … DB ライブラリ設定ファイル配置場所
        /lib
            /java
               /ext         … ライブラリ配置場所
     
    1. %FANTOM_HOME%etcsql に DB ライブラリ設定ファイルを配置
       config.props
     
    2. %FANTOM_HOME%libjavaetc にライブラリ配置
       sqljdbc4.jar
       ojdbc6.jar
       mysql-connector-java-5.1.22-bin.jar

    ソースコード(Fantom + JDBC Type4 + SQL Server)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:sqlserver://;serverName=localhost", "sa", "P@ssW0rd")
            stmt := db.sql("SELECT 'Hello, JDBC Type4 World!' AS Message").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    設定ファイル(Fantom + JDBC Type4 + SQL Server)

    java.drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver

    ソースコード(Fantom + JDBC Type4 + Oracle)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger")
            stmt := db.sql("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    設定ファイル(Fantom + JDBC Type4 + Oracle)

    java.drivers=oracle.jdbc.driver.OracleDriver

    ソースコード(Fantom + JDBC Type4 + MySQL)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:mysql://localhost:3306", "root", "P@ssW0rd")
            stmt := db.sql("SELECT 'Hello, JDBC Type4 World!' AS Message").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    設定ファイル(Fantom + JDBC Type4 + MySQL)

    java.drivers=com.mysql.jdbc.Driver

    実行方法

    C:¥> fan hello.fan

    実行結果

    Hello, JDBC Type4 World!
  8. Hello, JDBC Type2(Fantom) World!

    Posted on 10月 26th, 2012 by cx20

    JDBC Type2

    JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
    Type2 は JDBC と DBMS クライアントの API をマップさせたブリッジドライバである。
    例えば、Oracle であればクライアントライブラリとして OCI が使用される。
    以下は Fantom による JDBC ライブラリの使用例となっている。

    ディレクトリ構成

    %FANTOM_HOME%
        /etc
            /sql            … DB ライブラリ設定ファイル配置場所
        /lib
            /java
               /ext         … ライブラリ配置場所
     
    1. %FANTOM_HOME%etcsql に DB ライブラリ設定ファイルを配置
       config.props
     
    2. %FANTOM_HOME%libjavaetc にライブラリ配置
       ojdbc6.jar

    ソースコード(Fantom + JDBC Type2 + Oracle)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:oracle:oci:@orcl", "scott", "tiger")
            stmt := db.sql("SELECT 'Hello, JDBC Type2 World!' AS Message FROM DUAL").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    設定ファイル(Fantom + JDBC Type2 + Oracle)

    java.drivers=oracle.jdbc.driver.OracleDriver

    実行方法

    C:¥> fan hello.fan

    実行結果

    Hello, JDBC Type2 World!
  9. Hello, JDBC Type1(Fantom) World!

    Posted on 10月 25th, 2012 by cx20

    JDBC Type1

    JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
    Type1 は JDBC と ODBC の API をマップさせたブリッジドライバである。
    以下は Fantom による JDBC ライブラリの使用例となっている。

    ソースコード(Fantom + JDBC Type1 + ODBC + Jet データベース)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=.\hello.mdb", "", "")
            stmt := db.sql("SELECT 'Hello, JDBC Type1 World!' AS Message").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    ソースコード(Fantom + JDBC Type1 + ODBC + ACE データベース)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=.\hello.mdb", "", "")
            stmt := db.sql("SELECT 'Hello, JDBC Type1 World!' AS Message").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    ソースコード(Fantom + JDBC Type1 + ODBC + SQL Server)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:odbc:Driver={SQL Server};SERVER=(local)", "sa", "P@ssW0rd")
            stmt := db.sql("SELECT 'Hello, JDBC Type1 World!' AS Message").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    ソースコード(Fantom + JDBC Type1 + ODBC + Oracle)

    using sql
     
    class Hello
    {
        static Void main()
        {
            db := SqlConn.open("jdbc:odbc:Driver={Oracle in OraDb11g_home1};DBQ=ORCL", "scott", "tiger")
            stmt := db.sql("SELECT 'Hello, JDBC Type1 World!' AS Message FROM DUAL").query
            stmt.each |Obj row|
            {
                echo(row->Message)
            }
        }
    }

    実行方法

    C:¥> fan Hello.fan

    実行結果

    Hello, JDBC Type1 World!
  10. Hello, Fantom World!

    Posted on 1月 27th, 2012 by cx20

    Fantom

    Fantom は Java VM もしくは .NET 共通言語ランタイム上で動作するオブジェクト指向言語である。構文は Java や C# に似ている。
    言語の名前は当初、作者が住んでいる住所にちなんで「Fan」と付けられたが、検索しやすいように「Fantom」に変更された。

    ソースコード

    #!/usr/bin/env fan
    class Hello
    {
        static Void main()
        {
            echo("Hello, Fantom World!")
        }
    }

    実行方法(スクリプトファイルを指定して実行)

    $ fan hello.fan

    実行方法(実行権限を付与して実行)

    $ chmod +x hello.fan
    $ ./hello.fan

    実行方法(Java VM で実行)

    $ fan -version --Druntime=java
    Fantom Launcher
    Copyright (c) 2006-2011, Brian Frank and Andy Frank
    Licensed under the Academic Free License version 3.0
     
    Java Runtime:
      java.version:    1.6.0_29
      java.vm.name:    Java HotSpot(TM) 64-Bit Server VM
      java.vm.vendor:  Apple Inc.
      java.vm.version: 20.4-b02-402
      java.home:       /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
      fan.platform:    macosx-x86_64
      fan.version:     1.0.61
      fan.env:         sys::BootEnv
      fan.home:        /usr/local/Cellar/fantom/1.0.61/libexec
     
    $ fan --Druntime=java hello.fan

    実行方法(.NET Framework で実行)

    C:¥> fan -version --Druntime=dotnet
    Fantom Launcher
    Copyright (c) 2006-2011, Brian Frank and Andy Frank
    Licensed under the Academic Free License version 3.0
     
    .NET Runtime:
      clr.version:  2.0.50727.5448
      sys.platform: win32-x86
      sys.version:  1.0.61
     
    C:¥> fan --Druntime=dotnet hello.fan

    実行結果

    Hello, Fantom World!