Problem with direct assignment versus setter method

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

class CustomComponent < JComponent

@paint = GradientPaint.new(0, 0, Color::BLACK, 0, 200,

Color::WHITE)

@paint is a class variable not an instance variable

def initialize
# all instance class here!
@paint = GradientPaint.new(0, 0, Color::BLACK, 0, 200,
Color::WHITE)
end

def paintComponent(g)
g.paint = @paint # it work!
g.fillRect(0, 0, getWidth, getHeight)
end
end

Tiziano

The instance variable is one issue. It turns out that the Graphics2D
class (of which an instance is passed to paintComponent) defines a
public paint attribute and this is used instead of calling the setPaint
method.

See:

http://jira.codehaus.org/browse/JRUBY-2395

/s/ Andrew

Tiziano M. wrote:

class CustomComponent < JComponent

@paint = GradientPaint.new(0, 0, Color::BLACK, 0, 200,

Color::WHITE)

@paint is a class variable not an instance variable

def initialize
# all instance class here!
@paint = GradientPaint.new(0, 0, Color::BLACK, 0, 200,
Color::WHITE)
end

def paintComponent(g)
g.paint = @paint # it work!
g.fillRect(0, 0, getWidth, getHeight)
end
end

Tiziano

I tryed this code with jruby 1.1RC1 and it uses GradientPaint

Tiziano

def paintComponent(g)

   g.paint = @paint  # it works!
   g.fillRect(0, 0, getWidth, getHeight)

end

Andrew Whitehouse wrote:

The instance variable is one issue. It turns out that the Graphics2D
class (of which an instance is passed to paintComponent) defines a
public paint attribute and this is used instead of calling the setPaint
method.

See:

http://jira.codehaus.org/browse/JRUBY-2395

/s/ Andrew

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs