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

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

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

    沙漠中的魚

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

    JAVA AWT簡單打印程序

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

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

      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
    主站蜘蛛池模板: 国产免费伦精品一区二区三区| 亚洲小说图片视频| 国产成人亚洲精品电影| 最近中文字幕免费mv视频7| 亚洲毛片在线免费观看| 亚洲乱码中文字幕综合| 水蜜桃视频在线观看免费| 日本高清免费不卡在线| 妇女自拍偷自拍亚洲精品| 国产精品冒白浆免费视频| 国产亚洲成在线播放va| 亚洲人成电影网站国产精品| 亚洲一区在线视频| 免费看国产精品3a黄的视频| 亚洲综合中文字幕无线码| 天天干在线免费视频| 牛牛在线精品免费视频观看| 青青视频观看免费99| 亚洲精品无码高潮喷水在线| 国产亚洲免费的视频看| 亚洲美女在线国产| 男女一边桶一边摸一边脱视频免费| 亚洲中文字幕无码一久久区| 无码少妇精品一区二区免费动态| 亚洲国产成人久久一区久久 | 亚洲午夜久久久久久久久久| 你是我的城池营垒免费观看完整版| 国产成人免费一区二区三区| 污污视频免费观看网站| 亚洲va国产va天堂va久久| 91成年人免费视频| 色视频在线观看免费| 亚洲av网址在线观看| 成年女人免费v片| 国产黄色片免费看| 亚洲视频在线一区二区三区| 久久福利青草精品资源站免费 | 99在线视频免费观看视频 | 亚洲高清专区日韩精品| 中国内地毛片免费高清| 久久精品国产精品亚洲毛片|