亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? printabledocument.java

?? java下實現可打印文檔實例源碼
?? JAVA
字號:
package weichang;


import java.awt.*;
import java.awt.print.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;

/**
 * This class implements the Pageable and Printable interfaces and allows
 * the contents of any JTextComponent to be printed using the java.awt.print
 * printing API.
 **/
public class PrintableDocument implements Pageable, Printable {
    View root;              // The root View to be printed
    PageFormat format;      // Paper plus page orientation
    double scalefactor;     // How much to scale before printing
    int numPages;           // How many pages in the document
    double printX, printY;  // coordinates of upper-left of print area
    double printWidth;      // Width of the printable area
    double printHeight;     // Height of the printable area
    Rectangle drawRect;     // The rectangle in which the document is painted

    // How lenient are we with the bottom margin in widow/orphan prevention?
    static final double MARGIN_ADJUST = .97;

    // The font we use for printing page numbers
    static final Font headerFont = new Font("Serif", Font.PLAIN, 12);

    /**
     * This constructor allows printing the contents of any JTextComponent
     * using a default PageFormat and a default scale factor.  The default
     * scale factor is .75 because the default fonts are overly large.
     */
    public PrintableDocument(JTextComponent textComponent) {
        this(textComponent, new PageFormat(), .75);
    }

    /**
     * This constructor allows the contents of any JTextComponent to be
     * printed, using any specified PageFormat object and any scaling factor.
     **/
    public PrintableDocument(JTextComponent textComponent, PageFormat format,
                             double scalefactor)
    {
        // Remember the page format, and ask it for the printable area
        this.format = format;
        this.scalefactor = scalefactor;
        this.printX = format.getImageableX()/scalefactor;
        this.printY = format.getImageableY()/scalefactor;
        this.printWidth = format.getImageableWidth()/scalefactor;
        this.printHeight = format.getImageableHeight()/scalefactor;
        double paperWidth = format.getWidth()/scalefactor;
        // Get the document and its root Element from the text component
        Document document = textComponent.getDocument();
        Element rootElement = document.getDefaultRootElement();
        // Get the EditorKit and its ViewFactory from the text component
        EditorKit editorKit =textComponent.getUI().getEditorKit(textComponent);
        ViewFactory viewFactory = editorKit.getViewFactory();

        // Use the ViewFactory to create a root View object for the document
        // This is the object we'll print.
        root = viewFactory.create(rootElement);

        // The Swing text architecture requires us to call setParent() on
        // our root View before we use it for anything.  In order to do this,
        // we need a View object that can serve as the parent.  We use a
        // custom implementation defined below.
        root.setParent(new ParentView(root, viewFactory, textComponent));

        // Tell the view how wide the page is; it has to format itself
        // to fit within this width.  The height doesn't really matter here
        root.setSize((float)printWidth, (float)printHeight);

        // Now that the view has formatted itself for the specified width,
        // Ask it how tall it is.
        double documentHeight = root.getPreferredSpan(View.Y_AXIS);

        // Set up the rectangle that tells the view where to draw itself
        // We'll use it in other methods of this class.
        drawRect = new Rectangle(0, 0, (int)printWidth+1, (int)documentHeight+1);

        // Now if the document is taller than one page, we have to
        // figure out where the page breaks are.
        if (documentHeight > printHeight) paginate(root, drawRect);

        // Once we've broken it into pages, figure out how many pages.
        numPages = pageLengths.size() + 1;
    }

    // This is the starting offset of the page we're currently working on
    double pageStart = 0;

    /**
     * This method loops through the children of the specified view,
     * recursing as necessary, and inserts pages breaks when needed.
     * It makes a rudimentary attempt to avoid "widows" and "orphans".
     **/
    protected void paginate(View v, Rectangle2D allocation) {
        // Figure out how tall this view is, and tell it to allocate
        // that space among its children
        double myheight = v.getPreferredSpan(View.Y_AXIS);
        v.setSize((float)printWidth, (float)myheight);

        // Now loop through each of the children
        int numkids = v.getViewCount();
        for(int i = 0; i < numkids; i++) {
            View kid = v.getView(i);  // this is the child we're working with
            // Figure out its size and location
            Shape kidshape = v.getChildAllocation(i, allocation);
            if (kidshape == null) continue;
            Rectangle2D kidbox = kidshape.getBounds2D();

            // This is the Y coordinate of the bottom of the child
            double kidpos = kidbox.getY() + kidbox.getHeight() - pageStart;

            // If this is the first child of a group, then we want to ensure
            // that it doesn't get left by itself at the bottom of a page.
            // I.e. we want to prevent "widows"
            if ((numkids > 1) && (i == 0)) {
                // If it is not near the end of the page, then just move
                // on to the next child
                if (kidpos < printY + printHeight*MARGIN_ADJUST) continue;

                // Otherwise, the child is near the bottom of the page, so
                // break the page before this child and place this child on
                // the new page.
                breakPage(kidbox.getY());
                continue;
            }

            // If this is the last child of a group, we don't want it to
            // appear by itself at the top of a new page, so allow it to
            // squeeze past the bottom margin if necessary.  This helps to
            // prevent "orphans"
            if ((numkids > 1) && (i == numkids-1)) {
                // If it fits normally, just move on to the next one
                if (kidpos < printY + printHeight) continue;

                // Otherwise, if it fits with extra space, then break the
                // page at the end of the group
                if (kidpos < printY + printHeight/MARGIN_ADJUST) {
                    breakPage(allocation.getY() + allocation.getHeight());
                    continue;
                }
            }

            // If the child is not the first or last of a group, then we use
            // the bottom margin strictly.  If the child fits on the page,
            // then move on to the next child.
            if (kidpos < printY+printHeight) continue;

            // If we get here, the child doesn't fit on this page.  If it has
            // no children, then break the page before this child and continue.
            if (kid.getViewCount() == 0) {
                breakPage(kidbox.getY());
                continue;
            }

            // If we get here, then the child did not fit on the page, but it
            // has kids of its own, so recurse to see if any of those kids
            // will fit on the page.
            paginate(kid, kidbox);
        }
    }

    // For a document of n pages, this list stores the lengths of pages
    // 0 through n-2.  The last page is assumed to have a full length
    ArrayList pageLengths = new ArrayList();

    // For a document of n pages, this list stores the starting offset of
    // pages 1 through n-1.  The offset of page 0 is always 0
    ArrayList pageOffsets = new ArrayList();

    /**
     * Break a page at the specified Y coordinate.  Store the necessary
     * information into the pageLengths and pageOffsets lists
     **/
    void breakPage(double y) {
        double pageLength = y-pageStart-printY;
        pageStart = y-printY;
        pageLengths.add(new Double(pageLength));
        pageOffsets.add(new Double(pageStart));
    }

    /** Return the number of pages. This is a Pageable method.   */
    public int getNumberOfPages() { return numPages; }

    /**
     * Return the PageFormat object for the specified page.  This is a
     * Pageable method. This implementation uses the computed length of the
     * page in the returned PageFormat object.  The PrinterJob will use this
     * as a clipping region, which will prevent extraneous parts of the
     * document from being drawn in the top and bottom margins.
     **/
    public PageFormat getPageFormat(int pagenum) {
        // On the last page, just return the user-specified page format
        if (pagenum == numPages-1) return format;

        // Otherwise, look up the height of this page and return an
        // appropriate PageFormat.
        double pageLength = ((Double)pageLengths.get(pagenum)).doubleValue();
        PageFormat f = (PageFormat) format.clone();
        Paper p = f.getPaper();
        if (f.getOrientation() == PageFormat.PORTRAIT)
            p.setImageableArea(printX*scalefactor, printY*scalefactor,
                               printWidth*scalefactor, pageLength*scalefactor);
        else
            p.setImageableArea(printY*scalefactor, printX*scalefactor,
                               pageLength*scalefactor, printWidth*scalefactor);
        f.setPaper(p);
        return f;
    }

    /**
     * This Pageable method returns the Printable object for the specified
     * page.  Since this class implements both Pageable and Printable, it just
     * returns this.
     **/
    public Printable getPrintable(int pagenum) { return this; }

    /**
     * This is the basic Printable method that prints a specified page
     **/
    public int print(Graphics g, PageFormat format, int pageIndex) {
        // Return an error code on attempts to print past the end of the doc
        if (pageIndex >= numPages) return NO_SUCH_PAGE;

        // Cast the Graphics object so we can use Java2D operations
        Graphics2D g2 = (Graphics2D)g;

        // Translate to accomodate the top and left margins
        g2.translate(format.getImageableX(), format.getImageableY());

        // Scale the page by the specified scaling factor
        g2.scale(scalefactor, scalefactor);

        // Display a page number centered in the area of the top margin.
        // Set a new clipping region so we can draw into the top margin
        // But remember the original clipping region so we can restore it
        if (pageIndex > 0) {
            Shape originalClip = g.getClip();
            g2.setClip(new Rectangle(0, (int)-printY,
                                    (int)printWidth, (int)printY));
            // Compute the header to display, measure it, then display it
            String numString = "- " + (pageIndex+1) + " -";
            // Get string and font measurements
            FontRenderContext frc = g2.getFontRenderContext();
            Rectangle2D numBounds = headerFont.getStringBounds(numString, frc);
            LineMetrics metrics = headerFont.getLineMetrics(numString, frc);
            g2.setFont(headerFont);    // Set the font
            g2.setColor(Color.black);  // Print with black ink
            g2.drawString(numString,   // Display the string
                         (int)((printWidth-numBounds.getWidth())/2),
                         (int)(-(printY-numBounds.getHeight())/2 +
                               metrics.getAscent()));
            g2.setClip(originalClip);  // Restore the clipping region
        }

        // Get the staring position and length of the page within the document
        double pageStart = 0.0, pageLength = printHeight;
        if (pageIndex > 0)
            pageStart = ((Double)pageOffsets.get(pageIndex-1)).doubleValue();
        if (pageIndex < numPages-1)
            pageLength = ((Double)pageLengths.get(pageIndex)).doubleValue();

        // Scroll so that the appropriate part of the document is lined up
        // with the upper-left corner of the page
        g2.translate(0.0, -pageStart);

        // Now paint the entire document.  Because of the clipping region,
        // only the desired portion of the document will actually be drawn on
        // this sheet of paper.
        root.paint(g2, drawRect);

        // Finally return a success code
        return PAGE_EXISTS;
    }
    public void jobprint(){
      PrinterJob job=PrinterJob.getPrinterJob();
      job.setPrintable(this,job.defaultPage());
      if(job.printDialog()){
        try {
          job.print();
        }
        catch (PrinterException ex) {
        }
      }

    }
    /**
     * This inner class is a concrete implementation of View, with a
     * couple of key method implementations.  An instance of this class
     * is used as the parent of the root View object we want to print
     **/
    static class ParentView extends View {
        ViewFactory viewFactory; // The ViewFactory for the hierarchy of views
        Container container;     // The Container for the hierarchy of views

        public ParentView(View v, ViewFactory viewFactory, Container container)
        {
            super(v.getElement());
            this.viewFactory = viewFactory;
            this.container = container;
        }

        // These methods return key pieces of information required by
        // the View hierarchy.
        public ViewFactory getViewFactory() { return viewFactory; }
        public Container getContainer() { return container; }

        // These methods are abstract in View, so we've got to provide
        // dummy implementations of them here, even though they're never used.
        public void paint(Graphics g, Shape allocation) {}
        public float getPreferredSpan(int axis) { return 0.0f; }
        public int viewToModel(float x,float y,Shape a,Position.Bias[] bias) {
            return 0;
        }
        public Shape modelToView(int pos, Shape a, Position.Bias b)
            throws BadLocationException {
            return a;
        }
    }
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美影院精品一区| 亚洲成人精品影院| 无码av中文一区二区三区桃花岛| 国产制服丝袜一区| 色狠狠桃花综合| 欧美日韩中文一区| 国产精品人成在线观看免费 | 中文字幕av不卡| 久久国内精品自在自线400部| 在线免费观看一区| 国产麻豆欧美日韩一区| 在线观看一区不卡| 国产精品538一区二区在线| 日韩亚洲欧美成人一区| 日韩精品午夜视频| 欧美日本精品一区二区三区| 亚洲精品视频在线| 国产精品久久毛片av大全日韩| 福利91精品一区二区三区| 精品久久人人做人人爰| 国产一区二区三区高清播放| 欧美久久久久久蜜桃| 在线亚洲高清视频| 亚洲成av人影院| 欧美视频在线一区| 日韩制服丝袜先锋影音| 欧美精品乱码久久久久久 | 国产成人午夜电影网| 久久久国产精华| 波多野结衣中文字幕一区 | 97精品电影院| 亚洲一区二区视频在线观看| 欧美日韩国产小视频| 玖玖九九国产精品| 国产欧美一区二区精品性色 | 日本美女一区二区| 久久久噜噜噜久噜久久综合| 成人美女在线视频| 亚洲五码中文字幕| 欧美r级在线观看| 国产成人久久精品77777最新版本| 欧美激情在线看| 在线视频一区二区三| 久久精品999| 成人亚洲精品久久久久软件| 亚洲欧洲一区二区在线播放| 欧美另类变人与禽xxxxx| 奇米888四色在线精品| 日韩美女啊v在线免费观看| 成人av在线播放网址| 日韩精彩视频在线观看| 精品国产凹凸成av人导航| 99久久99久久精品免费看蜜桃 | 91在线观看高清| 日韩国产精品久久| 国产精品久久久久久户外露出 | 日本一区二区视频在线| 5566中文字幕一区二区电影| 成人av网址在线| 看电视剧不卡顿的网站| 亚洲黄色免费网站| 国产精品欧美一级免费| 日韩欧美国产一区二区三区 | 成人欧美一区二区三区视频网页 | 日韩欧美一区二区三区在线| 欧美性一级生活| 91在线观看成人| 成人丝袜视频网| 欧美日韩第一区日日骚| 成人av资源网站| 国产成人精品免费看| 美女一区二区久久| 日韩精品国产精品| 蜜臀av一级做a爰片久久| 天天av天天翘天天综合网| 亚洲人精品午夜| 亚洲色图欧洲色图婷婷| 亚洲欧美一区二区久久| 亚洲午夜一区二区| 久久在线免费观看| 欧美一区二区人人喊爽| 精品1区2区3区| heyzo一本久久综合| 成人高清免费观看| 91在线视频在线| 在线免费av一区| 欧美日韩精品免费观看视频| 欧美日韩一区二区三区不卡| 国产高清不卡一区二区| 国产福利一区在线观看| 成人av免费在线观看| 日本精品免费观看高清观看| 欧美日韩亚洲综合一区 | 日韩一区二区三区视频在线 | 日韩国产欧美在线视频| 国产一区中文字幕| 91丨国产丨九色丨pron| 精品视频资源站| 欧美成人精品二区三区99精品| 久久久久99精品国产片| |精品福利一区二区三区| 亚洲国产精品嫩草影院| 久久99国产乱子伦精品免费| av不卡免费在线观看| 欧美精品丝袜久久久中文字幕| 久久综合久久综合久久| 亚洲女子a中天字幕| 免费成人性网站| 色哟哟国产精品| 久久久久久9999| 日韩不卡手机在线v区| av在线不卡网| 久久影院视频免费| 91久久香蕉国产日韩欧美9色| 欧美成人综合网站| 亚洲欧洲日韩av| 精品一区二区日韩| 色嗨嗨av一区二区三区| 国产人成一区二区三区影院| 亚洲图片欧美综合| 丁香六月综合激情| 欧美久久一二三四区| 综合激情成人伊人| 国产高清在线精品| 91精品国产麻豆国产自产在线| 中文字幕一区二区三区色视频| 国产乱国产乱300精品| 欧美xxxxx牲另类人与| 亚洲图片欧美综合| 色吊一区二区三区| 国产精品久久久爽爽爽麻豆色哟哟 | 一区二区三区四区在线免费观看| 国产精品一区不卡| 久久亚洲精品国产精品紫薇| 青娱乐精品在线视频| 3d成人动漫网站| 亚洲第一激情av| 欧美一卡二卡三卡| 男女激情视频一区| 日韩一区二区三区在线观看| 香蕉乱码成人久久天堂爱免费| 欧美日韩另类一区| 午夜av区久久| 精品久久久久久无| 国产成人综合在线| 中文在线一区二区| 在线欧美日韩精品| 日韩avvvv在线播放| 欧美大度的电影原声| 成人午夜免费视频| 一区二区三区日本| 在线播放国产精品二区一二区四区| 日韩国产精品久久久久久亚洲| 2023国产精华国产精品| 不卡免费追剧大全电视剧网站| 亚洲一区在线播放| 久久欧美一区二区| 日本韩国视频一区二区| 日韩**一区毛片| 国产精品电影一区二区三区| 欧美三级电影网| 久久精品国产一区二区三| 国产精品久久精品日日| 欧美一区二区人人喊爽| 成人精品小蝌蚪| 琪琪久久久久日韩精品| 欧美国产国产综合| 日韩一区二区三区三四区视频在线观看 | 欧美亚洲综合另类| 国产成人在线免费| 日本午夜精品视频在线观看| 中文字幕一区二区日韩精品绯色 | 一区二区三区四区乱视频| 精品国产乱码久久久久久老虎| 91麻豆免费看片| 国产成人亚洲综合a∨猫咪| 亚洲va欧美va国产va天堂影院| 国产目拍亚洲精品99久久精品| 911国产精品| 一区二区高清视频在线观看| 国产精品卡一卡二卡三| 精品国产免费视频| 69久久99精品久久久久婷婷| 91视频在线观看免费| 成人app网站| av在线免费不卡| 成人午夜激情片| 国产老女人精品毛片久久| 九九国产精品视频| 蜜桃视频在线一区| 日本一不卡视频| 青娱乐精品视频在线| 五月天亚洲婷婷| 亚洲gay无套男同| 亚洲黄色片在线观看| 亚洲综合久久av| 日本欧美加勒比视频| 玖玖九九国产精品| 国产一区视频导航| 高潮精品一区videoshd|