Reputation: 11
I wanna print my jpanel through thermal printer. But when I print the panel, output's font doesn't match with panel font. Panel has JLabels and images . I've sent the code below. If you can help, I will really appreciate !
public static class Printer implements Printable {
final Component comp;
public Printer(Component comp) {
this.comp = comp;
}
@Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}
}
Upvotes: 1
Views: 114
Reputation: 57381
You have to set the desired font to all the child components. Set the font to all the labels because they don't use parent's font.
Upvotes: 1