Title: [960] trunk/image_voodoo/lib/image_voodoo.rb: Add scale() method.
Revision
960
Author
enebo
Date
2008-03-31 11:40:42 -0400 (Mon, 31 Mar 2008)

Log Message

Add scale() method. Add to_java method to be able to extra java object representing object data. Default x,y to 0 for JImagePanel. Make JImagePanel work with ImageVoodoo instance instead of raw java object

Modified Paths

Diff

Modified: trunk/image_voodoo/lib/image_voodoo.rb (959 => 960)


--- trunk/image_voodoo/lib/image_voodoo.rb	2008-03-27 20:50:57 UTC (rev 959)
+++ trunk/image_voodoo/lib/image_voodoo.rb	2008-03-31 15:40:42 UTC (rev 960)
@@ -27,15 +27,19 @@
   BAOS = java.io.ByteArrayOutputStream
 
   class JImagePanel < javax.swing.JPanel
-    def initialize(image, x, y)
+    def initialize(image, x=0, y=0)
       super()
       @image, @x, @y = image, x, y
     end
+    def image=(image)
+      @image = image
+      invalidate
+    end
     def getPreferredSize
       java.awt.Dimension.new(@image.width, @image.height)
     end
     def paintComponent(graphics)
-      graphics.drawImage(@image, @x, @y, nil)
+      graphics.drawImage(@image.to_java, @x, @y, nil)
     end
   end
 
@@ -61,6 +65,10 @@
     @src.width
   end
 
+  def to_java
+    @src
+  end
+
   def resize(width, height)
     target = BufferedImage.new(width, height, BufferedImage::TYPE_INT_RGB)
     graphics = target.graphics
@@ -112,7 +120,7 @@
     frame = JFrame.new("Preview")
     frame.add_window_listener WindowClosed.new(block)
     frame.set_bounds 0, 0, width + 20, height + 40
-    frame.add JImagePanel.new(@src, 10, 10)
+    frame.add JImagePanel.new(self, 10, 10)
     frame.visible = true
   end
 
@@ -122,13 +130,16 @@
     String.from_java_bytes(out.to_byte_array)
   end
 
-  def thumbnail(size)
-    scale = size.to_f / (width > height ? width : height)
-    new_width = (width * scale).to_i
-    new_height = (height * scale).to_i
+  def scale(ratio)
+    new_width = (width * ratio).to_i
+    new_height = (height * ratio).to_i
     resize(new_width, new_height) {|image| yield image }
   end
 
+  def thumbnail(size)
+    scale(size.to_f / (width > height ? width : height))
+  end
+
   def with_crop(left, top, right, bottom)
     subimage = @src.get_subimage(left, top, right - left, bottom - top)
     yield ImageVoodoo.new(subimage)
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to