| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1385 人关注过本帖
标题:求用java关于打印机的代码
只看楼主 加入收藏
lyflyg
Rank: 1
来 自:山西
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-3
收藏
 问题点数:0 回复次数:1 
求用java关于打印机的代码
本人小菜鸟,刚来到这个大家庭,先提个问题吧!!
    想做个C/S的系统,其中有个打印的功能。连接针式打印机。哪位大侠回答这个问题。帮帮忙咯
搜索更多相关主题的帖子: 打印机 java 代码 
2008-06-03 22:15
伊人遗梦
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-27
收藏
得分:0 
import   java.awt.*;   
  import   java.awt.event.*;   
  import   java.awt.font.*;   
  import   java.awt.geom.*;   
  import   java.awt.print.*;   
  import   java.util.*;   
  import   javax.swing.*;   
   
  public   class   PrintTest   
  {     public   static   void   main(String[]   args)   
        {     JFrame   frame   =   new   PrintTestFrame();   
              frame.show();   
        }   
  }   
   
  class   PrintTestFrame   extends   JFrame   
        implements   ActionListener   
  {     public   PrintTestFrame()   
        {     setTitle("PrintTest");   
              setSize(300,   300);   
              addWindowListener(new   WindowAdapter()   
                    {     public   void   windowClosing(WindowEvent   e)   
                          {     System.exit(0);   
                          }   
                    }   );   
   
              Container   contentPane   =   getContentPane();   
              canvas   =   new   PrintPanel();   
              contentPane.add(canvas,   "Center");   
   
              JPanel   buttonPanel   =   new   JPanel();   
              printButton   =   new   JButton("Print");   
              buttonPanel.add(printButton);   
              printButton.addActionListener(this);   
   
              pageSetupButton   =   new   JButton("Page   setup");   
              buttonPanel.add(pageSetupButton);   
              pageSetupButton.addActionListener(this);   
   
              contentPane.add(buttonPanel,   "North");   
        }   
   
        public   void   actionPerformed(ActionEvent   event)   
        {     Object   source   =   event.getSource();   
              if   (source   ==   printButton)   
              {     PrinterJob   printJob   =   PrinterJob.getPrinterJob();   
                    if   (pageFormat   ==   null)   
                          pageFormat   =   printJob.defaultPage();   
                    printJob.setPrintable(canvas,   pageFormat);   
                    if   (printJob.printDialog())   
                    {     try   
                          {     printJob.print();   
                          }   
                          catch   (PrinterException   exception)   
                          {     JOptionPane.showMessageDialog(this,   exception);   
                          }   
                    }   
              }   
              else   if   (source   ==   pageSetupButton)   
              {     PrinterJob   printJob   =   PrinterJob.getPrinterJob();   
                    if   (pageFormat   ==   null)   
                          pageFormat   =   printJob.defaultPage();   
                    pageFormat   =   printJob.pageDialog(pageFormat);   
              }   
        }   
   
        private   JButton   printButton;   
        private   JButton   pageSetupButton;   
   
        private   PrintPanel   canvas;   
        private   PageFormat   pageFormat;   
  }   
   
  class   PrintPanel   extends   JPanel   
        implements   Printable   
  {     public   void   paintComponent(Graphics   g)   
        {     super.paintComponent(g);   
              Graphics2D   g2   =   (Graphics2D)g;   
              drawPage(g2);   
        }   
   
        public   int   print(Graphics   g,   PageFormat   pf,   int   page)   
              throws   PrinterException   
        {     if   (page   >=   1)   return   Printable.NO_SUCH_PAGE;   
              Graphics2D   g2   =   (Graphics2D)g;   
              g2.setPaint(Color.black);   
              g2.translate(pf.getImageableX(),   pf.getImageableY());   
              g2.draw(new   Rectangle2D.Double(0,   0,   
                    pf.getImageableWidth(),   pf.getImageableHeight()));   
   
              drawPage(g2);   
              return   Printable.PAGE_EXISTS;   
        }   
   
        public   void   drawPage(Graphics2D   g2)   
        {     FontRenderContext   context   =   g2.getFontRenderContext();   
              Font   f   =   new   Font("Serif",   Font.PLAIN,   72);   
              GeneralPath   clipShape   =   new   GeneralPath();   
   
              TextLayout   layout   =   new   TextLayout("2426打印指南",   f,   context);   
              AffineTransform   transform   
                    =   AffineTransform.getTranslateInstance(0,   72);   
              Shape   outline   =   layout.getOutline(transform);   
              clipShape.append(outline,   false);   
   
              layout   =   new   TextLayout("thank you",   f,   context);   
              transform   
                    =   AffineTransform.getTranslateInstance(0,   144);   
              outline   =   layout.getOutline(transform);   
              clipShape.append(outline,   false);   
   
              g2.draw(clipShape);   
              g2.clip(clipShape);   
   
              final   int   NLINES   =50;   
              Point2D   p   =   new   Point2D.Double(0,   0);   
              for   (int   i   =   0;   i   <   NLINES;   i++)   
              {     double   x   =   (2   *   getWidth()   *   i)   /   NLINES;   
                    double   y   =   (2   *   getHeight()   *   (NLINES   -   1   -   i))   
                          /   NLINES;   
                    Point2D   q   =   new   Point2D.Double(x,   y);   
                    g2.draw(new   Line2D.Double(p,   q));   
              }   
        }   
  }
2008-06-27 14:47
快速回复:求用java关于打印机的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021644 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved