还是写点东西吧

来源:互联网 发布:客服平台软件 编辑:程序博客网 时间:2024/05/01 10:48

本来今天做的事不是很多的,但想了想还是写一点点东西吧

今天做什么了,脑子真乱现在.啊   好象下午过后没有作实质性开发的东西,上午只是认真的作好了要交上去的文档,弄的我都恶心了,现在做的只是让人反感,其实困难的还在后面,就是现在梦作的的那部分,有很多的JAVA语句还没有整太明白 ,所以 为了赶进度,真的要.......呵呵  希望我的电脑能给我最大的支持,呵呵,这样我才能...哈哈

今天读了一端关于打印机的代码,虽说还不能自己写,也不是了解的很透彻  但还是希望能和来这里的人共享这段代码吧,这段代码是网上一兄弟提供的  不是我写的啊  就一起来学一下吧,算是关于打印机比较标准的了

 

/*
 * Main.java
 *
 * Created on 2007年9月22日, 上午10:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package javaapplication4;

  /**  
    *   @version   1.00   1999-09-11  
    *   @author   Cay   Horstmann  
    */  
   
  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   Main  
  {     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("Hello",   f,   context);  
              AffineTransform   transform  
                    =   AffineTransform.getTranslateInstance(0,   72);  
              Shape   outline   =   layout.getOutline(transform);  
              clipShape.append(outline,   false);  
   
              layout   =   new   TextLayout("World",   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));  
              }  
        }  
  } 

 

我只改动了一小点点,"版权"是网上那哥们的啊   哈哈  共同学一下吧  呵呵

加油!

原创粉丝点击