Search in sources :

Example 6 with GraphicsContext

use of com.github.weisj.darklaf.util.graphics.GraphicsContext in project darklaf by weisJ.

the class PaintUtil method doPaint.

private static void doPaint(final Graphics2D g, final float width, final float height, final float arc, final float bw, final boolean inside) {
    GraphicsContext context = GraphicsUtil.setupStrokePainting(g);
    Shape outerRect;
    Shape innerRect;
    if (Scale.equalWithError(arc, 0)) {
        outerRect = new Rectangle2D.Float(0, 0, width, height);
        innerRect = new Rectangle2D.Float(bw, bw, width - 2 * bw, height - 2 * bw);
    } else {
        float outerArc = inside ? arc : arc + bw;
        float innerArc = inside ? arc - bw : arc;
        outerRect = new RoundRectangle2D.Float(0, 0, width, height, outerArc, outerArc);
        innerRect = new RoundRectangle2D.Float(bw, bw, width - 2 * bw, height - 2 * bw, innerArc, innerArc);
    }
    Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
    path.append(outerRect, false);
    path.append(innerRect, false);
    g.fill(path);
    context.restore();
}
Also used : GraphicsContext(com.github.weisj.darklaf.util.graphics.GraphicsContext) RoundRectangle2D(java.awt.geom.RoundRectangle2D) Path2D(java.awt.geom.Path2D) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D)

Example 7 with GraphicsContext

use of com.github.weisj.darklaf.util.graphics.GraphicsContext in project darklaf by weisJ.

the class StringPainter method drawStringImpl.

public static <T extends JComponent> void drawStringImpl(final Graphics g, final T c, final View view, final String text, final Rectangle textRect, final Font font, final FontMetrics fm, final int mnemIndex, final Color background) {
    if (text == null || text.equals(""))
        return;
    GraphicsContext context = GraphicsUtil.setupAntialiasing(g);
    final int asc = fm.getAscent();
    final int x = textRect.x;
    final int y = textRect.y;
    Graphics2D drawingGraphics = (Graphics2D) g;
    // Only needed for translucent AA painting.
    BufferedImage img = null;
    // Only needed for experimental algorithm.
    Point textPos = null;
    Color fgColor = g.getColor();
    Color bgColor = background;
    /*
         * If there is a non-opaque parent on Windows no sub-pixel AA is supported. In this case we paint
         * the text to an offscreen image with opaque background and paste it draw it back to the original
         * graphics object.
         *
         * See https://bugs.openjdk.java.net/browse/JDK-8215980?attachmentOrder=desc
         */
    Component window = getNonOpaqueWindow(c);
    boolean paintOpaqueBuffered = window != null;
    if (paintOpaqueBuffered) {
        if (bgColor == null) {
            bgColor = effectiveBackgroundColor(c);
        }
        LOGGER.finest(() -> "Using opaque buffering for " + c);
        double scaleX = Scale.getScaleX((Graphics2D) g);
        double scaleY = Scale.getScaleX((Graphics2D) g);
        if (experimentalAntialiasingEnabled) {
            textPos = new Point(x, y);
            textPos.setLocation(SwingUtilities.convertPoint(c, textPos, window));
            textPos.setLocation((int) Math.round(scaleX * textPos.x), (int) Math.round(scaleX * textPos.y));
            /*
                 * Ensure the background color has sufficient contrast to the foreground.
                 */
            Color fg = g.getColor();
            double brightness = ColorUtil.getPerceivedBrightness(fg);
            bgColor = brightness > 127 ? Color.BLACK : Color.WHITE;
        }
        img = ImageUtil.createCompatibleImage((int) Math.round(scaleX * textRect.width), (int) Math.round(scaleY * textRect.height));
        drawingGraphics = prepareImage(img, bgColor, fgColor, scaleX, scaleY);
        textRect.setLocation(0, 0);
    }
    drawingGraphics.setFont(font);
    View v = view != null ? view : PropertyUtil.getObject(c, BasicHTML.propertyKey, View.class);
    if (v != null) {
        v.paint(drawingGraphics, textRect);
    } else {
        int textY = textRect.y + asc;
        if (mnemIndex >= 0) {
            SwingUtil.drawStringUnderlineCharAt(c, drawingGraphics, text, mnemIndex, textRect.x, textY);
        } else {
            SwingUtil.drawString(c, drawingGraphics, text, textRect.x, textY);
        }
    }
    if (paintOpaqueBuffered) {
        drawingGraphics.dispose();
        Image result = postProcessImage((Graphics2D) g, img, textPos, bgColor, fgColor);
        g.drawImage(result, x, y, textRect.width, textRect.height, null);
    }
    context.restore();
}
Also used : GraphicsContext(com.github.weisj.darklaf.util.graphics.GraphicsContext) BufferedImage(java.awt.image.BufferedImage) View(javax.swing.text.View) BufferedImage(java.awt.image.BufferedImage)

Example 8 with GraphicsContext

use of com.github.weisj.darklaf.util.graphics.GraphicsContext in project darklaf by weisJ.

the class DarkProgressBarUI method paintDeterminate.

@Override
protected void paintDeterminate(final Graphics g, final JComponent c) {
    Graphics2D g2 = (Graphics2D) g.create();
    try {
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        Rectangle r = new Rectangle(progressBar.getSize());
        if (c.isOpaque() && c.getParent() != null) {
            g2.setColor(c.getParent().getBackground());
            g2.fill(r);
        }
        Insets i = progressBar.getInsets();
        DarkUIUtil.applyInsets(r, i);
        int amountFull = getAmountFull(i, r.width, r.height);
        Shape fullShape, coloredShape;
        int orientation = progressBar.getOrientation();
        if (orientation == SwingConstants.HORIZONTAL) {
            int pHeight = progressBar.getPreferredSize().height;
            int yOffset = r.y + (r.height - pHeight) / 2;
            fullShape = getShapedRect(r.x, yOffset, r.width, pHeight, pHeight);
            if (progressBar.getComponentOrientation().isLeftToRight()) {
                coloredShape = getShapedRect(r.x, yOffset, amountFull, pHeight, pHeight);
            } else {
                coloredShape = getShapedRect(r.x + r.width - amountFull, yOffset, amountFull, pHeight, pHeight);
            }
        } else {
            int pWidth = progressBar.getPreferredSize().width;
            int xOffset = r.x + (r.width - pWidth) / 2;
            fullShape = getShapedRect(xOffset, r.y, pWidth, r.height, pWidth);
            if (progressBar.getComponentOrientation().isLeftToRight()) {
                coloredShape = getShapedRect(xOffset, r.y, pWidth, amountFull, pWidth);
            } else {
                coloredShape = getShapedRect(xOffset, r.y + r.height - amountFull, pWidth, amountFull, pWidth);
            }
        }
        g2.setColor(getRemainderColor());
        g2.fill(fullShape);
        if (hasFailed(progressBar)) {
            g2.setColor(failedColor);
        } else if (hasPassed(progressBar)) {
            g2.setColor(passedColor);
        } else {
            g2.setColor(getFinishedColor());
        }
        g2.fill(coloredShape);
        if (progressBar.isStringPainted()) {
            GraphicsContext config = GraphicsUtil.setupStrokePainting(g);
            Rectangle progressRect = coloredShape.getBounds();
            if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
                paintString((Graphics2D) g, i.left, i.top, r.width, r.height, progressRect.x, progressRect.x + progressRect.width);
            } else {
                paintString((Graphics2D) g, i.left, i.top, r.width, r.height, progressRect.y, progressRect.y + progressRect.height);
            }
            config.restore();
        }
    } finally {
        g2.dispose();
    }
}
Also used : GraphicsContext(com.github.weisj.darklaf.util.graphics.GraphicsContext)

Example 9 with GraphicsContext

use of com.github.weisj.darklaf.util.graphics.GraphicsContext in project darklaf by weisJ.

the class DarkRoundedScrollBarUI method paintMaxiThumb.

@Override
protected void paintMaxiThumb(final Graphics2D g, final Rectangle rect) {
    GraphicsContext context = GraphicsUtil.setupStrokePainting(g);
    g.setComposite(COMPOSITE.derive(thumbAlpha));
    boolean horizontal = scrollbar.getOrientation() == JScrollBar.HORIZONTAL;
    int ins = 2;
    RoundRectangle2D roundRect = new RoundRectangle2D.Float();
    int width = rect.width - 2 * ins;
    int height = rect.height - 2 * ins;
    int x = rect.x + ins;
    int y = rect.y + ins;
    if (hideScrollBar) {
        float animationState = scrollBarListener.getTrackState();
        if (horizontal) {
            int newHeight = Math.round(minimumSize + (height - minimumSize) * animationState);
            y += height - newHeight;
            height = newHeight;
        } else {
            int newWidth = Math.round(minimumSize + (width - minimumSize) * animationState);
            if (scrollbar.getComponentOrientation().isLeftToRight()) {
                x += width - newWidth;
            }
            width = newWidth;
        }
    }
    int arc = horizontal ? height : width;
    roundRect.setRoundRect(x, y, width, height, arc, arc);
    if (!roundRect.isEmpty()) {
        g.setColor(getThumbColor());
        g.fill(roundRect);
        g.setColor(getThumbBorderColor());
        g.draw(roundRect);
    }
    context.restore();
}
Also used : GraphicsContext(com.github.weisj.darklaf.util.graphics.GraphicsContext) RoundRectangle2D(java.awt.geom.RoundRectangle2D)

Example 10 with GraphicsContext

use of com.github.weisj.darklaf.util.graphics.GraphicsContext in project darklaf by weisJ.

the class MenuItemUI method paintMenuItemImpl.

default void paintMenuItemImpl(final Graphics g, final JComponent c, final Icon checkIcon, final Icon arrowIcon, final int defaultTextIconGap) {
    // Save original graphics font and color
    GraphicsContext context = new GraphicsContext(g);
    JMenuItem mi = (JMenuItem) c;
    g.setFont(mi.getFont());
    Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight());
    DarkUIUtil.applyInsets(viewRect, mi.getInsets());
    MenuItemLayoutHelper lh = getMenuItemLayoutHelper(checkIcon, arrowIcon, defaultTextIconGap, mi, viewRect);
    MenuItemLayoutHelper.MILayoutResult lr = lh.layoutMenuItem();
    Color background = getBackground(mi);
    Color foreground = getForeground(mi);
    paintBackgroundImpl(g, mi, background);
    context.restore();
    paintCheckIcon(g, mi, lh, lr, foreground);
    context.restore();
    paintIcon(g, mi, lh, lr);
    g.setColor(foreground);
    paintText(g, mi, lh, lr);
    paintAccText(g, mi, lh, lr);
    paintArrowIcon(g, mi, lh, lr, foreground);
    context.restore();
}
Also used : MenuItemLayoutHelper(com.github.weisj.darklaf.compatibility.MenuItemLayoutHelper) GraphicsContext(com.github.weisj.darklaf.util.graphics.GraphicsContext) Color(java.awt.Color) Rectangle(java.awt.Rectangle) JMenuItem(javax.swing.JMenuItem)

Aggregations

GraphicsContext (com.github.weisj.darklaf.util.graphics.GraphicsContext)35 Path2D (java.awt.geom.Path2D)2 RoundRectangle2D (java.awt.geom.RoundRectangle2D)2 MenuItemLayoutHelper (com.github.weisj.darklaf.compatibility.MenuItemLayoutHelper)1 DarkTableScrollPaneBorder (com.github.weisj.darklaf.ui.table.DarkTableScrollPaneBorder)1 AlignmentExt (com.github.weisj.darklaf.util.AlignmentExt)1 CleanupTask (com.github.weisj.darklaf.util.value.CleanupTask)1 Color (java.awt.Color)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Area (java.awt.geom.Area)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 JMenuItem (javax.swing.JMenuItem)1 TableColumn (javax.swing.table.TableColumn)1 TableColumnModel (javax.swing.table.TableColumnModel)1 JTextComponent (javax.swing.text.JTextComponent)1 View (javax.swing.text.View)1