Archive for the ‘BeanShell’ Category

  1. Hello, Win32 GUI(BeanShell) World!

    Posted on 3月 19th, 2013 by cx20

    Win32 GUI(BeanShell)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は BeanShell にて Java の GUI ツールキット SWT の非公開 API を使用した Win32 GUI アプリケーション の例となっている。

    ソースコード

    import org.eclipse.swt.internal.Callback;
    import org.eclipse.swt.internal.win32.MSG;
    import org.eclipse.swt.internal.win32.OS;
    import org.eclipse.swt.internal.win32.PAINTSTRUCT;
    import org.eclipse.swt.internal.win32.RECT;
    import org.eclipse.swt.internal.win32.TCHAR;
    import org.eclipse.swt.internal.win32.WNDCLASS;
     
    public class Hello {
        public static int WndProc(int hWnd, int uMsg, int wParam, int lParam) {
            int hdc;
            PAINTSTRUCT lpPaint = new PAINTSTRUCT();
            String strMessage = "Hello, Win32 GUI(SWT) World!";
     
            switch (uMsg) {
            case OS.WM_PAINT:
                RECT rect = new RECT();
                hdc = OS.BeginPaint(hWnd, lpPaint);
                OS.GetClientRect(hWnd, rect);
                OS.DrawTextW(hdc, strMessage.toCharArray(), strMessage.length(), rect, OS.DT_SINGLELINE);
                OS.EndPaint(hWnd, lpPaint);
                return 0;
            case OS.WM_DESTROY:
                System.exit(wParam);
            default:
                return OS.DefWindowProc(hWnd, uMsg, wParam, lParam);
            }
        }
     
        public static void WinMain(String[] args) {
            int hInstance = OS.GetModuleHandle(null);
            TCHAR className  = new TCHAR(0, "helloWindow", true);
            TCHAR windowName = new TCHAR(0, "Hello, World", true);
     
            WNDCLASS wc      = new WNDCLASS();
            wc.style         = OS.CS_HREDRAW | OS.CS_VREDRAW;
            wc.lpfnWndProc   = new Callback(Hello.class, "WndProc", 4).getAddress();
            wc.hInstance     = hInstance;
            wc.hCursor       = OS.LoadCursor(0, OS.IDC_ARROW);
            wc.hbrBackground = OS.GetStockObject(OS.COLOR_WINDOW + 1 );
            wc.lpszClassName = OS.HeapAlloc(OS.GetProcessHeap(), OS.HEAP_ZERO_MEMORY, className.length() * 2);
            OS.MoveMemory(wc.lpszClassName, className, className.length() * 2);
     
            OS.RegisterClass(wc);
     
            int hWnd = OS.CreateWindowEx(
                0,
                className,
                windowName,
                OS.WS_OVERLAPPEDWINDOW,
                OS.CW_USEDEFAULT, 
                OS.CW_USEDEFAULT,
                640,
                480,
                0,
                0,
                hInstance,
                null);
     
            OS.ShowWindow(hWnd, OS.SW_SHOW);
            OS.UpdateWindow(hWnd);
     
            MSG lpMsg = new MSG();
            while (OS.GetMessage(lpMsg, 0, 0, 0)) {
                OS.TranslateMessage(lpMsg);
                OS.DispatchMessage(lpMsg);
            }
        }
     
        public static void main(String[] args) {
            WinMain( args );
        }
     
    }

    コンパイル&実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter Hello.bsh

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(SWT) World!              |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, COM(BeanShell) World!

    Posted on 1月 22nd, 2013 by cx20

    COM(BeanShell)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    BeanShell 自身には、COM を呼び出す機能を持っていないが、別途ライブラリを経由することで呼び出すことが可能である。
    以下は BeanShell より JACOB(JAVA-COM Bridge) ライブラリを使用した COM クライアントの例となっている。

    ソースコード

    import com.jacob.com.Variant;
    import com.jacob.activeX.ActiveXComponent;
     
    ActiveXComponent shell = new ActiveXComponent("Shell.Application"); 
    Variant hwnd = new Variant(0);
    Variant title = new Variant("Hello, COM(Jacob) World!");
    Variant option = new Variant(0);
    Variant rootFolder = new Variant(36);
    Variant[] params = new Variant[] { hwnd, title, option, rootFolder };
    Object folder = shell.invoke( "BrowseForFolder", params );

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;jacob.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter Hello.bsh

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(JACOB) Wolrd!               |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+
  3. Hello, Win32 API(BeanShell) World!

    Posted on 1月 12th, 2013 by cx20

    Win32 API(BeanShell)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は BeanShell にて SWT の非公開 API を使用した Win32 API 呼出しの例となっている。

    ソースコード

    import org.eclipse.swt.internal.win32.OS;
    import org.eclipse.swt.internal.win32.TCHAR;
     
    TCHAR lpText = new TCHAR(0, "Hello, Win32 API World!", true);
    TCHAR lpCaption = new TCHAR(0, "Hello, World", true);
    OS.MessageBox(0, lpText, lpCaption, OS.MB_OK );

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter Hello.bsh

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API World!
    ---------------------------
    OK   
    ---------------------------
  4. Hello, Java 3D(BeanShell) World!

    Posted on 12月 12th, 2012 by cx20

    Java 3D(BeanShell)

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

    ソースコード

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

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

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

    Posted on 12月 11th, 2012 by cx20

    Java 2D(BeanShell)

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

    ソースコード

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.*;
     
    class Hello extends JFrame {
        Hello( title ) {
            super( title );
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(640, 480);
     
            panel = new HelloPanel();
            add( panel );
        }
    }
     
    class HelloPanel extends JPanel {
        void paintComponent(Graphics g) {
            g2 = (Graphics2D)g;
            g.drawString("Hello, Java2D World!", 0, 16);
        }
    }
     
    frame = new Hello("Hello, World");
    frame.setVisible(true);

    コンパイル&実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Java 2D World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, SWT(BeanShell) World!

    Posted on 12月 9th, 2012 by cx20

    SWT(BeanShell)

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

    ソースコード

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.layout.*;
     
    display = new Display();
    shell = new Shell(display);
    shell.setText("Hello, World");
     
    layout = new FillLayout(SWT.VERTICAL);
    shell.setLayout(layout);
     
    label = new 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:¥> SET CLASSPATH=bsh-2.0b4.jar;org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

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

    Posted on 12月 8th, 2012 by cx20

    Swing(BeanShell)

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

    ソースコード

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Hello extends JFrame {
     
        Hello( String title ) {
            super( title );
            setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            setLocationRelativeTo( null );
            setSize( 640, 480 );
     
            label = new JLabel( "Hello, Swing World!" );
            label.setVerticalAlignment(JLabel.TOP);
            add( label );
        }
    }
     
    frame = new Hello( "Hello, World" );
    frame.setVisible( true );

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

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

    Posted on 12月 7th, 2012 by cx20

    AWT(BeanShell)

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

    ソースコード

    import java.awt.*;
    import java.awt.event.*;
     
    class Hello extends Frame {
        Hello( String title ) {
            super( title );
            addWindowListener(new HelloWindowAdapter());
            setSize(640, 480);
            setLayout(new FlowLayout(FlowLayout.LEFT));
            label = new Label("Hello, AWT World!");
            add(label);
        }
    }
     
    class HelloWindowAdapter extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
     
    Hello frame = new Hello( "Hello, World" );
    frame.setVisible(true);

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

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

    Posted on 10月 18th, 2012 by cx20

    JDBC Type4

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

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

    import java.sql.*;
     
    Class.forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    conn = DriverManager.getConnection("jdbc:sqlserver://;serverName=localhost", "sa", "P@ssW0rd");
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT 'Hello, JDBC Type4 World!' AS Message");
    while ( rs.next() ) {
         System.out.println( rs.getString(1) );
    }
    rs.close();
    stmt.close();
    conn.close();

    実行方法(BeanShell + JDBC Type4 + SQL Server)

    C:¥> java -cp "bsh-2.0b4.jar;sqljdbc4.jar;." bsh.Interpreter hello.bsh

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

    import java.sql.*;
     
    Class.forName ("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL");
    while ( rs.next() ) {
         System.out.println( rs.getString(1) );
    }
    rs.close();
    stmt.close();
    conn.close();

    実行方法(BeanShell + JDBC Type4 + Oracle)

    C:¥> java -cp "bsh-2.0b4.jar;ojdbc6.jar;." bsh.Interpreter hello.bsh

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

    import java.sql.*;
     
    Class.forName ("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "P@ssW0rd");
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT 'Hello, JDBC Type4 World!' AS Message");
    while ( rs.next() ) {
         System.out.println( rs.getString(1) );
    }
    rs.close();
    stmt.close();
    conn.close();

    実行方法(BeanShell + JDBC Type4 + MySQL)

    C:¥> java -cp "bsh-2.0b4.jar;mysql-connector-java-5.1.22-bin.jar;." bsh.Interpreter hello.bsh

    実行結果

    Hello, JDBC Type4 World!
  10. Hello, JDBC Type2(BeanShell) World!

    Posted on 10月 17th, 2012 by cx20

    JDBC Type2

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

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

    import java.sql.*;
     
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:oci:@orcl", "scott", "tiger");
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT 'Hello, JDBC Type2 World!' AS Message FROM DUAL");
    while ( rs.next() ) {
        System.out.println( rs.getString(1) );
    }
    rs.close();
    stmt.close();
    conn.close();

    実行方法

    C:¥> java -cp "bsh-2.0b4.jar;ojdbc6.jar;." bsh.Interpreter hello.bsh

    実行結果

    Hello, JDBC Type2 World!