How to make new-line when p rinting strings?
So i've got my printing java file in place, and working. Now i know that %n in a string doesn't cause the program to create a new-line when the strings are printed.
Obviously there are various methods for sending output to a printer, also depending on the printer type. So here is the method i'm using right now:
//PrintPage.java
The output width is specified with an array of strings to be printed.
This information is accepted in the constructor before being processed to form paragraphs broken after the specified width.
The problem here is that "%n" in the input strings are ineffective with printing - so how do I make a new line when I need one?
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ozzypos;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.print.*;
import java.text.*;
import javax.swing.*;
/**
*
* @author Ozzy
*/
public class PrintPage implements Printable, ActionListener {
public static float breakWidth = 0;
private static float padLeft = 4;
private static float padRight = 4;
public static String msg = "";
private static JDialog f;
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()+mmToPixel(padLeft), 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 static void main(String args[]) {
String printMsg = "";
for (String s:args) printMsg += (s + " ");
msg = printMsg;
UIManager.put("swing.boldMetal", Boolean.FALSE);
new PrintPage();
}
private void dialogWindowOpened(WindowEvent 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 */
}
}
f.setVisible(false);
}
PrintPage() {
initComponents();
}
PrintPage(String[] args, float mmWidth) {
breakWidth = mmToPixel(mmWidth-padRight);
main(args);
}
private void initComponents() {
f = new JDialog();
f.addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e) {dialogWindowOpened(e);}
});
JLabel printLabel = new JLabel("Printing...");
f.setTitle("Printing...");
f.add("Center", printLabel);
f.setModal(true);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private float mmToPixel(float mm) {
float pixels = ((mm/10f)/2.54f)*72f;
return pixels;
}
}