need multiple background images

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mayjune2004
Posts: 1
Joined: Fri May 07, 2010 6:46 pm
antibot: No, of course not.

need multiple background images

Post by mayjune2004 » Fri May 07, 2010 6:57 pm

Hello, I read the documentation and could not find a way to get done what I need. I searched on "background image" and then on "multiple" in the forum and could not find any existing post on point.

I want to use 2 background images on my chart -- one image in the lower right and a different image in the top half of chart.
The calls to:
plot.setBackgroundImage(image);
plot.setBackgroundImageAlignment(org.jfree.ui.Align.BOTTOM_RIGHT);

do not seem to account for 2 images in the same graph. Is that right?

Does anyone know of a workaround I can use to get this done?

Edit: I just found the ImageTitle in the API so I am going to try that. If anyone has an example, that'd be great. I searched the jfreechart sample code and did not find it.

Edit: Solved. Easier than I thought!

Here is the code to help someone else:

Code: Select all

        try {
            File file = new File("small_img.png");
            Image image = ImageIO.read(file);

            plot.setBackgroundPaint(null);
            plot.setBackgroundImage(image);
            plot.setBackgroundImageAlignment(org.jfree.ui.Align.BOTTOM_RIGHT);
            // 0 is transparent (invisible), 1 is opaque (fully visible)
            plot.setBackgroundImageAlpha(0.4f);
						
            // second image

            file = new File("center_img.png"); 
            image = ImageIO.read(file);
            ImageTitle imageTitle = new ImageTitle(image);
            XYTitleAnnotation annotation = new XYTitleAnnotation(0.5, 0.5, imageTitle);
            plot.addAnnotation(annotation);

        } catch (IOException e) {
            e.printStackTrace();
        }

One problem I found:
Since the second image is a title image and not a background image, the graph (plot) is overlaid by the title image in a small portion of the plot. Not good. Anyone know of a workaround? I may have to give up on this idea.

Mark James

Locked