import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.JobAttributes;
import java.awt.PageAttributes;
import java.awt.PrintJob;
import java.util.ArrayList;
import javax.swing.JFrame;
/**
* Class for printing-methods.
* <pre>Example:
* Printer printer = new Printer();
* printer.addString("Text to Print");
* printer.addBlankLine();
* printer.addTab();
* printer.addString("More Text to print");
* printer.printPage(this, "nix");
* </pre>
*/
public class Printer {
private String[] data;
private ArrayList ar = null;
private Font myFont = new Font("SansSerif",Font.PLAIN, 12);
/** Creates a new instance of Printer */
public Printer() {
}
/**
* Adds a string to the text that gets printed
* when you perform printPage().
* @param text Text to add
*/
public void addString(String text) {
if (ar == null) {
ar = new ArrayList();
ar.add(text);
}
else {
ar.add(text);
}
}
/**
* Adds a blank line to the text that gets
* printed when you perform printPage();
*/
public void addBlankLine() {
if(ar != null) ar.add("\n");
}
/**
* Adds a tab to the text that gets
* printed when you perform printPage();
*/
public void addTab() {
if(ar != null) ar.add("\t");
}
/**
* If you want to print a text perform this.
* @param f A frame/panel/container. Normally you can use this.
* @param title The titel of the printjob.
*/
void printPage(JFrame f, String title) {
if(f == null)
f = new JFrame();
PageAttributes page = new PageAttributes();
PrintJob prjob = f.getToolkit().getPrintJob(f, title, new JobAttributes(),page);
if (prjob != null) {
final int iScreenResol = f.getToolkit().getScreenResolution();
final int iPageResol = prjob.getPageResolution();
final Dimension dimScreenSize = f.getToolkit().getScreenSize();
final Dimension dimPageDimension = prjob.getPageDimension();
Graphics pg = prjob.getGraphics();
if (pg != null && iPageResol > 0) {
int iAddY = pg.getFontMetrics(this.myFont).getHeight();
int iBorder = (int) Math.round(iPageResol * 2. / 2.54);
int iPosX = iBorder + iBorder / 4;
int iPosY = iPosX - iAddY / 2;
int iWdth = dimPageDimension.width - iBorder * 2;
int iMidY = dimPageDimension.height / 2;
pg.setFont(myFont);
if (ar != null) {
data = (String[])ar.toArray(new String[ar.size()]);
for(int x= 0; x!= data.length; x++) {
if(data[x].equals("\n")) {
iPosY += (iAddY * 0.65); //nicht die komplette Höhe nehmen
}
else if(data[x].equals("\t")) {
iPosX += (iAddY * 2 /3 ) * 10;
if(iPosX > iWdth) {
iPosX = iBorder + iBorder / 4;
iPosY += iAddY;
}
else
iPosY -= iAddY;
}
else {
String str[] = seperateString(data[x],pg.getFontMetrics(myFont),true,iWdth);
for(int y = 0; str != null && y != str.length; y++) {
if( y > 0)
iPosY += (iAddY * 0.65);
pg.drawString( str[y], iPosX, iPosY += iAddY);
}
}
}
ar = null;
}
// If you want to print an empty text an error gets printed
else {
pg.drawString("Error, not initialized", iPosX, iPosY += iAddY);
}
pg.dispose();
}
prjob.end();
}
}
/**
* Method for trimming strings to adept output to screen.
* @param strText Your text
* @param fontMet Your font
* @param wrapword Use true if you want words to be seperated
* @param iWidth The width of the sheet
* @return String[]
*/
private String[] seperateString(String strText,FontMetrics fontMet,boolean wrapword,int iWidth) {
ArrayList myTmp = new ArrayList();
int z = 0;
for(int x = 0,y = 0; x != strText.length(); x++) {
y += fontMet.charWidth(strText.charAt(x));
if( y > iWidth) {
y = 0;
x--;
if(wrapword)
x = strText.lastIndexOf(" ",x) + 1;
myTmp.add(strText.substring(z,x));
z = x;
}
}
myTmp.add(strText.substring(z,strText.length()));
return (String[]) myTmp.toArray(new String[myTmp.size()]);
}
/**
* Method for setting the font
* to print with.
* @param font Font (Arial, Helvetica...)
*/
public void setFont(Font font) {
this.myFont = font;
}
}