Need some help with 2D Brinting please :)
First I made a test file to see if I can get something to print. That file works correctly and prints. Then I made Receipt.java to print stuff from my project, but the text doesn't print and I don't understand what's the difference between the test file and Receipt.java :confused:. Also, the print output from Receipt.java was a weird line of tiny glyphs at the top of the page which included stuff like these: '' ``.
Receipt.java takes a String array and paragraph width, formats the graphic and then prints it. The test file PrintPage.java does the same thing but from command-line, but it doesn't take the paragraph width.
//Receipt.java
Code:
package ozzypos;
import java.awt.*;
import java.awt.font.*;
import java.awt.print.*;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
public class Receipt implements Printable {
String msg = "";
float breakWidth;
int paragraphStart, paragraphEnd;
LineBreakMeasurer lineMeasurer = null;
AttributedString as = null;
AttributedCharacterIterator paragraph = null;
FontRenderContext frc = null;
float drawPosX, drawPosY;
Receipt(String[] printMsg, float mmWidth) {
for (String s:printMsg) msg += s;
System.out.println(msg);
breakWidth = mmToPixel(mmWidth);
}
private float mmToPixel(float mm) {
float pixels = ((mm/10f)/2.54f)*72f;
return pixels;
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) return NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
as = new AttributedString(msg);
paragraph = as.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
frc = g2d.getFontRenderContext();
lineMeasurer = new LineBreakMeasurer(paragraph, frc);
drawPosY = 100;
lineMeasurer.setPosition(paragraphStart);
while (lineMeasurer.getPosition() < paragraphEnd) {
TextLayout layout = lineMeasurer.nextLayout(breakWidth);
drawPosX = layout.isLeftToRight()? 0 : breakWidth - layout.getAdvance();
drawPosY += layout.getAscent();
layout.draw(g2d, drawPosX, drawPosX);
drawPosY += layout.getDescent() + layout.getLeading();
}
return PAGE_EXISTS;
}
public void doPrint() {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
//boolean ok = job.printDialog();
boolean ok = true;
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}
}
//Method from another file to print receipt
Code:
...
String[] xReportArray = new String[xStrings.size()];
xStrings.toArray(xReportArray);
new Receipt(xReportArray, 80).doPrint();
//The test file which works correctly (command-line)
Code:
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.print.*;
import java.text.*;
import javax.swing.*;
public class PrintPage implements Printable, ActionListener {
public static float breakWidth = 80;
public static String msg = "Hello World";
AttributedString as = null;
AttributedCharacterIterator paragraph = null;
int paragraphStart, paragraphEnd;
FontRenderContext frc = null;
LineBreakMeasurer lineMeasurer = null;
float drawPosX, drawPosY;
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) return NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
as = new AttributedString(msg);
paragraph = as.getIterator();
paragraphStart = paragraph.getBeginIndex();
paragraphEnd = paragraph.getEndIndex();
frc = g2d.getFontRenderContext();
lineMeasurer = new LineBreakMeasurer(paragraph, frc);
drawPosY = 0;
lineMeasurer.setPosition(paragraphStart);
while (lineMeasurer.getPosition() < paragraphEnd) {
TextLayout layout = lineMeasurer.nextLayout(breakWidth);
drawPosX = layout.isLeftToRight()? 0 : breakWidth - layout.getAdvance();
drawPosY += layout.getAscent();
layout.draw(g2d, drawPosX, drawPosY);
drawPosY += layout.getDescent() + layout.getLeading();
}
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
//boolean ok = job.printDialog();
boolean ok = true;
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}
public static void main(String args[]) {
String printMsg = "";
for (String s:args) printMsg += (s + " ");
msg = printMsg;
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame("Hello World Printer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JButton printButton = new JButton("Print Hello World");
printButton.addActionListener(new PrintPage());
f.add("Center", printButton);
f.pack();
f.setVisible(true);
}
}