Example usage for javax.swing JEditorPane paint

List of usage examples for javax.swing JEditorPane paint

Introduction

In this page you can find the example usage for javax.swing JEditorPane paint.

Prototype

public void paint(Graphics g) 

Source Link

Document

Invoked by Swing to draw components.

Usage

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

/**
 * Convert a html code to an image/*  w w w  .j a v a2s .  co  m*/
 * 
 * @param html html to convert
 * @return html converted to png
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
protected byte[] htmlToImage(String html) throws IOException, PPTGeneratorException {
    try {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);
        editor.setSize(editor.getPreferredSize());
        editor.addNotify();
        LOGGER.debug("Panel is built");
        BufferedImage bufferSave = new BufferedImage(editor.getPreferredSize().width,
                editor.getPreferredSize().height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bufferSave.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, editor.getPreferredSize().width, editor.getPreferredSize().height);
        editor.paint(g);
        LOGGER.debug("graphics is drawn");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferSave, "png", out);
        return out.toByteArray();
    } catch (HeadlessException e) {
        LOGGER.error("X Server no initialized or -Djava.awt.headless=true not set !");
        throw new PPTGeneratorException("X Server no initialized or -Djava.awt.headless=true not set !");
    }
}

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.PPTData.java

/**
 * Add an image with a html code without change its dimension
 * /*from  ww w.  j  a  v a  2  s.c o m*/
 * @param slideToSet slide to set
 * @param html html code
 * @param x horizontal position
 * @param y vertical position
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
protected void addHtmlPicture(Slide slideToSet, String html, int x, int y)
        throws IOException, PPTGeneratorException {
    try {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        if (!ge.isHeadlessInstance()) {
            LOGGER.warn("Runtime is not configured for supporting graphiv manipulation !");
        }
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText(html);
        LOGGER.debug("Editor pane is built");
        editor.setSize(editor.getPreferredSize());
        editor.addNotify(); // Serveur X requis
        LOGGER.debug("Panel rendering is done");
        BufferedImage bufferSave = new BufferedImage(editor.getPreferredSize().width,
                editor.getPreferredSize().height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bufferSave.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, editor.getPreferredSize().width, editor.getPreferredSize().height);
        editor.paint(g);
        LOGGER.debug("graphics is drawn");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferSave, "png", out);
        LOGGER.debug("image is written");
        addPicture(slideToSet, out.toByteArray(),
                new Rectangle(x, y, editor.getPreferredSize().width, editor.getPreferredSize().height));
        LOGGER.debug("image is added");
    } catch (HeadlessException e) {
        LOGGER.error("X Server no initialized or -Djava.awt.headless=true not set !");
        throw new PPTGeneratorException("X Server no initialized or -Djava.awt.headless=true not set !");
    }
}

From source file:net.yacy.cora.util.Html2Image.java

/**
 * render a html page with a JEditorPane, which can do html up to html v 3.2. No CSS supported!
 * @param url//from   ww  w.j  a va 2  s . c  o  m
 * @param size
 * @throws IOException 
 */
public static void writeSwingImage(String url, Dimension size, File destination) throws IOException {

    // set up a pane for rendering
    final JEditorPane htmlPane = new JEditorPane();
    htmlPane.setSize(size);
    htmlPane.setEditable(false);
    final HTMLEditorKit kit = new HTMLEditorKit() {

        private static final long serialVersionUID = 1L;

        @Override
        public Document createDefaultDocument() {
            HTMLDocument doc = (HTMLDocument) super.createDefaultDocument();
            doc.setAsynchronousLoadPriority(-1);
            return doc;
        }

        @Override
        public ViewFactory getViewFactory() {
            return new HTMLFactory() {
                @Override
                public View create(Element elem) {
                    View view = super.create(elem);
                    if (view instanceof ImageView) {
                        ((ImageView) view).setLoadsSynchronously(true);
                    }
                    return view;
                }
            };
        }
    };
    htmlPane.setEditorKitForContentType("text/html", kit);
    htmlPane.setContentType("text/html");
    htmlPane.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
        }
    });

    // load the page
    try {
        htmlPane.setPage(url);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // render the page
    Dimension prefSize = htmlPane.getPreferredSize();
    BufferedImage img = new BufferedImage(prefSize.width, htmlPane.getPreferredSize().height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = img.getGraphics();
    htmlPane.setSize(prefSize);
    htmlPane.paint(graphics);
    ImageIO.write(img, destination.getName().endsWith("jpg") ? "jpg" : "png", destination);
}