Display key characters in sample applet
This commit is contained in:
@@ -48,6 +48,7 @@ end
|
||||
-- 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 chars = {}
|
||||
local advance = function(x,vx,max,rnd)
|
||||
x = x + vx
|
||||
if x < 0 then
|
||||
@@ -66,17 +67,40 @@ animate = function()
|
||||
y2,vy2 = advance(y2,vy2,h)
|
||||
xi,vxi = advance(xi,vxi,w-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
|
||||
|
||||
-- the render step draws the scene
|
||||
local bg = luajava.newInstance("java.awt.Color", 0x22112244,true);
|
||||
local fg = luajava.newInstance("java.awt.Color", 0xffaa33);
|
||||
local ct = luajava.newInstance("java.awt.Color", 0x44ffff33,true);
|
||||
render = function(graphics)
|
||||
local w,h = applet:getWidth(), applet:getHeight()
|
||||
graphics:setColor(bg)
|
||||
graphics:fillRect(0,0,applet:getWidth(), applet:getHeight())
|
||||
graphics:drawImage(logo,xi,yi)
|
||||
graphics:fillRect(0,0,w,h)
|
||||
|
||||
-- line
|
||||
graphics:setColor(fg)
|
||||
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
|
||||
|
||||
-- add mouse listeners for specific mouse events
|
||||
@@ -102,8 +126,14 @@ applet:addMouseMotionListener(luajava.createProxy("java.awt.event.MouseMotionLis
|
||||
-- add key listeners
|
||||
applet: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))
|
||||
local id, code, char = e:getID(), e:getKeyCode(), e:getKeyChar()
|
||||
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,
|
||||
-- ckeyReleased = function(e) end,
|
||||
-- keyTyped = function(e) end,
|
||||
|
||||
Reference in New Issue
Block a user