Hello,
I’m a newcomer to JRuby and updated my install this morning to:
- JDK 1.5.0 update 15
- jruby-1.1RC3
To test it I found a sample (courtesy of
http://www.codecommit.com/blog/java/rapid-prototyping-with-jruby):
require ‘java’
JFrame = javax.swing.JFrame
JComponent = javax.swing.JComponent
GradientPaint = java.awt.GradientPaint
Color = java.awt.Color
class CustomComponent < JComponent
@paint = GradientPaint.new(0, 0, Color::BLACK, 0, 200, Color::WHITE)
def initialize
end
def paintComponent(g)
g.paint = @paint
g.fillRect(0, 0, getWidth, getHeight)
end
end
frame = JFrame.new ‘Test Frame’
frame.setSize(200, 200)
frame.add CustomComponent.new
frame.defaultCloseOperation = JFrame::EXIT_ON_CLOSE
frame.visible = true
However that just gave me a grey box, without the gradient. So I
played with it for a little while and changed the class definition to
this (which now works):
class CustomComponent < JComponent
def initialize
@paint2 = GradientPaint.new( 0, 0, Color::BLACK, 0, 200,
Color::WHITE )
end
def paintComponent(g)
g.setPaint( @paint2 )
# g.paint = @paint2
g.fillRect( 0, 0, getWidth, getHeight )
end
end
The key difference seems to be the call to setPaint() instead of
simply assigning to the attribute.
Is anyone able to tell me what’s going on here?
Thanks,
Andrew