annotationcomponentfactory.java

来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 115 行

JAVA
115
字号
// $Id: AnnotationComponentFactory.java,v 1.5 2007/05/22 15:26:02 mike Exp $package org.faceless.pdf2.viewer2;import javax.swing.*;import java.awt.*;import java.awt.image.BufferedImage;import org.faceless.pdf2.*;/** * A type of ViewerFeature that creates a {@link JComponent} to represent a * {@link PDFAnnotation} on the page. Typically AnnotationComponentFactories * are singleton objects, as they do not need to be tied to a specific viewer. * <p><i>This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.</i></p> * @since 2.8 */public abstract class AnnotationComponentFactory extends ViewerFeature{    /**     * Create a new AnnotationComponentFactory     * @param name the name of this ViewerFeature     */    public AnnotationComponentFactory(String name) {        super(name);    }    public String toString() {        return "AnnotationComponentFactory:"+super.toString();    }    /**     * Return true if this AnnotationComponentFactory could create a {@link JComponent}     * for the specified {@link PDFAnnotation}.     */    public abstract boolean matches(PDFAnnotation annot);    /**     * Return a JComponent that will visually represent the specified PDFAnnotation.     * @param pagepanel the panel the JComponent will be added to     * @param annot the annotation     */    public abstract JComponent createComponent(PagePanel pagepanel, PDFAnnotation annot);    /**     * This method can be called by the <code>paintComponent</code> method     * of each JComponent returned from an <code>AnnotationComponentFactory</code>     * to repaint the annotation using the content of the annotation.     * @param comp the component     * @param state the state to paint. Typically this will be "N"     * @param g the Graphics object     * @see PagePainter#paintAnnotation     */    protected static final void paintComponent(JComponent comp, String state, Graphics g) {        Graphics2D g2 = (Graphics2D)g;//            g2.setPaint(java.awt.Color.red);//            g2.draw(new java.awt.Rectangle(0, 0, comp.getWidth()-1, comp.getHeight()-1));        BufferedImage image = (BufferedImage)comp.getClientProperty("image."+state);        if (image==null) image = (BufferedImage)comp.getClientProperty("image.N");        if (image!=null) {            g.drawImage(image, 0, 0, null);        }    }    /**     * Cause the specified JComponent to be positioned at the PDF co-ordinates     * specified by <code>rect</code>.     * If the page is scrolled or zoomed the component will be zoomed and resized to match.     * @param component the Component     * @param rect the component location in PDF space, specified as [x1, y1, x2, y2]     */    public static final void bindComponentLocation(JComponent component, float[] rect) {        if (component!=null) component.putClientProperty("pdf.rect", rect);    }    /**     * Cause the specified JComponent to be positioned at the specified PDF co-ordinates.     * If the page is scrolled or zoomed the component will be zoomed and resized to match.     * @param component the Component     * @param x1 the left-most X co-ordinate of the component in PDF space     * @param y1 the bottom-most Y co-ordinate of the component in PDF space     * @param x2 the right-most X co-ordinate of the component in PDF space     * @param y2 the top-most Y co-ordinate of the component in PDF space     */    public static final void bindComponentLocation(JComponent component, float x1, float y1, float x2, float y2) {        bindComponentLocation(component, new float[] { x1, y1, x2, y2 });    }    /**     * Cause the specified JComponent to be positioned at same position as the PDFAnnotation.     * If the page is scrolled or zoomed the component will be zoomed and resized to match.     * @param component the Component     * @param annot the PDFAnnotation to bind the components position to     */    public static final void bindComponentLocation(JComponent component, PDFAnnotation annot) {        if (component!=null) component.putClientProperty("pdf.annotation", annot);    }    private static final AnnotationComponentFactory DEFAULT = new AnnotationComponentFactory("Default") {        public boolean matches(PDFAnnotation annot) {            return true;        }        public JComponent createComponent(PagePanel pagepanel, PDFAnnotation annot) {            return new JPanel() {                public void paintComponent(Graphics g) {                     AnnotationComponentFactory.paintComponent(this, "N", g);                }            };        }    };    static AnnotationComponentFactory getDefaultFactory() {        return DEFAULT;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?