Request for drawImage methods

A discussion forum for FXGraphics2D (adds a Java2D API to the JavaFX Canvas).
Locked
Bastien
Posts: 2
Joined: Mon Jan 05, 2015 1:18 pm
antibot: No, of course not.

Request for drawImage methods

Post by Bastien » Mon Jan 05, 2015 1:32 pm

Hi,

Good work for this library. I have developed a graphic library based on Java2D and now that we are switching on JavaFX, it is perfect.

I have one request for the drawImage methods. They all copy the provided image to an intermediate BufferedImage. Would it be possible to check if the provided image is actually a BufferedImage and perform the conversion only if it is not. For instance replace :

Code: Select all

public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
        BufferedImage img2 = new BufferedImage(width, height, 2);
        Graphics2D g2 = img2.createGraphics();
        g2.drawImage(img, 0, 0, width, height, (ImageObserver)null);
        WritableImage fxImage = SwingFXUtils.toFXImage(img2, (WritableImage)null);
        this.gc.drawImage(fxImage, (double)x, (double)y, (double)width, (double)height);
        return true;
}
with something like this

Code: Select all

    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
        BufferedImage img2 = convertToBufferedImage(img, width, height);
        WritableImage fxImage = SwingFXUtils.toFXImage(img2, (WritableImage)null);
        this.gc.drawImage(fxImage, (double)x, (double)y, (double)width, (double)height);
        return true;
    }

    private BufferedImage convertToBufferedImage(Image img, int width, int height){
        if(img instanceof BufferedImage){
            return (BufferedImage)img;
        }
        BufferedImage img2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img2.createGraphics();
        g2.drawImage(img, 0, 0, width, height, (ImageObserver) null);
        return img2;
    }
Cheers,

Bastien

Bastien
Posts: 2
Joined: Mon Jan 05, 2015 1:18 pm
antibot: No, of course not.

Re: Request for drawImage methods

Post by Bastien » Mon Jan 05, 2015 1:40 pm

Hi again,

After a smoke break, I thought that instead "convertToBufferImage" it could be "convertToWritableImage" like this :

Code: Select all

    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
        WritableImage fxImage = convertToWritableImage(img, width, height);
        this.gc.drawImage(fxImage, (double)x, (double)y, (double)width, (double)height);
        return true;
    }

    private WritableImage convertToWritableImage(Image img, int width, int height){
        final BufferedImage bufferedImage;
        if(img instanceof BufferedImage){
            bufferedImage = (BufferedImage)img;
        }
        else {
            bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = img2.createGraphics();
            g2.drawImage(img, 0, 0, width, height, (ImageObserver) null);
        }
        return SwingFXUtils.toFXImage(bufferedImage, null);
    }
Cheers,

Bastien

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Request for drawImage methods

Post by david.gilbert » Thu Jan 08, 2015 7:08 pm

This is a good suggestion, although a similar change has been made already at GitHub:

https://github.com/jfree/fxgraphics2d/c ... d4df849b17

Take a look and let me know if you think it needs any further change.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked