Transparency support suggestion

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Fredrik Israelsson

Transparency support suggestion

Post by Fredrik Israelsson » Sat Feb 15, 2003 10:08 am

Hello
I fixed transparency support and mailed to David Gilbert a few weeks ago. I have not gotten any response though, so I post it here for commenting.

Below is a description of the changes. The changes I've made are 100% backward compatible as I just added 3 functions. It requires suns java 1.4 for correct png-encoding.

I will mail the modified files to anyone upon request.

BTW, IE 6.0 on XP might not show transparent png:s correct, I get the background of the XP built in picture viewing program. It works in Opera, Netscape and Mozilla though, and is hopefully fixed in IE soon.

/Fredrik

------------------------------------------------------------
Description:

Modifications made to JFreeChart-0.9.4 in order to support
transparent png:s (and other formats made available through
javax.imageio.ImageIO).
------------------------------------------------------------
JFreeChart.java:

Added the function
public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info, int imageType)
that takes a BufferedImage image type, modified the existing function without imageType to call to call the new one.
------------------------------------------------------------
ChartUtilities.java:
Added
import javax.imageio.ImageIO;
and the functions
public static void saveChartToFile(File file,
JFreeChart chart,
int width, int height,
ChartRenderingInfo info,
String formatName,
boolean encodeAlpha) throws IOException

public static void saveChartToFile(File file,
JFreeChart chart,
int width, int height,
String formatName,
boolean encodeAlpha) throws IOException
------------------------------------------------------------
To try out the new feature:
create a chart...
chart.setBackgroundPaint(new Color(0,0,0,0));
try{
ChartUtilities.saveChartToFile(new File("test.png"), chart, 500, 270, "png", true);
}catch(Exception e){
System.out.println(e.toString());
}




------------------------------------------------------------
Addition to ChartUtilities.java
------------------------------------------------------------
import javax.imageio.ImageIO;

/**
* Saves the chart to an image file in jpg, png or other format.
* Supports transparency if the file format supports it.
* <P>
*
* @param file the file.
* @param chart the chart.
* @param width the image width.
* @param height the image height.
* @param formatName - a String containg the informal name of the format as described in javax.imageio.ImageIO (jpg, png, etc.)
* @param encodeAlpha encode in ABGR? Enabling transparency in PNG for example.
*
* @throws IOException if there are any I/O errors.
*/
public static void saveChartToFile(File file,
JFreeChart chart,
int width, int height,
String formatName,
boolean encodeAlpha) throws IOException {
saveChartToFile(file, chart, width, height, null, formatName, encodeAlpha);
}

/**
* Saves the chart to an image file in jpg, png or other format.
* Supports transparency if the file format supports it.
* <P>
*
* This method allows you to pass in a ChartRenderingInfo object, to collect
* information about the chart dimensions/entities. You will need this info
* if you want to create an HTML image map.
*
* @param file the file.
* @param chart the chart.
* @param width the image width.
* @param height the image height.
* @param info the chart rendering info.
* @param formatName a String containg the informal name of the format as described in javax.imageio.ImageIO (jpg, png etc.)
* @param encodeAlpha encode in ABGR? Enabling transparency in PNG for example.
*
* @throws IOException if there are any I/O errors.
*/
public static void saveChartToFile(File file,
JFreeChart chart,
int width, int height,
ChartRenderingInfo info,
String formatName,
boolean encodeAlpha) throws IOException {
if(encodeAlpha) ImageIO.write(chart.createBufferedImage(width, height, info, BufferedImage.TYPE_4BYTE_ABGR), formatName, file);
else ImageIO.write(chart.createBufferedImage(width, height, info, BufferedImage.TYPE_INT_RGB), formatName, file);
}

------------------------------------------------------------
Changes to JFreeChart.java
------------------------------------------------------------
/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param width the width.
* @param height the height.
* @param info optional object for collection chart dimension and entity information.
*
* @return a buffered image.
*/
public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info) {
return createBufferedImage(width, height, info, BufferedImage.TYPE_INT_RGB);
}

/**
* Creates and returns a buffered image into which the chart has been drawn.
*
* @param width the width.
* @param height the height.
* @param info optional object for collection chart dimension and entity information.
* @param imageType the imagetypes specified in BufferedImage
*
* @return a buffered image.
*/
public BufferedImage createBufferedImage(int width, int height, ChartRenderingInfo info, int imageType) {
BufferedImage image = new BufferedImage(width , height, imageType);
Graphics2D g2 = image.createGraphics();
this.draw(g2, new Rectangle2D.Double(0, 0, width, height), info);
g2.dispose();

return image;
}

David Gilbert

Re: Transparency support suggestion

Post by David Gilbert » Mon Feb 17, 2003 9:49 am

Hi Fredrik,

Thanks for your code. I'm sorry for missing your e-mail, things have been hectic over the last couple of weeks.

Since your code requires JDK 1.4, I'm going to hold off incorporating it directly into JFreeChart. But I will direct people to this thread if they ask about transparency...

Regards,

Dave Gilbert

Locked