Archive for the ‘JRuby’ Category
-
Hello, COM(JRuby) World!
Posted on 1月 19th, 2013 by cx20
COM(JRuby)
COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
COM を用いて開発された部品であれば言語を問わず利用することができる。
JRuby 自身には、COM を呼び出す機能を持っていないが、別途ライブラリを経由することで呼び出すことが可能である。
以下は JRuby より JACOB(JAVA-COM Bridge) ライブラリを使用した COM クライアントの例となっている。ソースコード
require 'java' require 'jacob.jar' import 'com.jacob.com.Variant' import 'com.jacob.activeX.ActiveXComponent' @shell = ActiveXComponent.new("Shell.Application") @hwnd = Variant.new(0) @title = Variant.new("Hello, COM(JACOB) World!") @option = Variant.new(0) @rootFolder = Variant.new(36) @params = [ @hwnd, @title, @option, @rootFolder ] @folder = @shell.invoke( "BrowseForFolder", @params )
実行方法
C:¥> jruby Hello.rb
実行結果
+----------------------------------------+ |Browse For Folder [X]| +----------------------------------------+ | Hello, COM(JACOB) Wolrd! | | | | +------------------------------------+ | | |[Windows] | | | | +[addins] | | | | +[AppCompat] | | | | +[AppPatch] | | | | +[assembly] | | | | : | | | | : | | | | : | | | +------------------------------------+ | | [Make New Folder] [ OK ] [Cancel] | +----------------------------------------+
-
Hello, Win32 API(JRuby) World!
Posted on 1月 9th, 2013 by cx20
Win32 API(JRuby)
Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は JRuby にて SWT の非公開 API を使用した Win32 API 呼出しの例となっている。ソースコード
require 'java' require 'org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar' import 'org.eclipse.swt.internal.win32.OS' import 'org.eclipse.swt.internal.win32.TCHAR' @lpText = TCHAR.new(0, "Hello, Win32 API World!", true) @lpCaption = TCHAR.new(0, "Hello, World", true) OS::MessageBox(0, @lpText, @lpCaption, OS::MB_OK )
実行方法
C:¥> jruby Hello.rb
実行結果
--------------------------- Hello, World! --------------------------- Hello, Win32 API World! --------------------------- OK ---------------------------
-
Hello, Java 3D(JRuby) World!
Posted on 11月 24th, 2012 by cx20
Java 3D
Java 3D は Java による 3D グラフィックライブラリである。
以下は JRuby による Java 3D の使用例となっている。ソースコード
require 'java' import 'java.awt.BorderLayout' import 'java.awt.GraphicsConfiguration' import 'java.awt.Font' import 'javax.media.j3d.Canvas3D' import 'javax.media.j3d.BranchGroup' import 'javax.media.j3d.Transform3D' import 'javax.media.j3d.TransformGroup' import 'javax.media.j3d.Appearance' import 'javax.media.j3d.Material' import 'javax.media.j3d.Font3D' import 'javax.media.j3d.FontExtrusion' import 'javax.media.j3d.Text3D' import 'javax.media.j3d.Shape3D' import 'javax.media.j3d.DirectionalLight' import 'javax.media.j3d.RotationInterpolator' import 'javax.media.j3d.Alpha' import 'javax.media.j3d.BoundingSphere' import 'javax.swing.JFrame' import 'javax.vecmath.Vector3f' import 'javax.vecmath.Color3f' import 'com.sun.j3d.utils.universe.SimpleUniverse' class HelloFrame < JFrame def initialize( title ) super(title) self.setSize(640, 480) self.setDefaultCloseOperation( JFrame::EXIT_ON_CLOSE ) @canvas3D = createCanvas3D() @scene = createSceneGraph() connect(@canvas3D, @scene) end def createCanvas3D() getContentPane().setLayout(BorderLayout.new()) @config = SimpleUniverse.getPreferredConfiguration() @canvas3D = Canvas3D.new(@config) getContentPane().add(@canvas3D) return @canvas3D end def createSceneGraph() @objRoot = BranchGroup.new() @mover = moveTextBack() @spinner = createSpinner() @rotator = RotationInterpolator.new(Alpha.new(-1, 4000), @spinner, Transform3D.new(), 0.0, Math::PI * 2) @rotator.setSchedulingBounds( BoundingSphere.new() ) @spinner.addChild(@rotator) @objRoot.addChild(@mover) @mover.addChild(@spinner) @spinner.addChild( createTextShape()) setLighting(@mover) return @objRoot end def createSpinner() @spinner = TransformGroup.new() @spinner.setCapability(TransformGroup::ALLOW_TRANSFORM_WRITE) return @spinner end def moveTextBack() @transform3D = Transform3D.new() @transform3D.setTranslation(Vector3f.new(0.0, 0.0, -10.0)) return TransformGroup.new(@transform3D) end def createTextShape() @textAppear = Appearance.new() @textAppear.setMaterial(Material.new()) @font3D = Font3D.new(Font.new("MS Pゴシック", Font::PLAIN, 1), FontExtrusion.new()) @textGeom = Text3D.new(@font3D, String.new("Hello, Java 3D World!")) @textGeom.setAlignment(Text3D::ALIGN_CENTER) @textShape = Shape3D.new() @textShape.setGeometry(@textGeom) @textShape.setAppearance(@textAppear) return @textShape end def setLighting(objMove) @light = DirectionalLight.new() @light.setInfluencingBounds(BoundingSphere.new()) @light.setDirection(Vector3f.new(0.0,0.0,-1.0)) @light.setColor(Color3f.new(1.0, 1.0, 1.0)) objMove.addChild(@light) end def connect(canvas3D, scene) @universe = SimpleUniverse.new(canvas3D) @universe.getViewingPlatform().setNominalViewingTransform() @universe.addBranchGraph(scene) end end @frame = HelloFrame.new("Hello, World") @frame.setVisible(true)
実行方法
C:¥> jrubyw Hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Java 3D World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Java 2D(JRuby) World!
Posted on 11月 23rd, 2012 by cx20
Java 2D
Java 2D は Java による 2D グラフィックライブラリである。
以下は JRuby による Java 2D の使用例となっている。ソースコード
require 'java' import 'java.awt.Graphics' import 'java.awt.Graphics2D' import 'javax.swing.JFrame' import 'javax.swing.JPanel' class HelloFrame < JFrame def initialize( title ) super( title ) self.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE) self.setSize(640, 480) @panel = HelloPanel.new() add( @panel ) end end class HelloPanel < JPanel def paintComponent(g) @g2 = g @g2.drawString("Hello, Java2D World!", 0, 16) end end @frame = HelloFrame.new("Hello, World") @frame.setVisible(true)
コンパイル&実行方法
C:¥> jrubyw Hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Java 2D World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, JavaFX(JRuby) World!
Posted on 11月 22nd, 2012 by cx20
JavaFX(Scala)
JavaFX は Java による RIA 向けの GUI ライブラリならびにプラットフォームである。
類似の RIA プラットフォームとしては Adobe Flex や Microsoft Silverlight などがある。
以下は JRuby にて JRubyFX ライブラリを用いた JavaFX の使用例となっている。ソースコード
require 'jrubyfx' class Hello include JRubyFX def start(stage) hbox = HBox.new hbox.children << Label.new("Hello, JavaFX World!") stage.scene = Scene.new(hbox, 640, 480) stage.title = "Hello, World" stage.show end end Hello.start
実行方法
C:¥> jruby --1.9 Hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, JavaFX World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, SWT(JRuby) World!
Posted on 11月 21st, 2012 by cx20
SWT(JRuby)
SWT(Standard Widget Toolkit) は Java で GUI を扱うためのライブラリである。
IBM により AWT や Swing を置き換える目的で作成された。
以下は JRuby による SWT の使用例となっている。ソースコード
require 'java' require 'org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar' import 'org.eclipse.swt.SWT' import 'org.eclipse.swt.widgets.Display' import 'org.eclipse.swt.widgets.Shell' import 'org.eclipse.swt.widgets.Label' import 'org.eclipse.swt.layout.FillLayout' @display = Display.new @shell = Shell.new(display) @shell.setText("Hello, World") @layout = FillLayout.new(SWT::VERTICAL) @shell.setLayout(@layout) @label = Label.new(@shell,SWT::BORDER) @label.setText("Hello, SWT World!") @shell.setSize( 640, 480 ) @shell.open while (!@shell.isDisposed) do if (!@display.readAndDispatch) then @display.sleep end end @display.dispose
実行方法
C:¥> jrubyw Hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, SWT World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, Swing(JRuby) World!
Posted on 11月 20th, 2012 by cx20
Swing(JRuby)
Swing は AWT(Abstract Window Toolkit) を拡張したものであり Java で GUI を扱うためのライブラリである。
J2SE 1.2 以降は AWT よりも Swing が使われることが多くなっている。
以下は JRuby による Swing の使用例となっている。ソースコード
require 'java' import 'javax.swing.JFrame' import 'javax.swing.JLabel' class Hello < JFrame def initialize(title) super( title ) self.setDefaultCloseOperation( JFrame::EXIT_ON_CLOSE ) self.setSize( 640, 480 ) @label = JLabel.new( "Hello, Swing World!" ) @label.setVerticalAlignment(JLabel::TOP) self.add( @label ) end end @frame = Hello.new( "Hello, World" ) @frame.setVisible( true )
実行方法
C:¥> jrubyw Hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Swing World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, AWT(JRuby) World!
Posted on 11月 19th, 2012 by cx20
AWT(JRuby)
AWT(Abstract Window Toolkit) は Java で GUI を扱うためのライブラリである。
J2SE 1.2 以降は AWT を拡張した Swing が使われることが多くなっている。
以下は JRuby による AWT の使用例となっている。ソースコード
require "java" import java.lang.System import java.awt.Frame import java.awt.Label import java.awt.FlowLayout import java.awt.event.WindowAdapter class Hello < Frame def initialize(title) super(title) self.addWindowListener(HelloWindowAdapter.new) self.setSize(640, 480) self.setLayout(FlowLayout.new(FlowLayout::LEFT)) @label = Label.new('Hello, AWT World!') self.add(@label) end end class HelloWindowAdapter < WindowAdapter def windowClosing(e) System.exit(0) end end @frame = Hello.new('Hello, World') @frame.setVisible(true)
実行方法
C:¥> jrubyw Hello.rb
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, AWT World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+
-
Hello, JDBC Type4(JRuby) World!
Posted on 10月 9th, 2012 by cx20
JDBC Type4
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type4 は DBMS のクライアントライブラリを使用しない Pure Java の DBMS ドライバである。
以下は JRuby による JDBC ライブラリの使用例となっている。ソースコード(JRuby + JDBC Type4 + SQL Server)
require 'rubygems' require 'java' require 'sqljdbc4.jar' Java::com.microsoft.sqlserver.jdbc.SQLServerDriver conn = java.sql.DriverManager.get_connection("jdbc:sqlserver://;serverName=localhost", "sa", "P@ssW0rd") stmt = conn.create_statement rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message") while (rs.next) do puts rs.getString(1) end rs.close stmt.close conn.close
ソースコード(Scala + JDBC Type4 + Oracle)
require 'rubygems' require 'java' require 'ojdbc6.jar' Java::oracle.jdbc.driver.OracleDriver conn = java.sql.DriverManager.get_connection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger") stmt = conn.create_statement rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL") while (rs.next) do puts rs.getString(1) end rs.close stmt.close conn.close
ソースコード(JRuby + JDBC Type4 + MySQL)
require 'rubygems' require 'java' require 'mysql-connector-java-5.1.22-bin.jar' Java::com.mysql.jdbc.Driver conn = java.sql.DriverManager.get_connection("jdbc:mysql://localhost:3306", "root", "P@ssW0rd") stmt = conn.create_statement rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message") while (rs.next) do puts rs.getString(1) end rs.close stmt.close conn.close
実行方法
C:¥> jruby Hello.rb
実行結果
Hello, JDBC Type4 World!
-
Hello, JDBC Type2(JRuby) World!
Posted on 10月 8th, 2012 by cx20
JDBC Type2
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type2 は JDBC と DBMS クライアントの API をマップさせたブリッジドライバである。
例えば、Oracle であればクライアントライブラリとして OCI が使用される。
以下は JRuby による JDBC ライブラリの使用例となっている。ソースコード(JRuby + JDBC Type2 + Oracle)
require 'rubygems' require 'java' require 'ojdbc6.jar' Java::oracle.jdbc.driver.OracleDriver conn = java.sql.DriverManager.get_connection("jdbc:oracle:oci:@orcl", "scott", "tiger") stmt = conn.create_statement rs = stmt.execute_query("SELECT 'Hello, JDBC Type2 World!' AS Message FROM DUAL") while (rs.next) do puts rs.getString(1) end rs.close stmt.close conn.close
実行方法
C:¥> jruby Hello.rb
実行結果
Hello, JDBC Type2 World!