I cannot seem to figure out how to send a text box control to print in Java.
The Program creates word search puzzles, now I would like to print them.
I simply want to print the text Box control as it is... But I had to try other methods as follows.
My first problem is when I attempt to send it txtEasy.getText() it attempts to print one really long string. This is not what I wanted. So I had to take one really long string and parse it out like this. Rows of 15 words by 20. Unfortunately you cant see that here. But it is a 15 by 20 box.
M S N O I T I S O P M O C L G D R B P U D C K S Q G B E I Q C D I N M B P I U N P M J J U T N L I N C V D Q B W I U A U T O R N T F J O D N G T E L G B R K C O M M S W G H T S A N S I G U K X U X S E P I E Q C T G C B O X H J N B D T T J P I I A A R Q Y C S W I F O L M M N R T I A E L P S P D Q O U U A P E H F I T S O F Q N L P L L H D O Q F A O A G T Q A B A S S R W T C L T N P G E N S T X T Y P O V T I E W L Q C F I H S E J F E M L A D N D A E N W K T Q N E I I K I J V S I G F Y H S E E P I Y J U S H R D F V N D R D I B E F S R I U H U Y E V S F O E Q J X V R N V V R G R L G Q S B M F G E J
My code is as follows.
As it is currently setup, I sent the text to the printer class as txtEasy.getText()
The Problem I am now having with this method is the font is not aligned the puzzle correctly.
So my two questions...
1) is there a way to sent the Text Box Control to the printer?
2) is there a way to format a Strings font? The font size is causing my uneven prints.
Thank you for any help.
private void btnEasyPrintActionPerformed(java.awt.event.ActionEvent evt) {
//System.out.println(txtEasy.getText());
PrinterJob job = PrinterJob.getPrinterJob();
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PageFormat pf = job.pageDialog(aset);
job.setPrintable(new PrintPuzzle(txtEasy.getText()), pf);
boolean ok = job.printDialog(aset);
if (ok) {
try {
job.print(aset);
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
//******** THE PRINT CLASS ********
import java.awt.*;
import java.awt.print.*;
public class PrintPuzzle implements Printable
{
String toPrint;
String formatedToPrint;
public PrintPuzzle(String item)
{
toPrint = item;
//toPrint = item;
//formatedToPrint = formatBoardToPrint(toPrint);
}
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException
{
if (page > 0)
{
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
String paintMe = "";
int YPosition = 20;
int first = 2;
int last = 46;
System.out.println(toPrint);
System.out.println("String Length is :" + toPrint.length());
for(int i = 0;i < 20;i++)
{
paintMe = toPrint.substring(first, last);
paintMe.
System.out.print(paintMe + "First Position : " + first + " Last Position : " + last + " YPosition : " + YPosition);
g.drawString(paintMe, 20, YPosition);
System.out.println();
first += 46;
last += 46;
YPosition += 10;
}//end for
//g.drawString(toPrint, 20, 20);
//g.drawString(toPrint, 20, 30);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
}