Display key characters in sample applet

This commit is contained in:
James Roseborough
2014-01-17 04:57:22 +00:00
parent 8201095b0d
commit 8dce89ab7d

View File

@@ -48,6 +48,7 @@ end
-- the animation step moves the line endpoints -- the animation step moves the line endpoints
local x1,y1,x2,y2,xi,yi = 160,240,480,240,0,0 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 vx1,vy1,vx2,vy2,vxi,vyi = -5,-6,7,8,3,1
local chars = {}
local advance = function(x,vx,max,rnd) local advance = function(x,vx,max,rnd)
x = x + vx x = x + vx
if x < 0 then if x < 0 then
@@ -66,17 +67,40 @@ animate = function()
y2,vy2 = advance(y2,vy2,h) y2,vy2 = advance(y2,vy2,h)
xi,vxi = advance(xi,vxi,w-100) xi,vxi = advance(xi,vxi,w-100)
yi,vyi = advance(yi,vyi,h-100) yi,vyi = advance(yi,vyi,h-100)
while #chars > 0 and chars[1].n <= 1 do
table.remove(chars, 1)
end
for i,c in pairs(chars) do
c.n = c.n - 1
end
end end
-- the render step draws the scene -- the render step draws the scene
local bg = luajava.newInstance("java.awt.Color", 0x22112244,true); local bg = luajava.newInstance("java.awt.Color", 0x22112244,true);
local fg = luajava.newInstance("java.awt.Color", 0xffaa33); local fg = luajava.newInstance("java.awt.Color", 0xffaa33);
local ct = luajava.newInstance("java.awt.Color", 0x44ffff33,true);
render = function(graphics) render = function(graphics)
local w,h = applet:getWidth(), applet:getHeight()
graphics:setColor(bg) graphics:setColor(bg)
graphics:fillRect(0,0,applet:getWidth(), applet:getHeight()) graphics:fillRect(0,0,w,h)
graphics:drawImage(logo,xi,yi)
-- line
graphics:setColor(fg) graphics:setColor(fg)
graphics:drawLine(x1,y1,x2,y2) graphics:drawLine(x1,y1,x2,y2)
-- text
graphics:setColor(ct)
graphics:translate(w/2,h/2)
for i,c in pairs(chars) do
local s = 200 / (256-c.n)
graphics:scale(s, s)
graphics:drawString(c.text, c.x-4, c.y+6)
graphics:scale(1/s, 1/s)
end
graphics:translate(-w/2,-h/2)
-- image
graphics:drawImage(logo,xi,yi)
end end
-- add mouse listeners for specific mouse events -- add mouse listeners for specific mouse events
@@ -102,8 +126,14 @@ applet:addMouseMotionListener(luajava.createProxy("java.awt.event.MouseMotionLis
-- add key listeners -- add key listeners
applet:addKeyListener(luajava.createProxy("java.awt.event.KeyListener", { applet:addKeyListener(luajava.createProxy("java.awt.event.KeyListener", {
keyPressed = function(e) keyPressed = function(e)
local id, code, char, text = e:getID(), e:getKeyCode(), e:getKeyChar(), e:getKeyText(e:getKeyCode()) local id, code, char = e:getID(), e:getKeyCode(), e:getKeyChar()
print('key id, code, char, text, pcall(string.char,char)', id, code, char, text, pcall(string.char,char)) local text, s, c = e:getKeyText(code), pcall(string.char, char)
print('key id, code, char, text, pcall(string.char,char)', id, code, char, text, c)
table.insert(chars, {
n=255,
x=-6+12*math.random(),
y=-6+12*math.random(),
text=(s and c or '[?]')})
end, end,
-- ckeyReleased = function(e) end, -- ckeyReleased = function(e) end,
-- keyTyped = function(e) end, -- keyTyped = function(e) end,