AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

speed up print(Graphics g, PageFormat pf, int pageIndex)

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Dado
Guest





PostPosted: Sun May 14, 2006 9:07 am    Post subject: speed up print(Graphics g, PageFormat pf, int pageIndex) Reply with quote



I
In a Printable/Pageable class print(Graphics g, PageFormat pf, int
pageIndex) is calling x times as is needed to send graphics to a printer.
It is pretty slow. What I need to change in a print method to get better
printing perforamnce?
Back to top
Steve W. Jackson
Guest





PostPosted: Mon May 15, 2006 7:07 pm    Post subject: Re: speed up print(Graphics g, PageFormat pf, int pageIndex Reply with quote



In article <e46pkj$q0g$1 (AT) ss408 (DOT) t-com.hr>, "Dado" <mario_zupan (AT) inet (DOT) hr>
wrote:

Quote:
I
In a Printable/Pageable class print(Graphics g, PageFormat pf, int
pageIndex) is calling x times as is needed to send graphics to a printer.
It is pretty slow. What I need to change in a print method to get better
printing perforamnce?

There's not anything at all you can do about the fact that this method
will be called some number of times. So your focus is probably best
applied to making sure that the work the method does is as fast as
possible.

In our app, we have an object that implements those two interfaces.
When instantiated (which we do in a thread), it goes through our
underlying data to map the pages to page numbers. Because of the nature
of what we're printing, we can't render each page in advance to a
BufferedImage without having memory issues. Instead, each call to the
print method is used to find the appropriate references and render it at
that time, making it slower than we'd like.

But that's the key area to focus on for improved speed. If your data to
print is not going to break the bank, as it were, in terms of memory
needs, you can keep it handy, much like a double-buffering approach for
a Swing component, and simply draw each page on request to the Graphics
item provided in this method. At the very least, however, you could
render a page and keep it around so that the next call for the same page
number won't have to repeat the work and you could simply draw the same
thing the next time around.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Back to top
Dado
Guest





PostPosted: Tue May 16, 2006 8:07 am    Post subject: Re: speed up print(Graphics g, PageFormat pf, int pageIndex Reply with quote



"Steve W. Jackson" <stevewjackson (AT) charter (DOT) net> je napisao u poruci
interesnoj grupi:stevewjackson-E87424.13263915052006 (AT) individual (DOT) net...
Quote:
In article <e46pkj$q0g$1 (AT) ss408 (DOT) t-com.hr>, "Dado" <mario_zupan (AT) inet (DOT) hr
wrote:

I
In a Printable/Pageable class print(Graphics g, PageFormat pf, int
pageIndex) is calling x times as is needed to send graphics to a
printer.
It is pretty slow. What I need to change in a print method to get better
printing perforamnce?

There's not anything at all you can do about the fact that this method
will be called some number of times. So your focus is probably best
applied to making sure that the work the method does is as fast as
possible.

In our app, we have an object that implements those two interfaces.
When instantiated (which we do in a thread), it goes through our

Do you mean that you put creating printable class:
Print print = new Print();
in a thread, or only job.print() ?


Quote:
underlying data to map the pages to page numbers. Because of the nature
of what we're printing, we can't render each page in advance to a
BufferedImage without having memory issues. Instead, each call to the
print method is used to find the appropriate references and render it at
that time, making it slower than we'd like.

But that's the key area to focus on for improved speed. If your data to
print is not going to break the bank, as it were, in terms of memory
needs, you can keep it handy, much like a double-buffering approach for
a Swing component, and simply draw each page on request to the Graphics
item provided in this method. At the very least, however, you could
render a page and keep it around so that the next call for the same page
number won't have to repeat the work and you could simply draw the same
thing the next time around.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama

I have to print a chart (gantt) that can grow on a dozen pages. So I think
that keeping every page in a BufferedImage will cause an OutOfMemory. So,
probably I don't have any chance to speed my printing.

This is my Printable class (print method):

public class Print extends JPanel implements ActionListener, Printable{
...
public int print(Graphics pg, PageFormat pageFormat,
int pageIndex) throws PrinterException {

Graphics2D g2d = (Graphics2D) pg;
int m_maxNumPage = 7;

if (pageIndex >= m_maxNumPage)
return NO_SUCH_PAGE;

pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());

int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();

int w = poX.intValue();
int h = poY.intValue();

if (w == 0 || h == 0)
return NO_SUCH_PAGE;

int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);
int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);

m_maxNumPage = nCol*nRow;

int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int x = iCol*wPage;
int y = iRow*hPage;
int wImage = Math.min(wPage, w-x);
int hImage = Math.min(hPage, h-y);

drawGantt(g2d);

System.gc();
return PAGE_EXISTS;
}
....
}
Back to top
Steve W. Jackson
Guest





PostPosted: Tue May 16, 2006 8:07 pm    Post subject: Re: speed up print(Graphics g, PageFormat pf, int pageIndex Reply with quote

In article <e4c099$lq8$1 (AT) ss408 (DOT) t-com.hr>, "Dado" <mario_zupan (AT) inet (DOT) hr>
wrote:

Quote:
"Steve W. Jackson" <stevewjackson (AT) charter (DOT) net> je napisao u poruci
interesnoj grupi:stevewjackson-E87424.13263915052006 (AT) individual (DOT) net...
In article <e46pkj$q0g$1 (AT) ss408 (DOT) t-com.hr>, "Dado" <mario_zupan (AT) inet (DOT) hr
wrote:

I
In a Printable/Pageable class print(Graphics g, PageFormat pf, int
pageIndex) is calling x times as is needed to send graphics to a
printer.
It is pretty slow. What I need to change in a print method to get better
printing perforamnce?

There's not anything at all you can do about the fact that this method
will be called some number of times. So your focus is probably best
applied to making sure that the work the method does is as fast as
possible.

In our app, we have an object that implements those two interfaces.
When instantiated (which we do in a thread), it goes through our

Do you mean that you put creating printable class:
Print print = new Print();
in a thread, or only job.print() ?

We create a Thread, which instantiates the object that implements
Printable and Pageable, creates a PrinterJob, and then creates a modal
dialog. The dialog executes job.print() in yet another thread,
monitoring until it's completed and displaying a progress indicator.
This lets the spooling process complete while the user watches, or has a
chance to cancel if desired. If they were to terminate the app while
spooling, then the access to our internal structures needed to render
pages would be lost.

Quote:


underlying data to map the pages to page numbers. Because of the nature
of what we're printing, we can't render each page in advance to a
BufferedImage without having memory issues. Instead, each call to the
print method is used to find the appropriate references and render it at
that time, making it slower than we'd like.

But that's the key area to focus on for improved speed. If your data to
print is not going to break the bank, as it were, in terms of memory
needs, you can keep it handy, much like a double-buffering approach for
a Swing component, and simply draw each page on request to the Graphics
item provided in this method. At the very least, however, you could
render a page and keep it around so that the next call for the same page
number won't have to repeat the work and you could simply draw the same
thing the next time around.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama

I have to print a chart (gantt) that can grow on a dozen pages. So I think
that keeping every page in a BufferedImage will cause an OutOfMemory. So,
probably I don't have any chance to speed my printing.

This is my Printable class (print method):

public class Print extends JPanel implements ActionListener, Printable{
...
public int print(Graphics pg, PageFormat pageFormat,
int pageIndex) throws PrinterException {

Graphics2D g2d = (Graphics2D) pg;
int m_maxNumPage = 7;

if (pageIndex >= m_maxNumPage)
return NO_SUCH_PAGE;

pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());

int wPage = (int)pageFormat.getImageableWidth();
int hPage = (int)pageFormat.getImageableHeight();

int w = poX.intValue();
int h = poY.intValue();

if (w == 0 || h == 0)
return NO_SUCH_PAGE;

int nCol = Math.max((int)Math.ceil((double)w/wPage), 1);
int nRow = Math.max((int)Math.ceil((double)h/hPage), 1);

m_maxNumPage = nCol*nRow;

int iCol = pageIndex % nCol;
int iRow = pageIndex / nCol;
int x = iCol*wPage;
int y = iRow*hPage;
int wImage = Math.min(wPage, w-x);
int hImage = Math.min(hPage, h-y);

drawGantt(g2d);

System.gc();
return PAGE_EXISTS;
}
...
}

You should not rely on the System.gc() call to do much for you.

The key to your problem's solution probably lies in the drawGantt method
you don't show. It has no information about what page it's being asked
to print, or at least none that's apparent to me in this example. But
given that printing in Java uses a banded approach, where the above
method could get called more than once for the same value of pageIndex,
your opportunity for savings of memory and time lies there.

My printing code uses the pageIndex value to look up a particular page
from pre-built maps, where it finds out the title and other information.
It also uses that to get a reference to another data structure that has
to be rendered onto the provided Graphics2D. I've now got code that
remembers the pageIndex value immediately before returning PAGE_EXISTS,
and checks against it on a subsequent call. If it's a match, I know
that the BufferedImage where I drew the current page's image before is
still good, so I simply draw it onto the provided g2d. If not, I clear
the BufferedImage and draw to it. The savings are relatively minor for
me, but it depends on how your code draws that image.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
Back to top
Dado
Guest





PostPosted: Wed May 17, 2006 8:07 am    Post subject: Re: speed up print(Graphics g, PageFormat pf, int pageIndex Reply with quote

My drawGantt method look like this:
public void drawGantt(Graphics2D g2d) {

Graphics komponentPart;

AffineTransform original =g2d.getTransform();

g2d.translate(xFaktor.intValue(), yFaktor.intValue());
g2d.scale(zoomFaktor.doubleValue()/2,zoomFaktor.doubleValue()/2);//zoomFaktor.doubleValue()


int visZaglavlja = drvo.getRowHeight();

g2d.setColor(Color.blue);
g2d.setFont(new Font(pismoStampati, Font.PLAIN, 23));
g2d.drawString(new String("gradili¹te: "+imeGradilistaPrihvat),50,50);
g2d.setFont(new Font(pismoStampati, Font.PLAIN, 14));
g2d.drawString(new String("voditelj gradili¹ta:
___________________"),50,80);

g2d.drawString(new String("Dana: "+
new java.util.Date(System.currentTimeMillis()).toString()),50,120);

//tree
komponentPart =
g2d.create(30,10+naslov,treePrihvat.getWidth(),treePrihvat.getHeight());
bufferPaint(treePrihvat, komponentPart);


komponentPart =
g2d.create(30+treePrihvat.getWidth(),10+naslov,tablicaPrihvat.getWidth(),zaglavljePrihvat.getHeight());

//header of a JTable
bufferPaint(zaglavljePrihvat, komponentPart);


komponentPart =
g2d.create(30+treePrihvat.getWidth(),10+naslov+zaglavljePrihvat.getHeight(),tablicaPrihvat.getWidth
(),tablicaPrihvat.getHeight());

//table
bufferPaint(tablicaPrihvat, komponentPart);


komponentPart.dispose();


g2d.translate(30+treePrihvat.getWidth()+tablicaPrihvat.getWidth(),20+naslov-zaglGrafa);

//the next methods are drawing gantt.
//It counts coordinates(according to a data from JTable) and
draws fill3DRect.

if (grafikon.prekidacDnMjPreview == true) {
grafikon.iscrtajMjerilo(g2d);

} else {
grafikon.iscrtajMjeriloMjesecno(g2d);
}


}

I got huge chart which I need to cut on a page and I will with the counting
of a Xaxis and Yaxis variables and use it in a code below, but I have one
question for you and I hope it is a last one to not to drag you any more
with my stupid question and also with the bad english.Smile. So if you be so
kind to look in a code bellow, which is rough draft, and see if I shot the
point you tryijng to describe to me.

BufferedImage bimg2 ;
....
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {

Graphics2D g2d = (Graphics2D) g;


g2d.translate((-(int)pf.getImageableWidth())*Xaxis,-(int)pf.getImageableHeight()*Yaxis);
g2d.setClip(((int)pf.getImageableWidth())*Xaxis,(int)pf.getImageableHeight()*Yaxis,(int)pf.getImageableWidth(),(int)
pf.getImageableHeight());

//radovi trenutni
int postojiDaNe = 0;

if (pageIndex > numberOfPages) {
curIndex = 0;
postojiDaNe = Printable.NO_SUCH_PAGE;
} else {

if (currIndex == pageIndex) {
bimg2 = new BufferedImage(xSizeAsPage,ySizeAsPage,
BufferedImage.TYPE_BYTE_GRAY);
g2d = bimg2.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0,xSizeAsPage,ySizeAsPage);
drawGantt(g2d);
curIndex = curIndex+1;
} else {
// probably dispose buffer if isn't null and

buffer = (Image)bimg2;

//Does this will speed up the printing ???
g2d.drawImage(buffer,0,0,this);




}


System.gc();
postojiDaNe = Printable.PAGE_EXISTS;
}

return postojiDaNe;


}
Back to top
Mahesh
Guest





PostPosted: Wed May 17, 2006 2:07 pm    Post subject: Re: speed up print(Graphics g, PageFormat pf, int pageIndex) Reply with quote

Hi,
You can try disable the doublebuffer if u r feelin that the perfomance
is low in printing the printer.

Thanks,
Maheshkumar
Back to top
Dado
Guest





PostPosted: Wed May 17, 2006 8:07 pm    Post subject: Re: speed up print(Graphics g, PageFormat pf, int pageIndex) Reply with quote

"Mahesh" <registered.here (AT) gmail (DOT) com> je napisao u poruci interesnoj
grupi:1147873324.006142.314570 (AT) 38g2000cwa (DOT) googlegroups.com...
Quote:
Hi,
You can try disable the doublebuffer if u r feelin that the perfomance
is low in printing the printer.

Thanks,
Maheshkumar


I did for a JTree and JTable:
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(grafika);
enableDoubleBuffering(componentToBePrinted);

But how and does it works for a g.drawLine, 3DRectangles etc. I thought
that it is usefull only with the GUI components.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.