I need to program a fairly straight forward print preview
function (in java) for a program i am writing.
There are two parts - how to scale the graphics for printing +
the actual presentation of the graphics in a print preview
I am a competent java programmer so explanation of the gui is not
necessary.
These are the specifics of how it should work:
I have a class "MakeDrawing(Graphics g)". it is passed a Graphics g
(NOT Graphics2d) and draws onto it. Usually the drawing will be 1028*768
but it could be smaller (or possibly larger i guess).
I need to know how to manipulate this picture - i am NOT competent with
graphics2d and need to know how to use it - especially to "scale to
fit" this for an 8.5 * 11 page with BOTH landscape
and portrait functionality.
The print preview gui will be as follows.
A Jinternal frame.
Two radio buttons - landscape, portrait
2 textfields for x and y scaling
a button for scale to fit (it will set those 2 x and y textfields appropraitely
a check box for constrain proportions for scale to fit
a button for print
and a JPanel (340 by 440 - 40 pixels per inch) that will contain the
print preview image.
I do not want any of that gui implementation. I mentioned it
so you will have an idea of the functionality required and the
JPanel size used and get a general idea of how this will all
come together.
So this is what i need -
the implementation of that JPanel that will display what the printed
page will look like.
and the methods to manipulate the image for those desired features for
printing. to draw the image call my Make.Drawing(g) class and method.
i have a fit to page and printing method written now but it doesnt
look good when printed and i dont really fully understand whats going on.
I need to understand printing space versus pixels. What imageable space is.
how to calculate and understand how a graphics object is displayed on
the printed page.
This is EXTREMELY time critical. if $40 is not enough im willing to
pay more for a fast and explanatory answer.
This is important. Im looking forward to getting a good answer.
-------------
this is the method i have so far
i got it off the internet
it is not important to keep this.
i just wanted to give an example of what i had.
completely scarp this or rewrite it as necessary.
public int print(Graphics g, PageFormat format, int pageIndex) {
format.setOrientation(format.PORTRAIT);
Graphics2D g2D = (Graphics2D) g;
g2D.translate(format.getImageableX (), format.getImageableY ());
disableDoubleBuffering(this);
// scale to fill the page
double dw = format.getImageableWidth();
double dh = format.getImageableHeight();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double xScale = dw / (double)( PS.plen+150 );
double yScale = dh / (double)( PS.pht+50 );
double scale = Math.min(xScale,yScale);
g2D.scale( xScale, yScale);
// for constrain proportions it would have been
// g2D.scale (scale, scale) - the minimum of xscale and yscale
Make.Drawing( g);
enableDoubleBuffering(this);
return Printable.PAGE_EXISTS;
}
public void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
} |