0

A piece of data is to be sent to the printer, and it consists of an image and some text on it. I call the Graphic2D drawImage(), passing to it a BufferedImage which the function scales down to fit the printer imageable area, and also call the drawString() serveral times. The problem is, some of the text printed "behind" the image and hence invisible!
Since drawImage() process includes a time taking scale-down, and I have no access to the PageFormat and the imageable area before I'm inside the print(), the drawString()s are taking place before the drawImage().
Maybe the solution is an ImageObserver, but I don't know how it can help in this case. Please help!

edit: The problem looks like this:

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException

    final Graphics2D g = (Graphics2D) graphics;

    g.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    int width = (int) pageFormat.getImageableWidth();
    int height = (int) pageFormat.getImageableHeight() / 2 - 10;

    g.drawImage(img, 0, 0, width, height, null);

    g.drawString("Some text 1", 20, 20);
    for (int i = 0; i < someList.size(); i++)
        g.drawString("Some text 2", 20, 40 + i * 15); // This is overshadowed by the image...!
    .
    .
    .
}
smr
  • 33
  • 6
  • May you should consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Aug 03 '15 at 01:00
  • An `ImageObserver` isn't going to help you here, you have no way to instruct the Print API to perform another pass, instead, you should probably rescale the image manually and cache the result, so when the Print API requests another pass of the page, you don't have to go through the scaling operations again. Something like [this](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) demonstrates a possible means for scaling images (which has a better output result then the graphics API) – MadProgrammer Aug 03 '15 at 01:02
  • So, I did a super quick test, printing to a PDF albeit, and that seemed to work. I still think you should cache the result of the scaling operation for subsequent cycles, but that's me – MadProgrammer Aug 03 '15 at 01:26
  • A quick test may be insufficient! The problem is somehow a matter of random!! It falls behind in some case and foreside in another! Anyhow, your solution can help, I think... Thank you! – smr Aug 03 '15 at 01:41

0 Answers0