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

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

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

    有才華的人,別忘記給滋潤你的那塊土壤施肥

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      28 隨筆 :: 5 文章 :: 147 評論 :: 0 Trackbacks
     前段時間在學(xué)習(xí)關(guān)于java打印的,瞎忙乎了好久。就是一張關(guān)于股票的數(shù)據(jù)分析表打印出來的結(jié)果A4不能完全顯示,會截取一部分打印不出來,如下圖做了簡單的例子點擊正常打印后的圖有的字不能完全顯示:

          本打算拿得所要打印的panel的畫筆,然后將他轉(zhuǎn)換成Graphics2D,再調(diào)用他的translate(double tx, double ty)scale(double sx, double sy)直接進行坐標(biāo)轉(zhuǎn)移縮放,然后對其打印,但是printJob.getGraphics()不能轉(zhuǎn)換成g2,會拋出sun.print.ProxyPrintGraphics cannot be cast to java.awt.Graphics2D這個異常。在這個異常上花了很長時間,總想一定有什么辦法在上面行的通。但是最后還是放棄。

       最后還是把當(dāng)前的panel畫成圖片存在內(nèi)存中,然后對這張圖片進行縮放處理,下圖是點擊按比例縮小打印后的圖,不過這樣處理后的圖有個問題就是有的字會模糊,縮放比例越小越不清楚,對照上下兩張圖看button上的字就能對比出來,不過對于縮放出來的圖,這種問題也是正常情況。

     下面是源碼:

    package kissJava.print;
    /**
     * 
    @author jxliangby
     * 
    @since  2007.10.14
     * 
    */

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.PageAttributes;
    import java.awt.PrintJob;
    import java.awt.RenderingHints;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.awt.print.PageFormat;
    import java.awt.print.Paper;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.io.IOException;
    import javax.print.PrintService;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class BigPrintExample extends JFrame implements ActionListener{
        
    private JPanel panel = new JPanel();
        
    private JPanel btnPanel = new JPanel();
        
    private JButton normBtn = new JButton("正常打印(打印不完)");
        
    private JButton zoomBtn = new JButton("按比例縮小打印");
        
    private JComponent component = new PaintComponent();
        
        
    public BigPrintExample(){
           
    this.setTitle("Print Test");
           
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
    this.setBounds((int)((SystemProperties.SCREEN_WIDTH - 800/ 2), (int)((SystemProperties.SCREEN_HEIGHT - 600/ 2), 800600);
           initLayout();
        }

        
    //組件布局
        private void initLayout(){
            
    this.getContentPane().setLayout(new BorderLayout());
            btnPanel.add(normBtn);
            btnPanel.add(zoomBtn);
            panel.setLayout(
    new BorderLayout());
            panel.add(component, BorderLayout.CENTER);
            panel.add(btnPanel, BorderLayout.SOUTH);
            
    this.getContentPane().add(panel);
            normBtn.addActionListener(
    this);
            zoomBtn.addActionListener(
    this);
        }

        
        
    public void actionPerformed(ActionEvent e) {  
            
    if(e.getSource() == normBtn)    
                normalPrint();
            
    else    
                zoomInPrint();
            
        }

        
    //截取打印,A4不能完全顯示
        private void normalPrint(){
            
    final PageAttributes pa = new PageAttributes();
            pa.setOrigin(java.awt.PageAttributes.OriginType.PRINTABLE);
            pa.setColor(java.awt.PageAttributes.ColorType.COLOR);
            pa.setMedia(java.awt.PageAttributes.MediaType.A4);
            
    final Toolkit tk = panel.getToolkit();
            
    final Graphics pg = panel.getGraphics();
            
    final Graphics gr = pg.create();
            
    final PrintJob pjob = tk.getPrintJob(
                
    new Frame() {
                     
    public Graphics getGraphics(){
                          
    return gr;
                     }

                }
    "printing pop-up frame"null, pa);

            
    if(pjob != null{
                
    final Graphics2D ppg = (Graphics2D)pjob.getGraphics();
                
    if(ppg != null){
                    panel.printAll(ppg);
    //打印
                     ppg.dispose();
                }

                pjob.end();
            }
     
        }

        
        
    private void zoomInPrint(){
            BufferedImage imag 
    = create(this.getWidth(), this.getHeight());
            BufferedImage   image 
    =null;
            
    try{
                 image 
    = zoomImage(imag, 0.70);  //變成0.8的圖片
                 
    //ImageIO.write(image, "jpg", new File("d:\tt.jpg"));
            }

            
    catch(Exception ed){
                 System.out.println(ed.toString());
             }
     
            PrinterJob pj 
    = PrinterJob.getPrinterJob();    
            PrintService[] services 
    = PrinterJob.lookupPrintServices();
            PageFormat pageFormat 
    = pj.defaultPage();//得到默認(rèn)頁格式
            
    //pageFormat.setOrientation(PageFormat.LANDSCAPE);//橫向打印
            Paper paper = pageFormat.getPaper();
            
    int nExtWidth = (int)paper.getImageableX()*4/5;
            
    int nExtHeight = (int)paper.getImageableY()*2/3;
           
    //設(shè)置頁面顯示大小,如調(diào)邊距等。。。。
           paper.setImageableArea(paper.getImageableX()-nExtWidth, paper.getImageableY()-nExtHeight,
               paper.getImageableWidth()
    +nExtWidth*2, paper.getImageableHeight()+nExtHeight*2);
           pageFormat.setPaper(paper);
           pj.setPrintable(
    new PrintableDemo(image), pageFormat);
           
    if (services.length > 0{
                
    try {
                    pj.setPrintService(services[
    0]);
                    
    if(pj.printDialog()) {
                        pj.print();
                    }

                }
     catch (PrinterException pe) 
                    System.err.println(pe);
                }

            }
     
        }

        
    /**
         * 將這個frame畫成BufferedImage;
         * 
    */

        
    private  BufferedImage create( int width, int height) {
            BufferedImage image 
    = null;
            
    try {    
                image 
    = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                getContentPane().paint(image.getGraphics()); 
            }
     catch (Exception e) {
                System.out.println(
    "ffff = " + e);
            }

            
    return image;
        }

        
    /**
         * 將BufferedImage按比例變換
         * scale為變換比例
         * 
    */

        
    private BufferedImage zoomImage(BufferedImage image, double scale)   throws   IOException  {  
     
            RenderingHints renderingHints 
    =  new  RenderingHints(  
                RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
            AffineTransformOp scaleOp 
    = new AffineTransformOp(  
                    AffineTransform.getScaleInstance(scale, scale), renderingHints);  
            BufferedImage targetImage 
    = new BufferedImage(  
                (
    int)(image.getWidth() * scale),    
                (
    int)(image.getHeight() * scale), image.getType());  
            scaleOp.filter(image, targetImage);  
            
    return   targetImage;  
        }

        
        
    public static void main(String[] args) {
            
    new BigPrintExample().setVisible(true);
        }


        
    class PaintComponent extends JComponent{
            
    private String mString = "Java Source and Support";
            
    private String javaStr = "I Love Java";
            
    private String author = "By jxliangby";
            
    private Font mFont = new Font("Serif", Font.PLAIN, 64);
            
    private  int strX = 50, strY = 150

            
    protected void paintComponent(Graphics g) {
                g.setFont(mFont);
                g.setColor(Color.red);
                g.drawString(mString, strX, strY);
                g.drawString(javaStr, strX , strY 
    + 150);
                g.drawString(author, strX 
    + 400 , strY + 300);
            }
        
        }

        
    /**
         * 實現(xiàn)Printtable,將圖片畫出。
         * 
    */

        
    class PrintableDemo implements Printable {
                Image image ;
    //= new ImageIcon("1.gif").getImage();
                 public PrintableDemo(Image image){
                    
    this.image = image;
                 }

                
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
                    
    if (pageIndex == 0{
                        Graphics2D g2d
    = (Graphics2D)g;
                        g2d.translate(pf.getImageableX(), pf.getImageableY()); 
                        g2d.setColor(Color.red);            
                        g2d.drawImage(image, 
    00null);
                        g2d.setColor(Color.black);
                        
    return Printable.PAGE_EXISTS;                    
                    }
     else {
                        
    return Printable.NO_SUCH_PAGE;
                    }

                }

                
            }

    }

    posted on 2008-03-05 11:23 kissjava 閱讀(1757) 評論(1)  編輯  收藏 所屬分類: swing

    評論

    # re: 處理A4不能打印大圖片 2009-02-04 17:28 laiang
    很棒,謝謝
      回復(fù)  更多評論
      

    主站蜘蛛池模板: 毛片在线免费视频| 亚洲理论片在线中文字幕| 日韩视频免费在线| 国产公开免费人成视频| 国产成A人亚洲精V品无码性色| 色播亚洲视频在线观看| 亚洲综合无码一区二区三区| 全部免费毛片在线播放| 欧美好看的免费电影在线观看| 亚洲伊人精品综合在合线| 特a级免费高清黄色片| 日本高清高色视频免费| 国产精品麻豆免费版| 亚洲色成人网一二三区| 91精品成人免费国产| 成人免费无码大片A毛片抽搐| 亚洲精品国产品国语在线| 亚洲日本天堂在线| 131美女爱做免费毛片| 亚洲国产婷婷综合在线精品| 亚洲欧洲日韩综合| 在线成人精品国产区免费| 亚洲精品无码日韩国产不卡?V| 国产精品偷伦视频免费观看了| 青青久在线视频免费观看| AV激情亚洲男人的天堂国语| 9420免费高清在线视频| 亚洲情XO亚洲色XO无码| 美女被爆羞羞网站在免费观看| 一级女人18毛片免费| 亚洲AV无码一区二区乱子伦| h视频免费高清在线观看| 亚洲尹人九九大色香蕉网站| 四虎影院在线免费播放| 国产成人精品日本亚洲专 | 日韩欧美亚洲中文乱码| 最近中文字幕电影大全免费版| 亚洲色成人WWW永久在线观看| 免费播放一区二区三区| 亚洲精品乱码久久久久蜜桃| 69成人免费视频无码专区|