Improve the swing sample lua application.
This commit is contained in:
13
README.html
13
README.html
@@ -19,7 +19,7 @@ Getting Started with LuaJ
|
|||||||
James Roseborough, Ian Farmer, Version 2.0.3
|
James Roseborough, Ian Farmer, Version 2.0.3
|
||||||
<p>
|
<p>
|
||||||
<small>
|
<small>
|
||||||
Copyright © 2009-2010 Luaj.org.
|
Copyright © 2009-2012 Luaj.org.
|
||||||
Freely available under the terms of the
|
Freely available under the terms of the
|
||||||
<a href="http://sourceforge.net/dbimage.php?id=196142">Luaj license</a>.
|
<a href="http://sourceforge.net/dbimage.php?id=196142">Luaj license</a>.
|
||||||
</small>
|
</small>
|
||||||
@@ -191,6 +191,12 @@ You should see the following output:
|
|||||||
hello, world
|
hello, world
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
To see how luaj can be used to acccess most Java API's including swing, try:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
java -cp lib/luaj-jse-2.0.3.jar lua examples/lua/swingapp.lua
|
||||||
|
</pre>
|
||||||
|
|
||||||
<h2>Compile lua source to lua bytecode</h2>
|
<h2>Compile lua source to lua bytecode</h2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@@ -501,7 +507,7 @@ The <em>JsePlatform.standardGlobals()</em> includes the <em>luajava</em> library
|
|||||||
It is patterned after the original <a href="http://www.keplerproject.org/luajava/">luajava project</a>.
|
It is patterned after the original <a href="http://www.keplerproject.org/luajava/">luajava project</a>.
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The following lua script will open a swiing frame on Java SE:
|
The following lua script will open a swing frame on Java SE:
|
||||||
<pre>
|
<pre>
|
||||||
jframe = luajava.bindClass( "javax.swing.JFrame" )
|
jframe = luajava.bindClass( "javax.swing.JFrame" )
|
||||||
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
|
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
|
||||||
@@ -511,7 +517,8 @@ The following lua script will open a swiing frame on Java SE:
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
See a longer sample in <em>examples/lua/swingapp.lua</em> for details, or try running it using:
|
See a longer sample in <em>examples/lua/swingapp.lua</em> for details, including a simple animation loop, rendering graphics, mouse and key handling, and image loading.
|
||||||
|
Or try running it using:
|
||||||
<pre>
|
<pre>
|
||||||
java -cp lib/luaj-jse-2.0.3.jar lua examples/lua/swingapp.lua
|
java -cp lib/luaj-jse-2.0.3.jar lua examples/lua/swingapp.lua
|
||||||
</pre>
|
</pre>
|
||||||
|
|||||||
BIN
examples/lua/logo.gif
Normal file
BIN
examples/lua/logo.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.6 KiB |
@@ -1,29 +1,132 @@
|
|||||||
|
-- Sample luaj program that presents an animated Swing window.
|
||||||
|
--
|
||||||
|
-- This basic application handles key, and mouse input, has a basic animation loop,
|
||||||
|
-- and renders double-buffered graphics including the logo image in a swing frame.
|
||||||
|
--
|
||||||
|
|
||||||
frame = luajava.newInstance( "javax.swing.JFrame", "Texts" );
|
-- bind to classes we need
|
||||||
pane = luajava.newInstance( "javax.swing.JPanel" );
|
local borderLayout = luajava.bindClass("java.awt.BorderLayout")
|
||||||
borderFactory = luajava.bindClass( "javax.swing.BorderFactory" )
|
local jframe = luajava.bindClass("javax.swing.JFrame")
|
||||||
border = borderFactory:createEmptyBorder( 30, 30, 10, 30 )
|
local bufferedImage = luajava.bindClass("java.awt.image.BufferedImage")
|
||||||
pane:setBorder( border )
|
local swingUtilities = luajava.bindClass("javax.swing.SwingUtilities")
|
||||||
label = luajava.newInstance( "javax.swing.JLabel", "This is a Label" );
|
local thread = luajava.bindClass("java.lang.Thread")
|
||||||
|
|
||||||
|
-- set up frame, get content pane
|
||||||
|
local frame = luajava.newInstance("javax.swing.JFrame", "Sample Luaj Application");
|
||||||
|
local content = frame:getContentPane()
|
||||||
|
|
||||||
layout = luajava.newInstance( "java.awt.GridLayout", 2, 2 );
|
-- add a buffered image as content
|
||||||
pane:setLayout( layout )
|
local image = luajava.newInstance("java.awt.image.BufferedImage", 640, 480, bufferedImage.TYPE_INT_RGB)
|
||||||
pane:add( label )
|
local icon = luajava.newInstance("javax.swing.ImageIcon", image)
|
||||||
pane:setBounds( 20, 30, 10, 30 )
|
local label = luajava.newInstance("javax.swing.JLabel", icon)
|
||||||
|
|
||||||
borderLayout = luajava.bindClass( "java.awt.BorderLayout" )
|
-- add the main pane to the main content
|
||||||
frame:getContentPane():add(pane, borderLayout.CENTER )
|
content:add(label, borderLayout.CENTER)
|
||||||
jframe = luajava.bindClass( "javax.swing.JFrame" )
|
|
||||||
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
|
frame:setDefaultCloseOperation(jframe.EXIT_ON_CLOSE)
|
||||||
frame:pack()
|
frame:pack()
|
||||||
frame:setVisible(true)
|
|
||||||
|
|
||||||
local listener = luajava.createProxy("java.awt.event.MouseListener",
|
-- simple animation framework
|
||||||
{
|
local tick,animate,render
|
||||||
mouseClicked = function(me)
|
local tnext = 0
|
||||||
print("clicked!", me)
|
tick = luajava.createProxy("java.lang.Runnable", {
|
||||||
|
run = function()
|
||||||
|
swingUtilities:invokeLater(tick)
|
||||||
|
if os.time()< tnext then return thread:sleep(1) end
|
||||||
|
tnext = math.max(os.time(),tnext+1000/60)
|
||||||
|
pcall(animate)
|
||||||
|
pcall(render)
|
||||||
|
label:repaint(0,0,0,640,480)
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
frame:addMouseListener(listener)
|
-- the animation step moves the line endpoints
|
||||||
|
local x1,y1,x2,y2,xi,yi = 160,240,480,240,0,0
|
||||||
|
local vx1,vy1,vx2,vy2,vxi,vyi = -5,-6,7,8,3,1
|
||||||
|
local advance = function(x,vx,max,rnd)
|
||||||
|
x = x + vx
|
||||||
|
if x < 0 then
|
||||||
|
return 0, math.random(2,10)
|
||||||
|
elseif x > max then
|
||||||
|
return max, math.random(-10,-2)
|
||||||
|
end
|
||||||
|
return x, vx
|
||||||
|
end
|
||||||
|
animate = function()
|
||||||
|
x1,y1,x2,y2 = x1+1,y1+1,x2-1,y2-1
|
||||||
|
x1,vx1 = advance(x1,vx1,640)
|
||||||
|
y1,vy1 = advance(y1,vy1,480)
|
||||||
|
x2,vx2 = advance(x2,vx2,640)
|
||||||
|
y2,vy2 = advance(y2,vy2,480)
|
||||||
|
xi,vxi = advance(xi,vxi,540)
|
||||||
|
yi,vyi = advance(yi,vyi,380)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- try loading the logo image from a couple locations
|
||||||
|
local imageio = luajava.bindClass("javax.imageio.ImageIO")
|
||||||
|
local file = luajava.bindClass("java.io.File")
|
||||||
|
local loadimage = function(path)
|
||||||
|
local s,i = pcall(imageio.read, imageio, luajava.new(file, path))
|
||||||
|
return s and i
|
||||||
|
end
|
||||||
|
local logo = loadimage("logo.gif") or loadimage("examples/lua/logo.gif")
|
||||||
|
|
||||||
|
-- the render step draws the scene
|
||||||
|
local g = image:getGraphics()
|
||||||
|
local bg = luajava.newInstance("java.awt.Color", 0x22112244, true);
|
||||||
|
local fg = luajava.newInstance("java.awt.Color", 0xffaa33);
|
||||||
|
render = function()
|
||||||
|
g:setColor(bg)
|
||||||
|
g:fillRect(0,0,640,480)
|
||||||
|
if logo then g:drawImage(logo,xi,yi) end
|
||||||
|
g:setColor(fg)
|
||||||
|
g:drawLine(x1,y1,x2,y2)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- add mouse listeners for specific mouse events
|
||||||
|
label:addMouseListener(luajava.createProxy("java.awt.event.MouseListener", {
|
||||||
|
mousePressed = function(e)
|
||||||
|
--print('mousePressed', e:getX(), e:getY(), e)
|
||||||
|
x1,y1 = e:getX(),e:getY()
|
||||||
|
end,
|
||||||
|
-- mouseClicked = function(e) end,
|
||||||
|
-- mouseEntered = function(e) end,
|
||||||
|
-- mouseExited = function(e) end,
|
||||||
|
-- mouseReleased = function(e) end,
|
||||||
|
}))
|
||||||
|
label:addMouseMotionListener(luajava.createProxy("java.awt.event.MouseMotionListener", {
|
||||||
|
mouseDragged = function(e)
|
||||||
|
--print('mouseDragged', e:getX(), e:getY(), e)
|
||||||
|
x2,y2 = e:getX(),e:getY()
|
||||||
|
end,
|
||||||
|
-- mouseMoved= function(e) end,
|
||||||
|
}))
|
||||||
|
|
||||||
|
-- add key listeners
|
||||||
|
frame:addKeyListener(luajava.createProxy("java.awt.event.KeyListener", {
|
||||||
|
keyPressed = function(e)
|
||||||
|
local id, code, char, text = e:getID(), e:getKeyCode(), e:getKeyChar(), e:getKeyText(e:getKeyCode())
|
||||||
|
print('key id, code, char, text, pcall(string.char,char)', id, code, char, text, pcall(string.char,char))
|
||||||
|
end,
|
||||||
|
-- keyReleased = function(e) end,
|
||||||
|
-- keyTyped = function(e) end,
|
||||||
|
}))
|
||||||
|
|
||||||
|
-- use the window listener to kick off animation
|
||||||
|
frame:addWindowListener(luajava.createProxy("java.awt.event.WindowListener", {
|
||||||
|
windowOpened = function(e)
|
||||||
|
swingUtilities:invokeLater(tick)
|
||||||
|
end,
|
||||||
|
-- windowActivated = function(e) end,
|
||||||
|
-- windowClosed = function(e) end,
|
||||||
|
-- windowClosing = function(e) end,
|
||||||
|
-- windowDeactivated = function(e) end,
|
||||||
|
-- windowDeiconified = function(e) end,
|
||||||
|
-- windowIconified = function(e) end,
|
||||||
|
}))
|
||||||
|
|
||||||
|
-- utility function to load an image from a file, for reference
|
||||||
|
local loadimage = function(filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Set window visible last to start app.
|
||||||
|
frame:setVisible(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user