Printing Graphics to a 8.5 X 11
My program....
There is a blank white page of a 8.5 X 11 paper with a 3X4 grid on it.
There is a user area to create grids on the right side. SO basically 2 JPanels EAST and WEST.
When the user clicks the "ADD" button it adds the current grid to one of the 3X4 units.
There is a print button - when u click print it prints basically what you see on the left side minus a few grid lines and indention.
**** MY QUESTION *****
Since the left side is just a preview it is also scalable to the user. Which means if I copy that Graphics object from the preview to the Printable print method. It is hard to scale it right for print.
So I figure I would make one central reference graphic object and copy from that. The problem is I cannot use a Graphic object that hasnt actualyl been painted on screen. How can I do this?
here is reduced SSCCE version of code showing when I remove (comment out) the mainGraphic the scaled preview on the right doesnt show anymore.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.print.*;
public class TransferGraphics {
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
MainGraphic mainGraphic = new MainGraphic();
PrintPreview printPreview = new PrintPreview();
TransferGraphics() {
mainGraphic.setPreferredSize(new Dimension(850, 750));
printPreview.setPreferredSize(new Dimension(400, 640));
//mainPanel.add(mainGraphic, BorderLayout.WEST);
mainPanel.add(printPreview, BorderLayout.EAST);[COLOR="Red"]How can I display this without displaying mainGraphic first?[/COLOR]
mainPanel.setBackground(Color.darkGray);
frame.add(mainPanel);
frame.setSize(1280, 760);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class MainGraphic extends JPanel {
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 850, 1100);
g.setColor(Color.RED);
g.fillRect(0, 0, 50, 50);
g.fillRect(55, 0, 50, 50);
g.fillRect(0, 55, 50, 50);
g.fillRect(55, 55, 50, 50);
g.setColor(Color.BLACK);
g.fillRect(800, 0, 50, 50);
g.setColor(Color.BLUE);
g.fillRect(800, 1050, 50, 50);
g.setColor(Color.GREEN);
g.fillRect(0, 1050, 50, 50);
}
}
public class PrintPreview extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale(.4, .4);
mainGraphic.print(g);
}
}
// MAIN METHOD
public static void main(String[] args) {
TransferGraphics tg = new TransferGraphics();
}
}