Search in sources :

Example 31 with FontRenderContext

use of java.awt.font.FontRenderContext in project processdash by dtuma.

the class RadarPlot method drawLabel.

/**
     * Draws the label for one radar axis.
     *
     * @param g2 The graphics device.
     * @param chartArea The area for the radar chart.
     * @param data The data for the plot.
     * @param axis The axis (zero-based index).
     * @param startAngle The starting angle.
     */
protected void drawLabel(Graphics2D g2, Rectangle2D chartArea, String label, int axis, double labelX, double labelY) {
    // handle label drawing...
    FontRenderContext frc = g2.getFontRenderContext();
    Rectangle2D labelBounds = this.axisLabelFont.getStringBounds(label, frc);
    LineMetrics lm = this.axisLabelFont.getLineMetrics(label, frc);
    double ascent = lm.getAscent();
    if (labelX == chartArea.getCenterX())
        labelX -= labelBounds.getWidth() / 2;
    else if (labelX < chartArea.getCenterX())
        labelX -= labelBounds.getWidth();
    if (labelY > chartArea.getCenterY())
        labelY += ascent;
    g2.setPaint(this.axisLabelPaint);
    g2.setFont(this.axisLabelFont);
    g2.drawString(label, (float) labelX, (float) labelY);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics)

Example 32 with FontRenderContext

use of java.awt.font.FontRenderContext in project android by JetBrains.

the class BlueprintLayer method drawComponent.

/**
   * Draw the given component and its children
   *
   * @param gc                 the graphics context
   * @param component          the component we want to draw
   * @param viewHandlerManager the view handler
   */
private boolean drawComponent(@NotNull Graphics2D gc, @NotNull NlComponent component, @NotNull ViewHandlerManager viewHandlerManager, boolean parentHandlesPainting) {
    boolean needsRepaint = false;
    boolean handlesPainting = false;
    if (component.viewInfo != null) {
        ViewHandler handler = component.getViewHandler();
        // Check if the view handler handles the painting
        if (handler != null && handler instanceof ViewGroupHandler) {
            ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
            if (viewGroupHandler.handlesPainting()) {
                if (handler.paintConstraints(myScreenView, gc, component)) {
                    return needsRepaint;
                }
                needsRepaint |= viewGroupHandler.drawGroup(gc, myScreenView, component);
                handlesPainting = true;
            }
        }
        if (!handlesPainting && !parentHandlesPainting) {
            // If not, layout the component ourselves
            Graphics2D g = (Graphics2D) gc.create();
            int x = getSwingX(myScreenView, component.x);
            int y = getSwingY(myScreenView, component.y);
            int w = getSwingDimension(myScreenView, component.w);
            int h = getSwingDimension(myScreenView, component.h);
            drawComponentBackground(g, component);
            String name = component.getTagName();
            name = name.substring(name.lastIndexOf('.') + 1);
            Font font = BLUEPRINT_TEXT_FONT;
            g.setFont(font);
            g.setColor(BLUEPRINT_FG_COLOR);
            String id = component.getId();
            int lineHeight = g.getFontMetrics().getHeight();
            FontRenderContext fontRenderContext = g.getFontRenderContext();
            if (id != null && h > lineHeight * 2) {
                // Can fit both
                Rectangle2D classBounds = font.getStringBounds(name, fontRenderContext);
                Rectangle2D idBounds = font.getStringBounds(id, fontRenderContext);
                int textY = y + h / 2;
                int textX = x + w / 2 - ((int) classBounds.getWidth()) / 2;
                if (component.isRoot()) {
                    textX = x + lineHeight;
                    textY = y - (int) (classBounds.getHeight() + idBounds.getHeight());
                }
                g.drawString(name, textX, textY);
                if (component.isRoot()) {
                    textX = x + lineHeight;
                    textY = y - (int) (idBounds.getHeight());
                } else {
                    textX = x + w / 2 - ((int) idBounds.getWidth()) / 2;
                    textY += (int) (idBounds.getHeight());
                }
                g.drawString(id, textX, textY);
            } else {
                // Only room for a single line: prioritize the id if it's available, otherwise the class name
                String text = id != null ? id : name;
                Rectangle2D stringBounds = font.getStringBounds(text, fontRenderContext);
                int textX = x + w / 2 - ((int) stringBounds.getWidth()) / 2;
                int textY = y + h / 2 + ((int) stringBounds.getHeight()) / 2;
                g.drawString(text, textX, textY);
            }
            g.dispose();
        }
    }
    // Draw the children of the component...
    for (NlComponent child : component.getChildren()) {
        needsRepaint |= drawComponent(gc, child, viewHandlerManager, handlesPainting);
    }
    return needsRepaint;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) Rectangle2D(java.awt.geom.Rectangle2D) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) FontRenderContext(java.awt.font.FontRenderContext)

Example 33 with FontRenderContext

use of java.awt.font.FontRenderContext in project jdk8u_jdk by JetBrains.

the class ClassCastExceptionForInvalidSurface method main.

public static void main(final String[] args) throws InterruptedException {
    // Catch all uncaught exceptions and treat them as test failure
    Thread.setDefaultUncaughtExceptionHandler((t, e) -> failed = e);
    // Data for rendering
    BufferedImage bi = new BufferedImage(10, 10, TYPE_INT_ARGB);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    Font font = new Font("Serif", Font.PLAIN, 12);
    GlyphVector gv = font.createGlyphVector(frc, new char[] { 'a', '1' });
    Thread t1 = new Thread(() -> {
        while (!isComplete()) {
            vi = gc.createCompatibleVolatileImage(10, 10);
            if (!list.offer(vi)) {
                vi.flush();
            }
        }
        list.forEach(Image::flush);
    });
    Thread t2 = new Thread(() -> {
        while (!isComplete()) {
            VolatileImage vi = list.poll();
            if (vi != null) {
                vi.flush();
            }
        }
    });
    Thread t3 = new Thread(() -> {
        while (!isComplete()) {
            vi.createGraphics().drawImage(bi, 1, 1, null);
        }
    });
    Thread t4 = new Thread(() -> {
        while (!isComplete()) {
            vi.createGraphics().drawGlyphVector(gv, 0, 0);
            vi.createGraphics().drawOval(0, 0, 10, 10);
            vi.createGraphics().drawLine(0, 0, 10, 10);
            vi.createGraphics().drawString("123", 1, 1);
            vi.createGraphics().draw(new Rectangle(0, 0, 10, 10));
            vi.createGraphics().fillOval(0, 0, 10, 10);
            final Graphics2D graphics = vi.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.fillPolygon(new int[] { 0, 10, 10, 0 }, new int[] { 0, 0, 10, 10 }, 4);
        }
    });
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    if (failed != null) {
        System.err.println("Test failed");
        failed.printStackTrace();
    }
}
Also used : GlyphVector(java.awt.font.GlyphVector) VolatileImage(java.awt.image.VolatileImage) Rectangle(java.awt.Rectangle) FontRenderContext(java.awt.font.FontRenderContext) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) VolatileImage(java.awt.image.VolatileImage) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Example 34 with FontRenderContext

use of java.awt.font.FontRenderContext in project bytecode-viewer by Konloch.

the class CodeSequenceDiagram method execute.

@Override
public void execute(ArrayList<ClassNode> classNodeList) {
    if (BytecodeViewer.viewer.workPane.getCurrentViewer() == null || !(BytecodeViewer.viewer.workPane.getCurrentViewer() instanceof ClassViewer)) {
        BytecodeViewer.showMessage("First open a class file.");
        return;
    }
    ClassNode c = BytecodeViewer.viewer.workPane.getCurrentViewer().cn;
    if (c == null) {
        BytecodeViewer.showMessage("ClassNode is null for CodeSequenceDiagram. Please report to @Konloch");
        return;
    }
    JFrame frame = null;
    if (c.name != null)
        frame = new JFrame("Code Sequence Diagram - " + c.name);
    else
        frame = new JFrame("Code Sequence Diagram - Unknown Name");
    frame.setIconImages(Resources.iconList);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(400, 320);
    mxGraph graph = new mxGraph();
    graph.setVertexLabelsMovable(false);
    graph.setGridEnabled(true);
    graph.setEnabled(false);
    graph.setCellsEditable(false);
    graph.setCellsSelectable(false);
    graph.setCellsMovable(false);
    graph.setCellsLocked(true);
    Object parent = graph.getDefaultParent();
    Font font = UIManager.getDefaults().getFont("TabbedPane.font");
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
    graph.getModel().beginUpdate();
    try {
        int testX = 10;
        int testY = 0;
        double magicNumber = 5.8;
        for (MethodNode m : (ArrayList<MethodNode>) c.methods) {
            String mIdentifier = c.name + "." + m.name + m.desc;
            Object node = graph.insertVertex(parent, null, mIdentifier, testX, testY, mIdentifier.length() * magicNumber, 30);
            Object attach = node;
            testX += (int) (font.getStringBounds(mIdentifier, frc).getWidth()) + 60;
            for (AbstractInsnNode i : m.instructions.toArray()) {
                if (i instanceof MethodInsnNode) {
                    MethodInsnNode mi = (MethodInsnNode) i;
                    String identifier = mi.owner + "." + mi.name + mi.desc;
                    Object node2 = graph.insertVertex(parent, null, identifier, testX, testY, identifier.length() * 5, 30);
                    testX += (int) (font.getStringBounds(identifier, frc).getWidth()) + 60;
                    graph.insertEdge(parent, null, null, attach, node2);
                    attach = node2;
                }
            }
            testY += 60;
            testX = 10;
        }
    } finally {
        graph.getModel().endUpdate();
    }
    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    frame.getContentPane().add(graphComponent);
    frame.setVisible(true);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ArrayList(java.util.ArrayList) com.mxgraph.view.mxGraph(com.mxgraph.view.mxGraph) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) AffineTransform(java.awt.geom.AffineTransform) ClassViewer(the.bytecode.club.bytecodeviewer.gui.ClassViewer) FontRenderContext(java.awt.font.FontRenderContext) com.mxgraph.swing.mxGraphComponent(com.mxgraph.swing.mxGraphComponent)

Example 35 with FontRenderContext

use of java.awt.font.FontRenderContext in project sulky by huxi.

the class MemoryStatus method paintMemoryStatus.

private void paintMemoryStatus(Graphics g, Rectangle paintingBounds) {
    MemoryInfo info = this.memoryInfo;
    Graphics2D g2 = (Graphics2D) g;
    // g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setComposite(AlphaComposite.Clear);
    g2.fillRect(0, 0, paintingBounds.width, paintingBounds.height);
    g2.setComposite(AlphaComposite.SrcOver);
    if (info != null) {
        if (!usingTotal) {
            double usedFraction = (((double) info.getUsed() / (double) info.getMax()));
            double totalFraction = (((double) info.getTotal() / (double) info.getMax()));
            int usedWidth = (int) (paintingBounds.width * usedFraction + 0.5);
            int totalWidth = (int) (paintingBounds.width * totalFraction + 0.5);
            drawBar(g2, 0, usedWidth, paintingBounds.height, USED_COLOR);
            // g.setColor(new Color(255, 255, 0,192));
            // g.fillRect(usedWidth, 0, totalWidth-usedWidth, paintingBounds.height);
            drawBar(g2, usedWidth, totalWidth, paintingBounds.height, TOTAL_COLOR);
        } else {
            double usedFraction = (((double) info.getUsed() / (double) info.getTotal()));
            int usedWidth = (int) (paintingBounds.width * usedFraction + 0.5);
            drawBar(g2, 0, usedWidth, paintingBounds.height, USED_COLOR);
        }
        // text
        {
            String text = HumanReadable.getHumanReadableSize(info.getUsed(), usingBinaryUnits, true) + "B";
            FontRenderContext frc = g2.getFontRenderContext();
            TextLayout tl = new TextLayout(text, getFont(), frc);
            Shape s = tl.getOutline(null);
            Rectangle textBounds = s.getBounds();
            if (logger.isDebugEnabled())
                logger.debug("textBounds: {}", textBounds);
            textBounds.x = (textBounds.x * -1) + (paintingBounds.width - textBounds.width) / 2;
            textBounds.y = (textBounds.y * -1) + (paintingBounds.height - textBounds.height) / 2;
            g.translate(textBounds.x, textBounds.y);
            if (logger.isDebugEnabled())
                logger.debug("corrected textBounds: {}", textBounds);
            // FontMetrics fm = g.getFontMetrics();
            // Rectangle2D lm = fm.getStringBounds(text, g);
            // int textX=(int) (paintingBounds.width-lm.getWidth());
            // int textBase=halfHeight-fm.getHeight()/2;
            g.setColor(Color.WHITE);
            GraphicsUtilities.drawHighlight(g2, s, GRADIENT_PIXELS, 0.2f);
            g.setColor(Color.BLACK);
            // g.drawString(text,textX,textBase);
            g2.fill(s);
        }
    }
}
Also used : Shape(java.awt.Shape) Rectangle(java.awt.Rectangle) FontRenderContext(java.awt.font.FontRenderContext) GradientPaint(java.awt.GradientPaint) Graphics2D(java.awt.Graphics2D) TextLayout(java.awt.font.TextLayout)

Aggregations

FontRenderContext (java.awt.font.FontRenderContext)79 Font (java.awt.Font)27 TextLayout (java.awt.font.TextLayout)25 GlyphVector (java.awt.font.GlyphVector)18 Graphics2D (java.awt.Graphics2D)17 Rectangle2D (java.awt.geom.Rectangle2D)17 AttributedString (java.text.AttributedString)14 AffineTransform (java.awt.geom.AffineTransform)13 Paint (java.awt.Paint)8 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)7 AttributedCharacterIterator (java.text.AttributedCharacterIterator)7 Color (java.awt.Color)5 Shape (java.awt.Shape)5 LineMetrics (java.awt.font.LineMetrics)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5 Point (java.awt.Point)4 BasicStroke (java.awt.BasicStroke)3 FontMetrics (java.awt.FontMetrics)3 Rectangle (java.awt.Rectangle)3