java简单打印

没什么,就是一个简单的打印,留作存档

 

 

public class PrintTest implements Printable {

	public static void main(String[] args) {
		PrinterJob job = PrinterJob.getPrinterJob();
		job.setJobName("Print Test!");
		try {
			PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			job.printDialog(pras);
			job.setPrintable(new PrintTest());
			job.print(pras);
		} catch (PrinterException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int print(Graphics g, PageFormat pf, int index)
			throws PrinterException {
		if (index == 0) {
			int x = (int) (pf.getImageableX() + pf.getImageableWidth() / 2);
			int y = (int) (pf.getImageableY() + pf.getImageableHeight() / 2);
			g.setColor(Color.RED);
			g.drawLine(x, y, x + 10, y + 10);
			g.drawString("Hello", x, y);
			return PAGE_EXISTS;
		} else {
			return NO_SUCH_PAGE;
		}
	}
}

你可能感兴趣的:(java)