Search in sources :

Example 96 with Composite

use of java.awt.Composite in project LoboEvolution by LoboEvolution.

the class TextureButtonUI method paintText.

/**
 * {@inheritDoc}
 */
@Override
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
    Graphics2D g2D = (Graphics2D) g;
    Composite savedComposite = g2D.getComposite();
    ButtonModel model = b.getModel();
    FontMetrics fm = JTattooUtilities.getFontMetrics(b, g, b.getFont());
    int mnemIndex = b.getDisplayedMnemonicIndex();
    if (model.isEnabled()) {
        int offs = 0;
        if (model.isArmed() && model.isPressed()) {
            offs = 1;
        }
        Color foreground = b.getForeground();
        Color background = b.getBackground();
        if (background instanceof ColorUIResource) {
            if (model.isPressed() && model.isArmed()) {
                foreground = AbstractLookAndFeel.getTheme().getPressedForegroundColor();
            } else if (model.isRollover()) {
                foreground = AbstractLookAndFeel.getTheme().getRolloverForegroundColor();
            }
        }
        if (AbstractLookAndFeel.getTheme().isTextShadowOn() && ColorHelper.getGrayValue(foreground) > 164) {
            AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
            g2D.setComposite(alpha);
            g.setColor(Color.black);
            JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x + offs, textRect.y + offs + fm.getAscent() + 1);
            g2D.setComposite(savedComposite);
        } else {
            if (!(model.isPressed() && model.isArmed())) {
                Object sc = b.getClientProperty("shadowColor");
                if (sc instanceof Color) {
                    g.setColor((Color) sc);
                    JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x + 1, textRect.y + 1 + fm.getAscent());
                }
            }
        }
        g.setColor(foreground);
        JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x + offs, textRect.y + offs + fm.getAscent());
    } else {
        AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f);
        g2D.setComposite(alpha);
        Color fc = b.getForeground();
        if (ColorHelper.getGrayValue(fc) > 164) {
            fc = ColorHelper.brighter(AbstractLookAndFeel.getDisabledForegroundColor(), 40);
            g.setColor(Color.black);
        } else {
            fc = AbstractLookAndFeel.getDisabledForegroundColor();
            g.setColor(Color.white);
        }
        JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x, textRect.y + 1 + fm.getAscent());
        g2D.setComposite(savedComposite);
        g.setColor(fc);
        JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
    }
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) FontMetrics(java.awt.FontMetrics) AlphaComposite(java.awt.AlphaComposite) Color(java.awt.Color) ButtonModel(javax.swing.ButtonModel) ColorUIResource(javax.swing.plaf.ColorUIResource) Graphics2D(java.awt.Graphics2D)

Example 97 with Composite

use of java.awt.Composite in project LoboEvolution by LoboEvolution.

the class GraphiteMenuUI method paintBackground.

/**
 * {@inheritDoc}
 */
@Override
protected void paintBackground(Graphics g, JComponent c, int x, int y, int w, int h) {
    JMenuItem b = (JMenuItem) c;
    ButtonModel model = b.getModel();
    if (c.getParent() instanceof JMenuBar) {
        if (model.isRollover() || model.isArmed() || c instanceof JMenu && model.isSelected()) {
            JTattooUtilities.fillHorGradient(g, AbstractLookAndFeel.getTheme().getMenuSelectionColors(), x, y, w, h);
        }
        if (model.isRollover() && !model.isSelected()) {
            Color[] colArr = AbstractLookAndFeel.getTheme().getMenuSelectionColors();
            Color frameColor = ColorHelper.darker(colArr[colArr.length - 1], 5);
            g.setColor(frameColor);
            g.drawRect(x, y, w - 1, h - 1);
        }
    } else {
        if (model.isArmed() || c instanceof JMenu && model.isSelected()) {
            JTattooUtilities.fillHorGradient(g, AbstractLookAndFeel.getTheme().getMenuSelectionColors(), x, y, w, h);
        } else if (!AbstractLookAndFeel.getTheme().isMenuOpaque()) {
            Graphics2D g2D = (Graphics2D) g;
            Composite composite = g2D.getComposite();
            AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, AbstractLookAndFeel.getTheme().getMenuAlpha());
            g2D.setComposite(alpha);
            g.setColor(AbstractLookAndFeel.getMenuBackgroundColor());
            g.fillRect(x, y, w, h);
            g2D.setComposite(composite);
        } else {
            g.setColor(AbstractLookAndFeel.getMenuBackgroundColor());
            g.fillRect(x, y, w, h);
        }
    }
    if (menuItem.isSelected() && menuItem.isArmed()) {
        g.setColor(AbstractLookAndFeel.getMenuSelectionForegroundColor());
    } else {
        g.setColor(AbstractLookAndFeel.getMenuForegroundColor());
    }
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) AlphaComposite(java.awt.AlphaComposite) Color(java.awt.Color) JMenuItem(javax.swing.JMenuItem) ButtonModel(javax.swing.ButtonModel) JMenuBar(javax.swing.JMenuBar) JMenu(javax.swing.JMenu) Graphics2D(java.awt.Graphics2D)

Example 98 with Composite

use of java.awt.Composite in project LoboEvolution by LoboEvolution.

the class HiFiTabbedPaneUI method paintText.

/**
 * {@inheritDoc}
 */
@Override
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) {
    Color backColor = tabPane.getBackgroundAt(tabIndex);
    if (!(backColor instanceof UIResource)) {
        super.paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected);
        return;
    }
    g.setFont(font);
    View v = getTextViewForTab(tabIndex);
    if (v != null) {
        // html
        Graphics2D g2D = (Graphics2D) g;
        Object savedRenderingHint = null;
        if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) {
            savedRenderingHint = g2D.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
            g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, AbstractLookAndFeel.getTheme().getTextAntiAliasingHint());
        }
        v.paint(g, textRect);
        if (AbstractLookAndFeel.getTheme().isTextAntiAliasingOn()) {
            g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, savedRenderingHint);
        }
    } else {
        // plain text
        int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex);
        Graphics2D g2D = (Graphics2D) g;
        Composite composite = g2D.getComposite();
        AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);
        g2D.setComposite(alpha);
        Color fc = tabPane.getForegroundAt(tabIndex);
        if (isSelected) {
            fc = AbstractLookAndFeel.getTheme().getTabSelectionForegroundColor();
        }
        if (!tabPane.isEnabled() || !tabPane.isEnabledAt(tabIndex)) {
            fc = AbstractLookAndFeel.getTheme().getDisabledForegroundColor();
        }
        if (ColorHelper.getGrayValue(fc) > 128) {
            g2D.setColor(Color.black);
        } else {
            g2D.setColor(Color.white);
        }
        JTattooUtilities.drawStringUnderlineCharAt(tabPane, g, title, mnemIndex, textRect.x + 1, textRect.y + 1 + metrics.getAscent());
        g2D.setComposite(composite);
        g2D.setColor(fc);
        JTattooUtilities.drawStringUnderlineCharAt(tabPane, g, title, mnemIndex, textRect.x, textRect.y + metrics.getAscent());
    }
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) AlphaComposite(java.awt.AlphaComposite) Color(java.awt.Color) View(javax.swing.text.View) UIResource(javax.swing.plaf.UIResource) Graphics2D(java.awt.Graphics2D)

Example 99 with Composite

use of java.awt.Composite in project LoboEvolution by LoboEvolution.

the class HiFiToggleButtonUI method paintText.

/**
 * {@inheritDoc}
 */
@Override
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
    ButtonModel model = b.getModel();
    FontMetrics fm = JTattooUtilities.getFontMetrics(b, g, b.getFont());
    int mnemIndex = b.getDisplayedMnemonicIndex();
    int offs = 0;
    if (model.isArmed() && model.isPressed()) {
        offs = 1;
    }
    Graphics2D g2D = (Graphics2D) g;
    Composite composite = g2D.getComposite();
    AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);
    g2D.setComposite(alpha);
    Color foreground = b.getForeground();
    Color background = b.getBackground();
    if (model.isPressed() && model.isArmed() || model.isSelected()) {
        if (foreground instanceof ColorUIResource && background instanceof ColorUIResource) {
            foreground = AbstractLookAndFeel.getTheme().getPressedForegroundColor();
        }
    }
    if (!model.isEnabled()) {
        foreground = AbstractLookAndFeel.getTheme().getDisabledForegroundColor();
    }
    if (ColorHelper.getGrayValue(foreground) > 128) {
        g2D.setColor(Color.black);
    } else {
        g2D.setColor(Color.white);
    }
    JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x + offs + 1, textRect.y + offs + fm.getAscent() + 1);
    g2D.setComposite(composite);
    g2D.setColor(foreground);
    JTattooUtilities.drawStringUnderlineCharAt(b, g, text, mnemIndex, textRect.x + offs, textRect.y + offs + fm.getAscent());
}
Also used : Composite(java.awt.Composite) AlphaComposite(java.awt.AlphaComposite) FontMetrics(java.awt.FontMetrics) AlphaComposite(java.awt.AlphaComposite) Color(java.awt.Color) ButtonModel(javax.swing.ButtonModel) ColorUIResource(javax.swing.plaf.ColorUIResource) Graphics2D(java.awt.Graphics2D)

Example 100 with Composite

use of java.awt.Composite in project qupath by qupath.

the class HierarchyOverlay method paintOverlay.

@Override
public void paintOverlay(final Graphics2D g2d, final ImageRegion imageRegion, final double downsampleFactor, final ImageData<BufferedImage> imageData, final boolean paintCompletely) {
    if (this.imageData != imageData) {
        this.imageData = imageData;
        updateOverlayServer();
    }
    // Get the selection model, which can influence colours (TODO: this might not be the best way to do it!)
    PathObjectHierarchy hierarchy = imageData == null ? null : imageData.getHierarchy();
    if (hierarchy == null)
        return;
    if (!isVisible() && hierarchy.getSelectionModel().noSelection())
        return;
    // Default RenderingHints (may be temporarily changed in some places)
    var defaultAntiAlias = RenderingHints.VALUE_ANTIALIAS_ON;
    var defaultStroke = RenderingHints.VALUE_STROKE_PURE;
    // Doesn't seem to help...?
    // boolean fastRendering = true;
    // if (fastRendering) {
    // defaultAntiAlias = RenderingHints.VALUE_ANTIALIAS_OFF;
    // defaultStroke = RenderingHints.VALUE_STROKE_DEFAULT;
    // }
    OverlayOptions overlayOptions = getOverlayOptions();
    long timestamp = overlayOptions.lastChangeTimestamp().get();
    int pointRadius = PathPrefs.pointRadiusProperty().get();
    if (overlayOptionsTimestamp != timestamp || pointRadius != lastPointRadius) {
        lastPointRadius = pointRadius;
        overlayOptionsTimestamp = timestamp;
    }
    int t = imageRegion.getT();
    int z = imageRegion.getZ();
    Rectangle serverBounds = AwtTools.getBounds(imageRegion);
    // Ensure antialias is on...?
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, defaultAntiAlias);
    // Get the displayed clip bounds for fast checking if ROIs need to be drawn
    Shape shapeRegion = g2d.getClip();
    if (shapeRegion == null)
        shapeRegion = AwtTools.getBounds(imageRegion);
    var boundsDisplayed = shapeRegion.getBounds();
    // Ensure the bounds do not extend beyond what the server actually contains
    boundsDisplayed = boundsDisplayed.intersection(serverBounds);
    if (boundsDisplayed.width <= 0 || boundsDisplayed.height <= 0)
        return;
    // Get the annotations & selected objects (which must be painted directly)
    Collection<PathObject> selectedObjects = new ArrayList<>(hierarchy.getSelectionModel().getSelectedObjects());
    selectedObjects.removeIf(p -> !p.hasROI() || (p.getROI().getZ() != z || p.getROI().getT() != t));
    ImageRegion region = AwtTools.getImageRegion(boundsDisplayed, z, t);
    // Paint detection objects
    long startTime = System.currentTimeMillis();
    if (overlayOptions.getShowDetections() && !hierarchy.isEmpty()) {
        // If we aren't downsampling by much, or we're upsampling, paint directly - making sure to paint the right number of times, and in the right order
        if (overlayServer == null || regionStore == null || downsampleFactor < 1.0) {
            Collection<PathObject> pathObjects;
            try {
                Set<PathObject> pathObjectsToPaint = new TreeSet<>(comparator);
                pathObjects = hierarchy.getObjectsForRegion(PathDetectionObject.class, region, pathObjectsToPaint);
            } catch (IllegalArgumentException e) {
                // This can happen (rarely) in a multithreaded environment if the level of a detection changes.
                // However, protecting against this fully by caching the level with integer boxing/unboxing would be expensive.
                logger.debug("Exception requesting detections to paint: " + e.getLocalizedMessage(), e);
                pathObjects = hierarchy.getObjectsForRegion(PathDetectionObject.class, region, null);
            }
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            PathHierarchyPaintingHelper.paintSpecifiedObjects(g2d, boundsDisplayed, pathObjects, overlayOptions, hierarchy.getSelectionModel(), downsampleFactor);
            if (overlayOptions.getShowConnections()) {
                Object connections = imageData.getProperty(DefaultPathObjectConnectionGroup.KEY_OBJECT_CONNECTIONS);
                if (connections instanceof PathObjectConnections)
                    PathHierarchyPaintingHelper.paintConnections((PathObjectConnections) connections, hierarchy, g2d, imageData.isFluorescence() ? ColorToolsAwt.TRANSLUCENT_WHITE : ColorToolsAwt.TRANSLUCENT_BLACK, downsampleFactor);
            }
        } else {
            // On the other hand, if a large image has been updated then we may be browsing quickly - better to repaint quickly while tiles may still be loading
            if (paintCompletely) {
                regionStore.paintRegionCompletely(overlayServer, g2d, shapeRegion, z, t, downsampleFactor, null, null, 5000);
            } else {
                regionStore.paintRegion(overlayServer, g2d, shapeRegion, z, t, downsampleFactor, null, null, null);
            }
        }
    }
    long endTime = System.currentTimeMillis();
    if (endTime - startTime > 500)
        logger.debug("Painting time: {} seconds", GeneralTools.formatNumber((endTime - startTime) / 1000.0, 4));
    // The setting below stops some weird 'jiggling' effects during zooming in/out, or poor rendering of shape ROIs
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, defaultAntiAlias);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, defaultStroke);
    // Prepare to handle labels, if we need to
    Collection<PathObject> objectsWithNames = new ArrayList<>();
    Collection<PathObject> annotations = hierarchy.getObjectsForRegion(PathAnnotationObject.class, region, null);
    for (var iterator = annotations.iterator(); iterator.hasNext(); ) {
        var next = iterator.next();
        if ((next.getName() != null && !next.getName().isBlank()))
            objectsWithNames.add(next);
        if (selectedObjects.contains(next))
            iterator.remove();
    }
    // Paint the annotations
    List<PathObject> pathObjectList = new ArrayList<>(annotations);
    Collections.sort(pathObjectList, Comparator.comparingInt(PathObject::getLevel).reversed().thenComparing(Comparator.comparingDouble((PathObject p) -> -p.getROI().getArea())));
    PathHierarchyPaintingHelper.paintSpecifiedObjects(g2d, boundsDisplayed, pathObjectList, overlayOptions, null, downsampleFactor);
    // Ensure that selected objects are painted last, to make sure they aren't obscured
    if (!selectedObjects.isEmpty()) {
        Composite previousComposite = g2d.getComposite();
        float opacity = overlayOptions.getOpacity();
        if (opacity < 1) {
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            PathHierarchyPaintingHelper.paintSpecifiedObjects(g2d, boundsDisplayed, selectedObjects, overlayOptions, hierarchy.getSelectionModel(), downsampleFactor);
            g2d.setComposite(previousComposite);
        } else {
            PathHierarchyPaintingHelper.paintSpecifiedObjects(g2d, boundsDisplayed, selectedObjects, overlayOptions, hierarchy.getSelectionModel(), downsampleFactor);
        }
    }
    // Paint labels
    if (overlayOptions.getShowNames() && !objectsWithNames.isEmpty()) {
        double requestedFontSize;
        switch(PathPrefs.viewerFontSizeProperty().get()) {
            case HUGE:
                requestedFontSize = 24;
                break;
            case LARGE:
                requestedFontSize = 18;
                break;
            case SMALL:
                requestedFontSize = 10;
                break;
            case TINY:
                requestedFontSize = 8;
                break;
            case MEDIUM:
            default:
                requestedFontSize = 14;
                break;
        }
        float fontSize = (float) (requestedFontSize * downsampleFactor);
        if (!GeneralTools.almostTheSame(font.getSize2D(), fontSize, 0.001))
            font = font.deriveFont(fontSize);
        g2d.setFont(font);
        var metrics = g2d.getFontMetrics(font);
        var rect = new Rectangle2D.Double();
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        for (var annotation : objectsWithNames) {
            var name = annotation.getName();
            var roi = annotation.getROI();
            if (name != null && !name.isBlank() && roi != null && !overlayOptions.isPathClassHidden(annotation.getPathClass())) {
                g2d.setColor(ColorToolsAwt.TRANSLUCENT_BLACK);
                var bounds = metrics.getStringBounds(name, g2d);
                double pad = 5.0 * downsampleFactor;
                double x = roi.getCentroidX() - bounds.getWidth() / 2.0;
                double y = roi.getCentroidY() + bounds.getY() + metrics.getAscent() + pad;
                rect.setFrame(x + bounds.getX() - pad, y + bounds.getY() - pad, bounds.getWidth() + pad * 2, bounds.getHeight() + pad * 2);
                g2d.fill(rect);
                g2d.setColor(Color.WHITE);
                g2d.drawString(name, (float) x, (float) y);
            }
        }
    }
}
Also used : PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) PathDetectionObject(qupath.lib.objects.PathDetectionObject) PathObjectConnections(qupath.lib.objects.PathObjectConnections) Shape(java.awt.Shape) AlphaComposite(java.awt.AlphaComposite) Composite(java.awt.Composite) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) ImageRegion(qupath.lib.regions.ImageRegion) PathObject(qupath.lib.objects.PathObject) OverlayOptions(qupath.lib.gui.viewer.OverlayOptions) TreeSet(java.util.TreeSet) PathAnnotationObject(qupath.lib.objects.PathAnnotationObject) PathDetectionObject(qupath.lib.objects.PathDetectionObject) PathObject(qupath.lib.objects.PathObject)

Aggregations

Composite (java.awt.Composite)248 AlphaComposite (java.awt.AlphaComposite)236 Graphics2D (java.awt.Graphics2D)120 Paint (java.awt.Paint)80 Rectangle2D (java.awt.geom.Rectangle2D)80 Color (java.awt.Color)76 Shape (java.awt.Shape)64 BasicStroke (java.awt.BasicStroke)47 Stroke (java.awt.Stroke)44 BufferedImage (java.awt.image.BufferedImage)42 AffineTransform (java.awt.geom.AffineTransform)33 Point (java.awt.Point)32 Line2D (java.awt.geom.Line2D)30 Font (java.awt.Font)28 Point2D (java.awt.geom.Point2D)24 Rectangle (java.awt.Rectangle)23 GradientPaint (java.awt.GradientPaint)22 GeneralPath (java.awt.geom.GeneralPath)20 RectangleInsets (org.jfree.ui.RectangleInsets)20 FontMetrics (java.awt.FontMetrics)19