java applet实现套打
来源:互联网 发布:淘宝编辑宝贝图片尺寸 编辑:程序博客网 时间:2024/05/09 16:15
- import java.awt.print.*;
- import java.awt.*;
- public class PrintTest implements Printable {
- private String[] value = null;
- private int[][] position = null;
-
- public int print(Graphics g, PageFormat pf, int pageIndex) {
-
- System.out.println(pageIndex);
-
- if (pageIndex > 0) {
- return Printable.NO_SUCH_PAGE;
- }
- Graphics2D g2d = (Graphics2D) g;
- for (int i = 0; i < value.length; i++) {
- String str = value[i];
- int[] temp = position[i];
- int x = temp[0];
- int y = temp[1];
- g2d.drawString(str, x, y);
- }
- return Printable.PAGE_EXISTS;
- }
- public static void main(String[] args) {
- printReport();
- }
- public static void printReport() {
- PrinterJob pj = PrinterJob.getPrinterJob();
- PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
- Paper paper = pf.getPaper();
-
- double pageWidth = 480;
- double pageHeight = 640;
- paper.setSize(pageWidth, pageHeight);
- paper.setImageableArea(0, 0, pageWidth, pageHeight);
- pf.setPaper(paper);
-
- PrintTest printTest=new PrintTest();
- printTest.setValue(new String []{"001", "002", "003"});
- printTest.setPosition(new int [][]{{ 10, 50 }, { 30, 70 }, { 50, 90 }});
-
- pj.setPrintable(printTest, pf);
- if (pj.printDialog()) {
- try {
- pj.print();
- } catch (PrinterException e) {
- e.printStackTrace();
- }
- }
- }
-
- public int[][] getPosition() {
- return position;
- }
-
- public void setPosition(int[][] position) {
- this.position = position;
- }
-
- public String[] getValue() {
- return value;
- }
-
- public void setValue(String[] value) {
- this.value = value;
- }
- }