沙漠中的魚

          欲上天堂,先下地獄
          posts - 0, comments - 56, trackbacks - 0, articles - 119
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          JAVA AWT簡單打印程序

          Posted on 2008-03-15 22:35 沙漠中的魚 閱讀(814) 評論(0)  編輯  收藏 所屬分類: 控件開發

              以前用SWT 3232包的在WINDOWS下開發的打印程序,在linux下的包沒有實現打印功能,不過最新的eclipse3.3下面已經實現了打印功能,但是需要linux2.10才可以,但是我們采用共創linux的內核只有2.06,只好改用AWT來實現,在網上找到一段簡單的打印程序

            1import java.awt.*;
            2import java.awt.print.*;
            3import java.io.*;
            4import java.util.Vector;
            5
            6public class PageableText implements Pageable, Printable {
            7  // Constants for font name, size, style and line spacing
            8  public static String FONTFAMILY = "Monospaced";
            9  public static int FONTSIZE = 10;
           10  public static int FONTSTYLE = Font.PLAIN;
           11  public static float LINESPACEFACTOR = 1.1f;
           12
           13  PageFormat format;   // The page size, margins, and orientation
           14  Vector lines;        // The text to be printed, broken into lines
           15  Font font;           // The font to print with
           16  int linespacing;     // How much space between lines
           17  int linesPerPage;    // How many lines fit on a page
           18  int numPages;        // How many pages required to print all lines
           19  int baseline = -1;   // The baseline position of the font.
           20
           21  /** Create a PageableText object for a string of text */
           22  public PageableText(String text, PageFormat format) throws IOException 
           23    this(new StringReader(text), format); 
           24  }

           25
           26  /** Create a PageableText object for a file of text */
           27  public PageableText(File file, PageFormat format) throws IOException 
           28    this(new FileReader(file), format); 
           29  }

           30
           31  /** Create a PageableText object for a stream of text */
           32  public PageableText(Reader stream, PageFormat format) throws IOException {
           33    this.format = format;
           34
           35    // First, read all the text, breaking it into lines.
           36    // This code ignores tabs, and does not wrap long lines.
           37    BufferedReader in = new BufferedReader(stream);
           38    lines = new Vector();
           39    String line;
           40    while((line = in.readLine()) != null
           41      lines.addElement(line);
           42
           43    // Create the font we will use, and compute spacing between lines
           44    font = new Font(FONTFAMILY, FONTSTYLE, FONTSIZE);
           45    linespacing = (int) (FONTSIZE * LINESPACEFACTOR);
           46
           47    // Figure out how many lines per page, and how many pages
           48    linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
           49    numPages = (lines.size()-1)/linesPerPage + 1;
           50  }

           51
           52  // These are the methods of the Pageable interface.
           53  // Note that the getPrintable() method returns this object, which means
           54  // that this class must also implement the Printable interface.
           55  public int getNumberOfPages() return numPages; }
           56  public PageFormat getPageFormat(int pagenum) return format; }
           57  public Printable getPrintable(int pagenum) return this; }
           58
           59  /**
           60   * This is the print() method of the Printable interface.
           61   * It does most of the printing work.
           62   */

           63  public int print(Graphics g, PageFormat format, int pagenum) {
           64    // Tell the PrinterJob if the page number is not a legal one.
           65    if ((pagenum < 0| (pagenum >= numPages)) 
           66      return NO_SUCH_PAGE;
           67
           68    // First time we're called, figure out the baseline for our font.
           69    // We couldn't do this earlier because we needed a Graphics object
           70    if (baseline == -1{
           71      FontMetrics fm = g.getFontMetrics(font);
           72      baseline = fm.getAscent();
           73    }

           74
           75    // Clear the background to white.  This shouldn't be necessary, but is
           76    // required on some systems to workaround an implementation bug
           77    g.setColor(Color.white);
           78    g.fillRect((int)format.getImageableX(), (int)format.getImageableY(),
           79               (int)format.getImageableWidth(), 
           80               (int)format.getImageableHeight());
           81
           82    // Set the font and the color we will be drawing with.
           83    // Note that you cannot assume that black is the default color!
           84    g.setFont(font);
           85    g.setColor(Color.black);
           86
           87    // Figure out which lines of text we will print on this page
           88    int startLine = pagenum * linesPerPage;
           89    int endLine = startLine + linesPerPage - 1;
           90    if (endLine >= lines.size()) 
           91      endLine = lines.size()-1;
           92
           93    // Compute the position on the page of the first line.
           94    int x0 = (int) format.getImageableX();
           95    int y0 = (int) format.getImageableY() + baseline;
           96    System.out.print("x:"+x0+",y:"+y0);
           97    // Loop through the lines, drawing them all to the page.
           98    for(int i=startLine; i <= endLine; i++{
           99      // Get the line
          100      String line = (String)lines.elementAt(i);
          101
          102      // Draw the line.
          103      // We use the integer version of drawString(), not the Java 2D
          104      // version that uses floating-point coordinates. A bug in early
          105      // Java2 implementations prevents the Java 2D version from working.
          106      if (line.length() > 0
          107        g.drawString(line, x0, y0);
          108
          109      // Move down the page for the next line.
          110      y0 += linespacing;  
          111    }

          112    g.setColor(Color.red);
          113    g.drawString("test"2020);
          114    // Tell the PrinterJob that we successfully printed the page.
          115    return PAGE_EXISTS;
          116  }

          117
          118  /**
          119   * This is a test program that demonstrates the use of PageableText
          120   */

          121  public static void main(String[] args) throws IOException, PrinterException {
          122    // Get the PrinterJob object that coordinates everything
          123    PrinterJob job = PrinterJob.getPrinterJob();
          124
          125    // Get the default page format, then ask the user to customize it.
          126    PageFormat format = job.pageDialog(job.defaultPage());
          127
          128    
          129    System.out.println("height:"+format.getHeight()+",mHeight:"+format.getImageableHeight());
          130    // Create our PageableText object, and tell the PrinterJob about it
          131    job.setPageable(new PageableText(new File("PageableText.java"), format));
          132
          133    // Ask the user to select a printer, etc., and if not canceled, print!
          134    if (job.printDialog()) 
          135      job.print();
          136  }

          137}

          138
          139
          主站蜘蛛池模板: 沈阳市| 涞源县| 定兴县| 蒙山县| 芜湖市| 峨山| 绥棱县| 思南县| 格尔木市| 南陵县| 元阳县| 县级市| 两当县| 松桃| 张家口市| 安泽县| 民勤县| 龙陵县| 临沭县| 诸暨市| 屏东市| 驻马店市| 黎平县| 伊川县| 黑山县| 嘉义县| 湖口县| 淳化县| 望奎县| 海晏县| 县级市| 寻乌县| 五指山市| 塔河县| 佛山市| 青铜峡市| 三河市| 淄博市| 上栗县| 宿松县| 灯塔市|