Search in sources :

Example 1 with MMGraphics2DWrapper

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper 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 MMGraphics2DWrapper

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper 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 MMGraphics2DWrapper

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper 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 MMGraphics2DWrapper

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

the class MindMapPanel method paintComponent.

@Override
@SuppressWarnings("unchecked")
public void paintComponent(@Nonnull final Graphics g) {
    if (this.lockIfNotDisposed()) {
        try {
            final Graphics2D gfx = (Graphics2D) g.create();
            try {
                final String error = this.errorText;
                this.getConfiguration().getRenderQuality().prepare(gfx);
                if (error != null) {
                    drawErrorText(gfx, this.getSize(), error);
                } else {
                    if (this.model.getRoot().getPayload() == null) {
                        updateElementsAndSizeForGraphics(gfx, true, true);
                    }
                    drawOnGraphicsForConfiguration(new MMGraphics2DWrapper(gfx), this.config, this.model, true, this.selectedTopics);
                    drawDestinationElement(gfx, this.config);
                }
                paintChildren(g);
                if (this.draggedElement != null) {
                    this.draggedElement.draw(gfx);
                } else if (this.mouseDragSelection != null) {
                    gfx.setColor(COLOR_MOUSE_DRAG_SELECTION);
                    gfx.fill(this.mouseDragSelection.asRectangle());
                }
            } finally {
                gfx.dispose();
            }
        } finally {
            this.unlock();
        }
    }
}
Also used : MMGraphics2DWrapper(com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper)

Example 5 with MMGraphics2DWrapper

use of com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper 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

MMGraphics2DWrapper (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics2DWrapper)5 MMGraphics (com.igormaznitsa.mindmap.swing.panel.ui.gfx.MMGraphics)4 BufferedImage (java.awt.image.BufferedImage)3 Dimension2D (java.awt.geom.Dimension2D)2 Nullable (javax.annotation.Nullable)2 AbstractElement (com.igormaznitsa.mindmap.swing.panel.ui.AbstractElement)1 Rectangle2D (java.awt.geom.Rectangle2D)1 Nonnull (javax.annotation.Nonnull)1