Search in sources :

Example 31 with Rectangle2D

use of java.awt.geom.Rectangle2D in project intellij-community by JetBrains.

the class ImmediatePainter method paintImmediately.

private void paintImmediately(final Graphics g, final int offset, final char c2) {
    final EditorImpl editor = myEditor;
    final Document document = editor.getDocument();
    final LexerEditorHighlighter highlighter = (LexerEditorHighlighter) myEditor.getHighlighter();
    final EditorSettings settings = editor.getSettings();
    final boolean isBlockCursor = editor.isInsertMode() == settings.isBlockCursor();
    final int lineHeight = editor.getLineHeight();
    final int ascent = editor.getAscent();
    final int topOverhang = editor.myView.getTopOverhang();
    final int bottomOverhang = editor.myView.getBottomOverhang();
    final char c1 = offset == 0 ? ' ' : document.getCharsSequence().charAt(offset - 1);
    final List<TextAttributes> attributes = highlighter.getAttributesForPreviousAndTypedChars(document, offset, c2);
    updateAttributes(editor, offset, attributes);
    final TextAttributes attributes1 = attributes.get(0);
    final TextAttributes attributes2 = attributes.get(1);
    if (!(canRender(attributes1) && canRender(attributes2))) {
        return;
    }
    FontLayoutService fontLayoutService = FontLayoutService.getInstance();
    final float width1 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes1.getFontType()), c1);
    final float width2 = fontLayoutService.charWidth2D(editor.getFontMetrics(attributes2.getFontType()), c2);
    final Font font1 = EditorUtil.fontForChar(c1, attributes1.getFontType(), editor).getFont();
    final Font font2 = EditorUtil.fontForChar(c1, attributes2.getFontType(), editor).getFont();
    final Point2D p2 = editor.offsetToXY(offset, false);
    float p2x = (float) p2.getX();
    int p2y = (int) p2.getY();
    int width1i = (int) (p2x) - (int) (p2x - width1);
    int width2i = (int) (p2x + width2) - (int) p2x;
    Caret caret = editor.getCaretModel().getPrimaryCaret();
    //noinspection ConstantConditions
    final int caretWidth = isBlockCursor ? editor.getCaretLocations(false)[0].myWidth : JBUI.scale(caret.getVisualAttributes().getWidth(settings.getLineCursorWidth()));
    final float caretShift = isBlockCursor ? 0 : caretWidth == 1 ? 0 : 1 / JBUI.sysScale((Graphics2D) g);
    final Rectangle2D caretRectangle = new Rectangle2D.Float((int) (p2x + width2) - caretShift, p2y - topOverhang, caretWidth, lineHeight + topOverhang + bottomOverhang + (isBlockCursor ? -1 : 0));
    final Rectangle rectangle1 = new Rectangle((int) (p2x - width1), p2y, width1i, lineHeight);
    final Rectangle rectangle2 = new Rectangle((int) p2x, p2y, (int) (width2i + caretWidth - caretShift), lineHeight);
    final Consumer<Graphics> painter = graphics -> {
        EditorUIUtil.setupAntialiasing(graphics);
        fillRect(graphics, rectangle2, attributes2.getBackgroundColor());
        drawChar(graphics, c2, p2x, p2y + ascent, font2, attributes2.getForegroundColor());
        fillRect(graphics, caretRectangle, getCaretColor(editor));
        fillRect(graphics, rectangle1, attributes1.getBackgroundColor());
        drawChar(graphics, c1, p2x - width1, p2y + ascent, font1, attributes1.getForegroundColor());
    };
    final Shape originalClip = g.getClip();
    g.setClip(new Rectangle2D.Float((int) p2x - caretShift, p2y, width2i + caretWidth, lineHeight));
    if (DOUBLE_BUFFERING.asBoolean()) {
        paintWithDoubleBuffering(g, painter);
    } else {
        painter.consume(g);
    }
    g.setClip(originalClip);
    if (PIPELINE_FLUSH.asBoolean()) {
        Toolkit.getDefaultToolkit().sync();
    }
    if (DEBUG.asBoolean()) {
        pause();
    }
}
Also used : RegistryValue(com.intellij.openapi.util.registry.RegistryValue) UIUtil(com.intellij.util.ui.UIUtil) Point2D(java.awt.geom.Point2D) com.intellij.openapi.editor(com.intellij.openapi.editor) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Rectangle2D(java.awt.geom.Rectangle2D) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) JBUI(com.intellij.util.ui.JBUI) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) EffectType(com.intellij.openapi.editor.markup.EffectType) Disposer(com.intellij.openapi.util.Disposer) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) IterationState(com.intellij.openapi.editor.impl.view.IterationState) FontLayoutService(com.intellij.openapi.editor.impl.view.FontLayoutService) EditorTextField(com.intellij.ui.EditorTextField) BufferedImage(java.awt.image.BufferedImage) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) java.awt(java.awt) VolatileImage(java.awt.image.VolatileImage) EditorUIUtil(com.intellij.openapi.editor.ex.util.EditorUIUtil) List(java.util.List) EditorColors(com.intellij.openapi.editor.colors.EditorColors) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) Gray(com.intellij.ui.Gray) JBColor(com.intellij.ui.JBColor) Consumer(com.intellij.util.Consumer) javax.swing(javax.swing) Rectangle2D(java.awt.geom.Rectangle2D) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) Point2D(java.awt.geom.Point2D) FontLayoutService(com.intellij.openapi.editor.impl.view.FontLayoutService) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 32 with Rectangle2D

use of java.awt.geom.Rectangle2D in project intellij-community by JetBrains.

the class ColorProgressBar method paintComponent.

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    final GraphicsConfig config = GraphicsUtil.setupAAPainting(g);
    Graphics2D g2 = (Graphics2D) g;
    if (myFraction > 1) {
        myFraction = 1;
    }
    Dimension size = getSize();
    g2.setPaint(UIUtil.getListBackground());
    Rectangle2D rect = new Rectangle2D.Double(2, 2, size.width - 4, size.height - 4);
    g2.fill(rect);
    g2.setPaint(new JBColor(SHADOW1, UIUtil.getBorderColor()));
    rect.setRect(1, 1, size.width - 2, size.height - 2);
    g2.drawRoundRect(1, 1, size.width - 2, size.height - 2, 5, 5);
    g2.setPaint(SHADOW2);
    g2.drawRoundRect(0, 0, size.width - 2, size.height - 2, 5, 5);
    int y_center = size.height / 2;
    int y_steps = size.height / 2 - 3;
    int alpha_step = y_steps > 0 ? (255 - 70) / y_steps : 255 - 70;
    int x_offset = 4;
    g.setClip(4, 3, size.width - 8, size.height - 6);
    int bricksToDraw = myFraction == 0 ? 0 : getBricksToDraw(myFraction);
    if (myIndeterminate) {
        int startFrom = bricksToDraw < INDETERMINATE_BRICKS_DRAW ? 0 : bricksToDraw - INDETERMINATE_BRICKS_DRAW;
        int endTo = bricksToDraw + INDETERMINATE_BRICKS_DRAW < getBricksToDraw(1) ? bricksToDraw + INDETERMINATE_BRICKS_DRAW : getBricksToDraw(1);
        for (int i = startFrom; i <= endTo; i++) {
            g2.setPaint(myColor);
            int startXOffset = x_offset + (BRICK_WIDTH + BRICK_SPACE) * i;
            UIUtil.drawLine(g2, startXOffset, y_center, startXOffset + BRICK_WIDTH - 1, y_center);
            for (int j = 0; j < y_steps; j++) {
                Color color = ColorUtil.toAlpha(myColor, 255 - alpha_step * (j + 1));
                g2.setPaint(color);
                UIUtil.drawLine(g2, startXOffset, y_center - 1 - j, startXOffset + BRICK_WIDTH - 1, y_center - 1 - j);
                if (!(y_center % 2 != 0 && j == y_steps - 1)) {
                    UIUtil.drawLine(g2, startXOffset, y_center + 1 + j, startXOffset + BRICK_WIDTH - 1, y_center + 1 + j);
                }
            }
            g2.setColor(ColorUtil.toAlpha(myColor, 255 - alpha_step * (y_steps / 2 + 1)));
            g2.drawRect(startXOffset, y_center - y_steps, BRICK_WIDTH - 1, size.height - 7);
        }
    } else {
        for (int i = 0; i < bricksToDraw; i++) {
            g2.setPaint(myColor);
            UIUtil.drawLine(g2, x_offset, y_center, x_offset + BRICK_WIDTH - 1, y_center);
            for (int j = 0; j < y_steps; j++) {
                Color color = ColorUtil.toAlpha(myColor, 255 - alpha_step * (j + 1));
                g2.setPaint(color);
                UIUtil.drawLine(g2, x_offset, y_center - 1 - j, x_offset + BRICK_WIDTH - 1, y_center - 1 - j);
                if (!(y_center % 2 != 0 && j == y_steps - 1)) {
                    UIUtil.drawLine(g2, x_offset, y_center + 1 + j, x_offset + BRICK_WIDTH - 1, y_center + 1 + j);
                }
            }
            g2.setColor(ColorUtil.toAlpha(myColor, 255 - alpha_step * (y_steps / 2 + 1)));
            g2.drawRect(x_offset, y_center - y_steps, BRICK_WIDTH - 1, size.height - 7);
            x_offset += BRICK_WIDTH + BRICK_SPACE;
        }
    }
    config.restore();
}
Also used : JBColor(com.intellij.ui.JBColor) Rectangle2D(java.awt.geom.Rectangle2D) JBColor(com.intellij.ui.JBColor) GraphicsConfig(com.intellij.openapi.ui.GraphicsConfig)

Example 33 with Rectangle2D

use of java.awt.geom.Rectangle2D in project intellij-community by JetBrains.

the class BalloonImpl method show.

private void show(PositionTracker<Balloon> tracker, AbstractPosition position) {
    assert !myDisposed : "Balloon is already disposed";
    if (isVisible())
        return;
    final Component comp = tracker.getComponent();
    if (!comp.isShowing())
        return;
    myTracker = tracker;
    myTracker.init(this);
    JRootPane root = ObjectUtils.notNull(UIUtil.getRootPane(comp));
    myVisible = true;
    myLayeredPane = root.getLayeredPane();
    myPosition = position;
    UIUtil.setFutureRootPane(myContent, root);
    myFocusManager = IdeFocusManager.findInstanceByComponent(myLayeredPane);
    final Ref<Component> originalFocusOwner = new Ref<>();
    final Ref<FocusRequestor> focusRequestor = new Ref<>();
    final Ref<ActionCallback> proxyFocusRequest = new Ref<>(ActionCallback.DONE);
    boolean mnemonicsFix = myDialogMode && SystemInfo.isMac && Registry.is("ide.mac.inplaceDialogMnemonicsFix");
    if (mnemonicsFix) {
        final IdeGlassPaneEx glassPane = (IdeGlassPaneEx) IdeGlassPaneUtil.find(myLayeredPane);
        assert glassPane != null;
        proxyFocusRequest.set(new ActionCallback());
        myFocusManager.doWhenFocusSettlesDown(new ExpirableRunnable() {

            @Override
            public boolean isExpired() {
                return isDisposed();
            }

            @Override
            public void run() {
                IdeEventQueue.getInstance().disableInputMethods(BalloonImpl.this);
                originalFocusOwner.set(myFocusManager.getFocusOwner());
                focusRequestor.set(myFocusManager.getFurtherRequestor());
            }
        });
    }
    if (myRequestFocus) {
        myFocusManager.doWhenFocusSettlesDown(new ExpirableRunnable() {

            @Override
            public boolean isExpired() {
                return isDisposed();
            }

            @Override
            public void run() {
                myOriginalFocusOwner = myFocusManager.getFocusOwner();
                // Set the accessible parent so that screen readers don't announce
                // a window context change -- the tooltip is "logically" hosted
                // inside the component (e.g. editor) it appears on top of.
                AccessibleContextUtil.setParent(myContent, myOriginalFocusOwner);
                // Set the focus to "myContent"
                myFocusManager.requestFocus(getContentToFocus(), true);
            }
        });
    }
    myLayeredPane.addComponentListener(myComponentListener);
    myTargetPoint = myPosition.getShiftedPoint(myTracker.recalculateLocation(this).getPoint(myLayeredPane), myCalloutShift);
    int positionChangeFix = 0;
    if (myShowPointer) {
        Rectangle rec = getRecForPosition(myPosition, true);
        if (!myPosition.isOkToHavePointer(myTargetPoint, rec, getPointerLength(myPosition), getPointerWidth(myPosition), getArc())) {
            rec = getRecForPosition(myPosition, false);
            Rectangle lp = new Rectangle(new Point(myContainerInsets.left, myContainerInsets.top), myLayeredPane.getSize());
            lp.width -= myContainerInsets.right;
            lp.height -= myContainerInsets.bottom;
            if (!lp.contains(rec)) {
                Rectangle2D currentSquare = lp.createIntersection(rec);
                double maxSquare = currentSquare.getWidth() * currentSquare.getHeight();
                AbstractPosition targetPosition = myPosition;
                for (AbstractPosition eachPosition : myPosition.getOtherPositions()) {
                    Rectangle2D eachIntersection = lp.createIntersection(getRecForPosition(eachPosition, false));
                    double eachSquare = eachIntersection.getWidth() * eachIntersection.getHeight();
                    if (maxSquare < eachSquare) {
                        maxSquare = eachSquare;
                        targetPosition = eachPosition;
                    }
                }
                myPosition = targetPosition;
                positionChangeFix = myPosition.getChangeShift(position, myPositionChangeXShift, myPositionChangeYShift);
            }
        }
    }
    if (myPosition != position) {
        myTargetPoint = myPosition.getShiftedPoint(myTracker.recalculateLocation(this).getPoint(myLayeredPane), myCalloutShift > 0 ? myCalloutShift + positionChangeFix : positionChangeFix);
    }
    createComponent();
    myComp.validate();
    Rectangle rec = myComp.getContentBounds();
    if (myShowPointer && !myPosition.isOkToHavePointer(myTargetPoint, rec, getPointerLength(myPosition), getPointerWidth(myPosition), getArc())) {
        myShowPointer = false;
        myComp.removeAll();
        myLayeredPane.remove(myComp);
        createComponent();
        if (!new Rectangle(myLayeredPane.getSize()).contains(new Rectangle(myComp.getSize()))) {
            // Balloon is bigger than window, don't show it at all.
            myComp.removeAll();
            myLayeredPane.remove(myComp);
            myLayeredPane = null;
            hide();
            return;
        }
    }
    for (JBPopupListener each : myListeners) {
        each.beforeShown(new LightweightWindowEvent(this));
    }
    runAnimation(true, myLayeredPane, null);
    myLayeredPane.revalidate();
    myLayeredPane.repaint();
    if (mnemonicsFix) {
        proxyFocusRequest.get().doWhenDone(() -> myFocusManager.requestFocus(originalFocusOwner.get(), true));
    }
    Toolkit.getDefaultToolkit().addAWTEventListener(myAwtActivityListener, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
    if (ApplicationManager.getApplication() != null) {
        ActionManager.getInstance().addAnActionListener(new AnActionListener.Adapter() {

            @Override
            public void beforeActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
                if (myHideOnAction) {
                    hide();
                }
            }
        }, this);
    }
    if (myHideOnLinkClick) {
        JEditorPane editorPane = UIUtil.uiTraverser(myContent).traverse().filter(JEditorPane.class).first();
        if (editorPane != null) {
            editorPane.addHyperlinkListener(new HyperlinkAdapter() {

                @Override
                protected void hyperlinkActivated(HyperlinkEvent e) {
                    hide();
                }
            });
        }
    }
}
Also used : HyperlinkEvent(javax.swing.event.HyperlinkEvent) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) IdeGlassPaneEx(com.intellij.openapi.wm.impl.IdeGlassPaneEx) JBPopupListener(com.intellij.openapi.ui.popup.JBPopupListener) DataContext(com.intellij.openapi.actionSystem.DataContext) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) FocusRequestor(com.intellij.openapi.wm.FocusRequestor) Rectangle2D(java.awt.geom.Rectangle2D) RoundRectangle2D(java.awt.geom.RoundRectangle2D) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 34 with Rectangle2D

use of java.awt.geom.Rectangle2D in project android by JetBrains.

the class DrawConnectionUtils method drawHorizontalMarginIndicator.

/**
   * Utility function to draw an horizontal margin indicator
   *
   * @param g    graphics context
   * @param text the text to display
   * @param x1   x1 coordinate
   * @param x2   x2 coordinate
   * @param y    y coordinate
   */
public static void drawHorizontalMarginIndicator(Graphics2D g, String text, boolean isMarginReference, int x1, int x2, int y) {
    if (x1 > x2) {
        int temp = x1;
        x1 = x2;
        x2 = temp;
    }
    if (text == null) {
        g.drawLine(x1, y, x2, y);
        g.drawLine(x1, y, x1 + CONNECTION_ARROW_SIZE, y - CONNECTION_ARROW_SIZE);
        g.drawLine(x1, y, x1 + CONNECTION_ARROW_SIZE, y + CONNECTION_ARROW_SIZE);
        g.drawLine(x2, y, x2 - CONNECTION_ARROW_SIZE, y - CONNECTION_ARROW_SIZE);
        g.drawLine(x2, y, x2 - CONNECTION_ARROW_SIZE, y + CONNECTION_ARROW_SIZE);
        return;
    }
    Canvas c = new Canvas();
    Font previousFont = g.getFont();
    Font font = isMarginReference ? sFontReference : sFont;
    FontMetrics fm = c.getFontMetrics(font);
    g.setFont(font);
    int padding = 4;
    Rectangle2D bounds = fm.getStringBounds(text, g);
    int th = (int) bounds.getHeight();
    int tw = (int) bounds.getWidth();
    int offset = 3 * CONNECTION_ARROW_SIZE;
    int w = ((x2 - x1) - (tw + 2 * padding)) / 2;
    if (w <= padding) {
        g.drawLine(x1, y, x2, y);
        g.drawString(text, x1 + w + padding, y + offset);
        g.drawLine(x1, y - CONNECTION_ARROW_SIZE, x1, y + CONNECTION_ARROW_SIZE);
        g.drawLine(x2, y - CONNECTION_ARROW_SIZE, x2, y + CONNECTION_ARROW_SIZE);
    } else {
        g.drawLine(x1, y, x1 + w, y);
        g.drawLine(x2 - w, y, x2, y);
        g.drawString(text, x1 + w + padding, (int) (y + (bounds.getHeight() / 2)));
        g.drawLine(x1, y, x1 + CONNECTION_ARROW_SIZE, y - CONNECTION_ARROW_SIZE);
        g.drawLine(x1, y, x1 + CONNECTION_ARROW_SIZE, y + CONNECTION_ARROW_SIZE);
        g.drawLine(x2, y, x2 - CONNECTION_ARROW_SIZE, y - CONNECTION_ARROW_SIZE);
        g.drawLine(x2, y, x2 - CONNECTION_ARROW_SIZE, y + CONNECTION_ARROW_SIZE);
    }
    g.setFont(previousFont);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D)

Example 35 with Rectangle2D

use of java.awt.geom.Rectangle2D in project android by JetBrains.

the class DrawConnectionUtils method drawVerticalMargin.

/**
   *
   * @param g
   * @param string
   * @param x
   * @param y1
   * @param y2
   */
public static void drawVerticalMargin(Graphics2D g, String string, boolean isReference, int x, int y1, int y2) {
    Font previousFont = g.getFont();
    FontMetrics metrics = g.getFontMetrics();
    Rectangle2D rect = metrics.getStringBounds(string, g);
    g.drawLine(x, y1, x, y2);
    float sx = (float) (x + MARGIN_SPACING);
    float sy = (float) ((y2 + y1) / 2 + rect.getHeight() / 2 - metrics.getDescent());
    if (isReference) {
        g.setFont(sFontReference);
    }
    g.drawString(string, sx, sy);
    g.setFont(previousFont);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)262 Graphics2D (java.awt.Graphics2D)42 AffineTransform (java.awt.geom.AffineTransform)34 Color (java.awt.Color)33 Paint (java.awt.Paint)28 Point2D (java.awt.geom.Point2D)26 BufferedImage (java.awt.image.BufferedImage)23 Font (java.awt.Font)21 FontMetrics (java.awt.FontMetrics)20 Dimension (java.awt.Dimension)19 RoundRectangle2D (java.awt.geom.RoundRectangle2D)18 Rectangle (java.awt.Rectangle)16 FontRenderContext (java.awt.font.FontRenderContext)15 Point (java.awt.Point)11 BasicStroke (java.awt.BasicStroke)10 ArrayList (java.util.ArrayList)10 GradientPaint (java.awt.GradientPaint)9 TextLayout (java.awt.font.TextLayout)9 RadialGradientPaint (java.awt.RadialGradientPaint)8 Shape (java.awt.Shape)7