Search in sources :

Example 1 with MMGraphics

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics in project netbeans-mmd-plugin by raydac.

the class MindMapPanel method renderMindMapAsImage.

@Nullable
public static BufferedImage renderMindMapAsImage(@Nonnull final MindMap model, @Nonnull final MindMapPanelConfig cfg, final boolean expandAll, @Nonnull final RenderQuality quality) {
    final MindMap workMap = new MindMap(model, null);
    workMap.resetPayload();
    if (expandAll) {
        MindMapUtils.removeCollapseAttr(workMap);
    }
    final Dimension2D blockSize = calculateSizeOfMapInPixels(workMap, null, cfg, expandAll, quality);
    if (blockSize == null) {
        return null;
    }
    final BufferedImage img = new BufferedImage((int) blockSize.getWidth(), (int) blockSize.getHeight(), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = img.createGraphics();
    final MMGraphics gfx = new MMGraphics2DWrapper(g);
    try {
        quality.prepare(g);
        gfx.setClip(0, 0, img.getWidth(), img.getHeight());
        layoutFullDiagramWithCenteringToPaper(gfx, workMap, cfg, blockSize);
        drawOnGraphicsForConfiguration(gfx, cfg, workMap, false, null);
    } finally {
        gfx.dispose();
    }
    return img;
}
Also used : MMGraphics2DWrapper(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper) Dimension2D(java.awt.geom.Dimension2D) MMGraphics(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics) BufferedImage(java.awt.image.BufferedImage) Nullable(javax.annotation.Nullable)

Example 2 with MMGraphics

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics in project netbeans-mmd-plugin by raydac.

the class Utils method renderWithTransparency.

@Nonnull
public static Image renderWithTransparency(final float opacity, @Nonnull final AbstractElement element, @Nonnull final MindMapPanelConfig config, @Nonnull final RenderQuality quality) {
    final AbstractElement cloned = element.makeCopy();
    final Rectangle2D bounds = cloned.getBounds();
    final float increase = config.safeScaleFloatValue(config.getElementBorderWidth() + config.getShadowOffset(), 0.0f);
    final int imageWidth = (int) Math.round(bounds.getWidth() + increase);
    final int imageHeight = (int) Math.round(bounds.getHeight() + increase);
    bounds.setRect(0.0d, 0.0d, bounds.getWidth(), bounds.getHeight());
    final BufferedImage result = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            result.setRGB(x, y, 0);
        }
    }
    final Graphics2D g = result.createGraphics();
    final MMGraphics gfx = new MMGraphics2DWrapper(g);
    try {
        quality.prepare(g);
        cloned.doPaint(gfx, config, false);
    } finally {
        gfx.dispose();
    }
    int alpha;
    if (opacity <= 0.0f) {
        alpha = 0x00;
    } else if (opacity >= 1.0f) {
        alpha = 0xFF;
    } else {
        alpha = Math.round(0xFF * opacity);
    }
    alpha <<= 24;
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            final int curAlpha = result.getRGB(x, y) >>> 24;
            if (curAlpha == 0xFF) {
                result.setRGB(x, y, (result.getRGB(x, y) & 0xFFFFFF) | alpha);
            } else if (curAlpha != 0x00) {
                final int calculated = Math.round(curAlpha * opacity) << 24;
                result.setRGB(x, y, (result.getRGB(x, y) & 0xFFFFFF) | calculated);
            }
        }
    }
    return result;
}
Also used : AbstractElement(com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement) MMGraphics2DWrapper(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper) Rectangle2D(java.awt.geom.Rectangle2D) MMGraphics(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics) BufferedImage(java.awt.image.BufferedImage) Nonnull(javax.annotation.Nonnull)

Example 3 with MMGraphics

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics in project netbeans-mmd-plugin by raydac.

the class MindMapPanel method calculateSizeOfMapInPixels.

@Nullable
public static Dimension2D calculateSizeOfMapInPixels(@Nonnull final MindMap model, @Nullable final Graphics2D graphicsContext, @Nonnull final MindMapPanelConfig cfg, final boolean expandAll, @Nonnull final RenderQuality quality) {
    final MindMap workMap = new MindMap(model, null);
    workMap.resetPayload();
    Graphics2D g = graphicsContext;
    if (g == null) {
        BufferedImage img = new BufferedImage(32, 32, cfg.isDrawBackground() ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB);
        g = img.createGraphics();
    }
    final MMGraphics gfx = new MMGraphics2DWrapper(g);
    quality.prepare(g);
    Dimension2D blockSize = null;
    try {
        if (calculateElementSizes(gfx, workMap, cfg)) {
            if (expandAll) {
                final AbstractElement root = assertNotNull((AbstractElement) assertNotNull(workMap.getRoot()).getPayload());
                root.collapseOrExpandAllChildren(false);
                calculateElementSizes(gfx, workMap, cfg);
            }
            blockSize = assertNotNull(layoutModelElements(workMap, cfg));
            final double paperMargin = cfg.getPaperMargins() * cfg.getScale();
            blockSize.setSize(blockSize.getWidth() + paperMargin * 2, blockSize.getHeight() + paperMargin * 2);
        }
    } finally {
        gfx.dispose();
    }
    return blockSize;
}
Also used : MMGraphics2DWrapper(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper) Dimension2D(java.awt.geom.Dimension2D) MMGraphics(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics) BufferedImage(java.awt.image.BufferedImage) Nullable(javax.annotation.Nullable)

Example 4 with MMGraphics

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics in project netbeans-mmd-plugin by raydac.

the class AbstractElement method doPaint.

public final void doPaint(@Nonnull final MMGraphics g, @Nonnull final MindMapPanelConfig cfg, final boolean drawCollapsator) {
    final MMGraphics gfx = g.copy();
    try {
        if (this.hasChildren() && !isCollapsed()) {
            doPaintConnectors(g, isLeftDirection(), cfg);
        }
        final Rectangle clip = g.getClipBounds();
        if (clip == null) {
            gfx.translate(this.bounds.getX(), this.bounds.getY());
            drawComponent(gfx, cfg, drawCollapsator);
        } else if (clip.intersects(this.bounds)) {
            gfx.translate(this.bounds.getX(), this.bounds.getY());
            drawComponent(gfx, cfg, drawCollapsator);
        }
    } finally {
        gfx.dispose();
    }
}
Also used : Rectangle(java.awt.Rectangle) MMGraphics(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)

Example 5 with MMGraphics

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics in project netbeans-mmd-plugin by raydac.

the class MindMapPanel method updateElementsAndSizeForGraphics.

public boolean updateElementsAndSizeForGraphics(@Nullable final Graphics2D graph, final boolean enforce, final boolean doListenerNotification) {
    boolean result = true;
    if (enforce || !isValid()) {
        if (lockIfNotDisposed()) {
            try {
                if (graph != null) {
                    final MMGraphics gfx = new MMGraphics2DWrapper(graph);
                    if (calculateElementSizes(gfx, this.model, this.config)) {
                        Dimension pageSize = getSize();
                        final Container parent = this.getParent();
                        if (parent != null) {
                            if (parent instanceof JViewport) {
                                pageSize = ((JViewport) parent).getExtentSize();
                            }
                        }
                        changeSizeOfComponent(layoutFullDiagramWithCenteringToPaper(gfx, this.model, this.config, pageSize), doListenerNotification);
                        result = true;
                        fireNotificationComponentElementsLayouted(graph);
                    }
                }
            } finally {
                unlock();
            }
        }
    }
    return result;
}
Also used : MMGraphics2DWrapper(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper) MMGraphics(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)

Aggregations

MMGraphics (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)6 MMGraphics2DWrapper (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper)4 BufferedImage (java.awt.image.BufferedImage)4 Dimension2D (java.awt.geom.Dimension2D)2 Nullable (javax.annotation.Nullable)2 MindMap (com.igormaznitsa.mindmap.model.MindMap)1 HasOptions (com.igormaznitsa.mindmap.plugins.api.HasOptions)1 MindMapPanelConfig (com.igormaznitsa.mindmap.swing.panel.MindMapPanelConfig)1 AbstractElement (com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement)1 Rectangle (java.awt.Rectangle)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Nonnull (javax.annotation.Nonnull)1