<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks
    import java.awt.*;
    import java.awt.print.*;
    import java.io.*;

    /**
    * Utility class to print some lines of text to the default printer. Uses some
    * default font settings, and gets the page size from the PrinterJob object.
    *
    * Note: this little example class does not handle pagination. All the text must
    * fit on a single page.
    *
    * This class can also be used as a standalone utility. If the main method is
    * invoked, it reads lines of text from System.in, and prints them to the
    * default printer.
    */

    public class PrintGIF implements Printable {
    /**
    * Default font size, 12 point
    */
    public static final int DEFAULT_FONT_SIZE = 12;

    /**
    * Default type name, Serif
    */
    public static final String DEFAULT_FONT_NAME = "Serif";

    private PrinterJob job;

    private String typeName;

    private int typeSize;

    private Font typeFont;

    private Font typeFontBold;

    private String[] header;

    private String[] body;

    /**
    * Create a TextPrinter object with the default type font and size.
    */
    public PrintGIF() {
    this(DEFAULT_FONT_NAME, DEFAULT_FONT_SIZE);
    }

    /**
    * Create a TextPrinter object ready to print text with a given font and
    * type size.
    */
    public PrintGIF(String name, int size) {
    if (size < 3 || size > 127) {
    throw new IllegalArgumentException("Type size out of range");
    }
    typeName = name;
    typeSize = size;
    typeFont = new Font(typeName, Font.PLAIN, typeSize);
    typeFontBold = new Font(typeName, Font.BOLD, typeSize);
    job = null;
    }

    /**
    * Initialize the printer job.
    */
    protected void init() {
    job = PrinterJob.getPrinterJob();
    }

    /**
    * Initialize the print job, and return the base number of characters per
    * line with the established font size and font. This is really just a
    * guess, because we can't get the font metrics yet.
    */
    public int getCharsPerLine() {
    if (job == null) {
    init();
    }
    PageFormat pf;
    pf = job.defaultPage();
    double width = pf.getImageableWidth(); // in 72nd of a pt
    double ptsize = typeFont.getSize();
    double ptwid = ptsize * 3 / 4;
    double cnt = (width / ptwid);
    return (int) (Math.round(cnt));
    }

    /**
    * Print some text. Headers are printed first, in bold, followed by the body
    * text, in plain style. If the boolean argument interactive is set to true,
    * then the printer dialog gets shown.
    *
    * Either array may be null, in which case they are treated as empty.
    *
    * This method returns true if printing was initiated, or false if the user
    * cancelled printer. This method may throw PrinterException if printing
    * could not be started.
    */
    public boolean doPrint(String[] header, String[] body, boolean interactive)
    throws PrinterException {
    if (job == null) {
    init();
    }
    if (interactive)
    try {
    if (job.printDialog()) {
    // we are going to print
    } else {
    // we are not going to print
    return false;
    }
    } catch (Exception pe) {
    System.err.println("Could not pop up print dialog");
    // assume user wants to print anyway...
    }

    job.setPrintable(this);
    this.header = header;
    this.body = body;
    job.print();
    job = null; // we are no longer initialized
    return true;
    }

    /**
    * Perform printing according to the Java printing model. NEVER CALL THIS
    * DIRECTLY! It will be called by the PrinterJob as necessary. This method
    * always returns Printable.NO_SUCH_PAGE for any page number greater than 0.
    */
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
    throws PrinterException {
    if (pageIndex != 0) {
    return NO_SUCH_PAGE;
    }
    FontMetrics fm;
    graphics.setFont(typeFont);
    graphics.setColor(Color.black);
    fm = graphics.getFontMetrics();

    // fill in geometric and rendering guts here
    int i;
    double x, y;
    x = pageFormat.getImageableX();
    y = pageFormat.getImageableY() + fm.getMaxAscent();

    // do the headings
    if (header != null) {
    graphics.setFont(typeFontBold);
    for (i = 0; i < header.length; i++) {
    graphics.drawString(header[i], (int) x, (int) y);
    y += fm.getHeight();
    }
    }

    // do the body
    if (body != null) {
    graphics.setFont(typeFont);
    for (i = 0; i < body.length; i++) {
    graphics.drawString(body[i], (int) x, (int) y);
    y += fm.getHeight();
    }
    }

    return PAGE_EXISTS;
    }

    /**
    * Main method for testing. This main method sets up a header of
    * "PRINTER TEST" and reads System.in to get body text.
    */
    public static void main(String[] args) {
    BufferedReader br;
    java.util.List lines;

    TextPrinter tp;
    tp = new TextPrinter();
    lines = new java.util.ArrayList();
    try {
    InputStreamReader isr;
    isr = new InputStreamReader(System.in);
    br = new BufferedReader(isr);
    String line;
    for (line = br.readLine(); line != null; line = br.readLine()) {
    lines.add(line);
    }
    br.close();

    System.out.println("chars per line: " + tp.getCharsPerLine());
    System.out.println("attempting to print...");
    String[] headers = new String[1];
    headers[0] = "PRINT TEST";
    String[] body = new String[lines.size()];
    for (int ix = 0; ix < lines.size(); ix++) {
    body[ix] = (String) (lines.get(ix));
    }
    boolean didit = tp.doPrint(headers, body, true);
    System.out.println("doPrint returns " + didit);
    } catch (Exception e) {
    System.err.println("Error printing: " + e);
    e.printStackTrace();
    }
    }
    }














    posted on 2008-08-05 17:13 jinfeng_wang 閱讀(274) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久国产亚洲精品无码| 亚洲精品无码久久| 成年男女免费视频网站 | 成全视成人免费观看在线看| 亚洲产国偷V产偷V自拍色戒| 最新中文字幕电影免费观看| 一级一看免费完整版毛片| 亚洲精品白色在线发布| 亚洲成AV人网址| 亚洲一区二区三区免费在线观看| 国产午夜亚洲精品不卡免下载 | 国产精品亚洲а∨无码播放| 皇色在线视频免费网站| 国产精品hd免费观看| 亚洲午夜电影一区二区三区| 伊人久久综在合线亚洲91| 成视频年人黄网站免费视频| 国产精品一区二区三区免费| 99久久国产亚洲综合精品| 亚洲精品无码成人片久久 | 亚洲人成影院在线高清| 国产乱辈通伦影片在线播放亚洲| 免费A级毛片无码无遮挡内射| 中文字幕免费在线观看动作大片| 亚洲字幕AV一区二区三区四区| 亚洲av永久无码精品古装片 | 亚洲AV无码之日韩精品| 美女视频黄的全免费视频 | 亚洲国产一区二区三区| 99精品全国免费观看视频| 91免费国产精品| 拍拍拍无挡视频免费观看1000| 亚洲av乱码一区二区三区按摩| 亚洲熟妇av一区| 亚洲妇熟XXXX妇色黄| 亚洲精品无码激情AV| 日韩成人免费视频播放| 免费看污成人午夜网站| 亚洲免费观看在线视频| 无码专区AAAAAA免费视频| 国产在线国偷精品免费看|