SWT Backgroundimage

A free public discussion forum for the JFreeChart class library.

SWT Backgroundimage

Postby sommer » Fri Nov 23, 2007 11:54 am

Hi everyone,
we have got a problem with the chart(plot).setBackgroundImage Function and SWT.
The Image is loaded but not shown.
I already tried several conversions between swt and awt but until now they don't show the expected behaviour.

Any Suggestions?

Greetings
Hauke
sommer
 
Posts: 4
Joined: Tue Aug 21, 2007 6:53 am

Postby heprom » Sun Nov 25, 2007 9:32 am

Thanks for reporting the bug, this can be due to some unimplemented method in the SWTGraphics2D class. I've open a tracker on source forge (1837979); I'll have a look at this. It would help to have some code to reproduce the issue.

Regards,
/heprom
heprom
 
Posts: 91
Joined: Sat May 27, 2006 4:25 am
Location: Paris

Postby david.gilbert » Mon Nov 26, 2007 12:06 pm

Here's a patch that I knocked together. Bear in mind that I know next-to-nothing about SWT, so this might be rubbish...but it seems to work (a little slowly perhaps):

Code: Select all
Index: /home/dgilbert/workspace/jfreechart-1.0.x-bugs/swt/org/jfree/experimental/swt/SWTGraphics2D.java
===================================================================
--- /home/dgilbert/workspace/jfreechart-1.0.x-bugs/swt/org/jfree/experimental/swt/SWTGraphics2D.java   (revision 429)
+++ /home/dgilbert/workspace/jfreechart-1.0.x-bugs/swt/org/jfree/experimental/swt/SWTGraphics2D.java   (working copy)
@@ -49,6 +49,7 @@
  *               still ignored at this point) (DG);
  * 23-Oct-2007 : Implemented drawPolygon(), drawPolyline(), drawOval(),
  *               fillOval(), drawArc() and fillArc() (DG);
+ * 26-Nov-2007 : Implemented a couple of drawImage() methods (DG);
  *
  */

@@ -74,6 +75,7 @@
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
+import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.DirectColorModel;
@@ -934,7 +936,8 @@
     }

     /**
-     * Draws an image at (x, y).
+     * Draws an SWT image with the top left corner of the image aligned to the
+     * point (x, y).
      *
      * @param image  the image.
      * @param x  the x-coordinate.
@@ -962,24 +965,55 @@

     }

-    /* (non-Javadoc)
-     * @see java.awt.Graphics#drawImage(java.awt.Image, int, int,
-     * java.awt.image.ImageObserver)
+    /**
+     * Draws an image with the top left corner aligned to the point (x, y).
+     *
+     * @param image  the image.
+     * @param x  the x-coordinate.
+     * @param y  the y-coordinate.
+     * @param observer  ignored here.
+     *
+     * @return <code>true</code> if the image has been drawn.
      */
     public boolean drawImage(Image image, int x, int y,
             ImageObserver observer) {
-        // TODO Auto-generated method stub
-        return false;
+        ImageData data = convertAWTImageToSWT(image);
+        if (data == null) {
+            return false;
+        }
+        org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image(
+                this.gc.getDevice(), data);
+        this.gc.drawImage(im, x, y);
+        im.dispose();
+        return true;
     }

-    /* (non-Javadoc)
-     * @see java.awt.Graphics#drawImage(java.awt.Image, int, int, int, int,
-     * java.awt.image.ImageObserver)
+    /**
+     * Draws an image with the top left corner aligned to the point (x, y),
+     * and scaled to the specified width and height.
+     *
+     * @param image  the image.
+     * @param x  the x-coordinate.
+     * @param y  the y-coordinate.
+     * @param width  the width for the rendered image.
+     * @param height  the height for the rendered image.
+     * @param observer  ignored here.
+     *
+     * @return <code>true</code> if the image has been drawn.
      */
     public boolean drawImage(Image image, int x, int y, int width, int height,
             ImageObserver observer) {
-        // TODO Auto-generated method stub
-        return false;
+        ImageData data = convertAWTImageToSWT(image);
+        if (data == null) {
+            return false;
+        }
+        org.eclipse.swt.graphics.Image im = new org.eclipse.swt.graphics.Image(
+                this.gc.getDevice(), data);
+        org.eclipse.swt.graphics.Rectangle bounds = im.getBounds();
+        this.gc.drawImage(im, 0, 0, bounds.width, bounds.height, x, y, width,
+                height);
+        im.dispose();
+        return true;
     }

     /* (non-Javadoc)
@@ -988,8 +1022,18 @@
      */
     public boolean drawImage(Image image, int x, int y, Color bgcolor,
             ImageObserver observer) {
-        // TODO Auto-generated method stub
-        return false;
+        if (image == null) {
+            throw new IllegalArgumentException("Null 'image' argument.");
+        }
+        int w = image.getWidth(null);
+        int h = image.getHeight(null);
+        if (w == -1 || h == -1) {
+            return false;
+        }
+        Paint savedPaint = getPaint();
+        fill(new Rectangle2D.Double(x, y, w, h));
+        setPaint(savedPaint);
+        return drawImage(image, x, y, observer);
     }

     /* (non-Javadoc)
@@ -998,8 +1042,18 @@
      */
     public boolean drawImage(Image image, int x, int y, int width, int height,
             Color bgcolor, ImageObserver observer) {
-        // TODO Auto-generated method stub
-        return false;
+        if (image == null) {
+            throw new IllegalArgumentException("Null 'image' argument.");
+        }
+        int w = image.getWidth(null);
+        int h = image.getHeight(null);
+        if (w == -1 || h == -1) {
+            return false;
+        }
+        Paint savedPaint = getPaint();
+        fill(new Rectangle2D.Double(x, y, w, h));
+        setPaint(savedPaint);
+        return drawImage(image, x, y, width, height, observer);
     }

     /* (non-Javadoc)
@@ -1188,6 +1242,29 @@
         return awtTransform;
     }

+    /**
+     * Converts an AWT image to SWT.
+     *
+     * @param image  the image (<code>null</code> not permitted).
+     *
+     * @return Image data.
+     */
+    static ImageData convertAWTImageToSWT(Image image) {
+        if (image == null) {
+            throw new IllegalArgumentException("Null 'image' argument.");
+        }
+        int w = image.getWidth(null);
+        int h = image.getHeight(null);
+        if (w == -1 || h == -1) {
+            return null;
+        }
+        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
+        Graphics g = bi.getGraphics();
+        g.drawImage(image, 0, 0, null);
+        g.dispose();
+        return convertToSWT(bi);
+    }
+   
     static ImageData convertToSWT(BufferedImage bufferedImage) {
         if (bufferedImage.getColorModel() instanceof DirectColorModel) {
             DirectColorModel colorModel


Should I commit this to Subversion?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11316
Joined: Fri Mar 14, 2003 10:29 am

Postby heprom » Mon Nov 26, 2007 6:07 pm

David, this looks good, please commit...

Henry
heprom
 
Posts: 91
Joined: Sat May 27, 2006 4:25 am
Location: Paris

Postby sommer » Wed Nov 28, 2007 10:16 am

Thanks David,
looks very good under Windows.
Under Linux I didn't get it to work by now but I'm trying.

Hauke
sommer
 
Posts: 4
Joined: Tue Aug 21, 2007 6:53 am

Postby david.gilbert » Wed Nov 28, 2007 10:23 am

It should be OK on Linux - my development machine runs Ubuntu Linux.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11316
Joined: Fri Mar 14, 2003 10:29 am

Postby sommer » Thu Nov 29, 2007 6:11 am

Hi David,
on an other machine it works.
Seems to be a version problem.

Hauke
sommer
 
Posts: 4
Joined: Tue Aug 21, 2007 6:53 am


Return to JFreeChart - General

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 9 guests