开发者

Scaling diagrams in Java to fit one print page

I have a diagramming tool that uses Java and Graphics2D. I would like to scale the resulting diagram so it fits exactly into an 8.5 x 11 page (landscape) when printing.

I saw somewhere that page scaling is in 72ths of an inch, but I'm not sure how this relates to screen's dpi. To make things more complex, my screen is working at 144dpi, but most users of the package will be using 72dpi or 96dpi (not sure which...?). Has anyone experience at managing all these dpi's, including printing?

Using the java.awt.print package I c开发者_开发问答an print my diagram, but they are too big, using several pages. I can scale them using Graphics.scale(double, double), but how do I calculate the scaling factor so the diagrams fit on one page? And is Graphics.scale(double, double) the right tool anyway?


I found this code around the web. I think it fixes your problem.

Graphics2D g2d = (Graphics2D)g; 
//Scale the component to fit    
//Calculate the scale factor to fit the window to the page.
double scaleX = pageFormat.getImageableWidth();
double scaleY = pageFormat.getImageableHeight();
double scale = Math.min(scaleX,scaleY);  //Get minimum scale factor

I hope it helps you. Link to the web

Regards!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜