Example usage for java.awt Graphics2D getFont

List of usage examples for java.awt Graphics2D getFont

Introduction

In this page you can find the example usage for java.awt Graphics2D getFont.

Prototype

public abstract Font getFont();

Source Link

Document

Gets the current font.

Usage

From source file:org.slage.TextObject.java

/**
 * Draw text at the object's position/*from  w ww . ja v a2 s  . c o  m*/
 * 
 * @param G2D graphics context to render the text to
 */
public void draw(java.awt.Graphics2D G2D) {

    Font oldFont = G2D.getFont();
    Color oldColor = G2D.getColor();

    G2D.setFont(font);
    G2D.setColor(color);

    G2D.drawString(getName(), getPosition().x, getPosition().y);

    G2D.setFont(oldFont);
    G2D.setColor(oldColor);
}

From source file:org.optaplanner.examples.rocktour.swingui.RockTourWorldPanel.java

public void resetPanel(RockTourSolution solution) {
    translator = new LatitudeLongitudeTranslator();
    RockBus bus = solution.getBus();//from  ww  w  .ja v  a  2  s. c om
    translator.addCoordinates(bus.getStartLocation().getLatitude(), bus.getStartLocation().getLongitude());
    translator.addCoordinates(bus.getEndLocation().getLatitude(), bus.getEndLocation().getLongitude());
    for (RockShow show : solution.getShowList()) {
        translator.addCoordinates(show.getLocation().getLatitude(), show.getLocation().getLongitude());
    }

    Dimension size = getSize();
    double width = size.getWidth();
    double height = size.getHeight();
    translator.prepareFor(width, height);

    Graphics2D g = createCanvas(width, height);
    g.setFont(g.getFont().deriveFont((float) LOCATION_NAME_TEXT_SIZE));
    List<RockShow> showList = solution.getShowList();
    int maxAvailableDateSetSize = showList.stream().mapToInt(show -> show.getAvailableDateSet().size()).max()
            .orElse(-1);
    for (RockShow show : showList) {
        RockLocation location = show.getLocation();
        int x = translator.translateLongitudeToX(location.getLongitude());
        int y = translator.translateLatitudeToY(location.getLatitude());
        double percentage = (double) show.getAvailableDateSet().size() / maxAvailableDateSetSize;
        g.setColor(TangoColorFactory.buildPercentageColor(TangoColorFactory.PLUM_3, TangoColorFactory.PLUM_1,
                percentage));
        g.fillRect(x - 1, y - 1, 3, 3);
        if (location.getCityName() != null && showList.size() <= 500) {
            g.drawString(StringUtils.abbreviate(location.getCityName(), 20), x + 3, y - 3);
        }
        if (show.getDate() != null) {
            g.drawString(DAY_FORMATTER.format(show.getDate()), x + 3, y - 3 + LOCATION_NAME_TEXT_SIZE * 3 / 2);
        }
    }
    g.setColor(TangoColorFactory.ALUMINIUM_4);
    RockLocation busStartLocation = bus.getStartLocation();
    int domicileX = translator.translateLongitudeToX(busStartLocation.getLongitude());
    int domicileY = translator.translateLatitudeToY(busStartLocation.getLatitude());
    g.fillRect(domicileX - 2, domicileY - 2, 5, 5);
    if (busStartLocation.getCityName() != null && showList.size() <= 500) {
        g.drawString(busStartLocation.getCityName(), domicileX + 3, domicileY - 3);
    }
    Set<RockShow> needsBackToDomicileLineSet = new HashSet<>(showList);
    for (RockShow trailingShow : showList) {
        if (trailingShow.getPreviousStandstill() instanceof RockShow) {
            needsBackToDomicileLineSet.remove(trailingShow.getPreviousStandstill());
        }
    }
    g.setColor(TangoColorFactory.CHOCOLATE_1);
    for (RockShow show : showList) {
        if (show.getPreviousStandstill() != null) {
            RockLocation previousLocation = show.getPreviousStandstill().getDepartureLocation();
            RockLocation location = show.getLocation();
            translator.drawRoute(g, previousLocation.getLongitude(), previousLocation.getLatitude(),
                    location.getLongitude(), location.getLatitude(), true, false);
            // Back to bus line
            if (needsBackToDomicileLineSet.contains(show)) {
                translator.drawRoute(g, location.getLongitude(), location.getLatitude(),
                        busStartLocation.getLongitude(), busStartLocation.getLatitude(), true, true);
            }
        }
    }
    g.setFont(g.getFont().deriveFont((float) TEXT_SIZE));
    // Legend
    g.setColor(TangoColorFactory.ALUMINIUM_4);
    g.fillRect(5, (int) height - 17 - TEXT_SIZE, 5, 5);
    g.drawString("Bus start", 15, (int) height - 10 - TEXT_SIZE);
    g.setColor(TangoColorFactory.PLUM_2);
    g.fillRect(6, (int) height - 11, 3, 3);
    g.drawString("Show (darker means less available)", 15, (int) height - 5);
    repaint();
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setFont(new Font("Serif", Font.PLAIN, 48));

    FontRenderContext frc = g2.getFontRenderContext();
    String s = "www.java2s.com";
    Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);
    float width = (float) bounds.getWidth();
    int centerX = 100;
    int baselineY = 70;
    g2.drawString(s, centerX - width / 2, baselineY);
}

From source file:org.apache.fop.render.java2d.Java2DPainter.java

/** {@inheritDoc} */
public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[] dx, String text)
        throws IFException {
    g2dState.updateColor(state.getTextColor());
    FontTriplet triplet = new FontTriplet(state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
    //TODO Ignored: state.getFontVariant()
    //TODO Opportunity for font caching if font state is more heavily used
    Font font = getFontInfo().getFontInstance(triplet, state.getFontSize());
    //String fontName = font.getFontName();
    //float fontSize = state.getFontSize() / 1000f;
    g2dState.updateFont(font.getFontName(), state.getFontSize() * 1000);

    Graphics2D g2d = this.g2dState.getGraph();
    GlyphVector gv = g2d.getFont().createGlyphVector(g2d.getFontRenderContext(), text);
    Point2D cursor = new Point2D.Float(0, 0);

    int l = text.length();
    int dxl = (dx != null ? dx.length : 0);

    if (dx != null && dxl > 0 && dx[0] != 0) {
        cursor.setLocation(cursor.getX() - (dx[0] / 10f), cursor.getY());
        gv.setGlyphPosition(0, cursor);/*from  www.  j a  v  a  2s. com*/
    }
    for (int i = 0; i < l; i++) {
        char orgChar = text.charAt(i);
        float glyphAdjust = 0;
        int cw = font.getCharWidth(orgChar);

        if ((wordSpacing != 0) && CharUtilities.isAdjustableSpace(orgChar)) {
            glyphAdjust += wordSpacing;
        }
        glyphAdjust += letterSpacing;
        if (dx != null && i < dxl - 1) {
            glyphAdjust += dx[i + 1];
        }

        cursor.setLocation(cursor.getX() + cw + glyphAdjust, cursor.getY());
        gv.setGlyphPosition(i + 1, cursor);
    }
    g2d.drawGlyphVector(gv, x, y);
}

From source file:org.polymap.core.data.feature.FeatureRenderProcessor2.java

protected void drawErrorMsg(Graphics2D g, String msg, Throwable e) {
    g.setColor(Color.RED);//from  w  w w . j av a 2s.c o m
    g.setStroke(new BasicStroke(1));
    g.getFont().deriveFont(Font.BOLD, 12);
    if (msg != null) {
        g.drawString(msg, 10, 10);
    }
    if (e != null) {
        g.drawString(e.toString(), 10, 30);
    }
}

From source file:BookTest.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page >= 1)
        return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.black);//  w  ww  . ja  va  2  s .  c o m
    g2.translate(pf.getImageableX(), pf.getImageableY());
    FontRenderContext context = g2.getFontRenderContext();
    Font f = g2.getFont();
    TextLayout layout = new TextLayout(title, f, context);
    float ascent = layout.getAscent();
    g2.drawString(title, 0, ascent);
    return Printable.PAGE_EXISTS;
}

From source file:pl.edu.icm.visnow.system.main.VisNow.java

private static void renderSplashFrame(float progress, String loadText, String bottomTextUpperLine,
        String bottomTextLowerLine) {
    if (splash == null) {
        return;// www . j a v a2 s.c om
    }

    try {
        Graphics2D g = splash.createGraphics();
        if (g == null) {
            return;
        }

        if (!splash.isVisible())
            return;

        Rectangle bounds = splash.getBounds();
        Font f = g.getFont();
        FontMetrics fm = g.getFontMetrics(f);
        java.awt.geom.Rectangle2D rect = fm.getStringBounds(loadText, g);
        int texth = (int) Math.ceil(rect.getHeight());
        g.setComposite(AlphaComposite.Clear);
        //g.setColor(Color.RED);
        g.fillRect(PROGRESS_TEXT_X_POSITION, bounds.height - PROGRESS_TEXT_Y_MARGIN - texth - 5,
                bounds.width - PROGRESS_TEXT_X_POSITION, texth + 10);

        g.setFont(lowerLineFont);
        fm = g.getFontMetrics(g.getFont());
        rect = fm.getStringBounds(bottomTextLowerLine, g);
        int lowerLineTextHeight = (int) Math.ceil(rect.getHeight());
        g.fillRect(BOTTOM_TEXT_X_MARGIN, bounds.height - BOTTOM_TEXT_Y_MARGIN - lowerLineTextHeight - 5,
                bounds.width - BOTTOM_TEXT_X_MARGIN, lowerLineTextHeight + 10);

        g.setFont(f);
        fm = g.getFontMetrics(g.getFont());
        rect = fm.getStringBounds(bottomTextUpperLine, g);
        texth = (int) Math.ceil(rect.getHeight());
        g.fillRect(BOTTOM_TEXT_X_MARGIN, bounds.height - lowerLineTextHeight - BOTTOM_TEXT_Y_MARGIN - texth - 5,
                bounds.width - BOTTOM_TEXT_X_MARGIN, lowerLineTextHeight + 10);

        g.setPaintMode();
        //        g.setColor(Color.BLACK);
        g.setColor(new Color(0, 75, 50));
        g.drawString(loadText, PROGRESS_TEXT_X_POSITION, bounds.height - PROGRESS_TEXT_Y_MARGIN);
        g.drawString(bottomTextUpperLine, BOTTOM_TEXT_X_MARGIN,
                bounds.height - lowerLineTextHeight - BOTTOM_TEXT_Y_MARGIN);
        g.setFont(lowerLineFont);
        g.drawString(bottomTextLowerLine, BOTTOM_TEXT_X_MARGIN, bounds.height - BOTTOM_TEXT_Y_MARGIN);
        g.setFont(f);

        //        g.setColor(Color.BLACK);
        g.setColor(new Color(0, 150, 100));
        g.drawRect(PROGRESS_BAR_X_MARGIN, bounds.height - PROGRESS_BAR_Y_MARGIN,
                bounds.width - PROGRESS_BAR_X_MARGIN, PROGRESS_BAR_HEIGHT);
        int progressWidth = bounds.width - 2 * PROGRESS_BAR_X_MARGIN;
        int done = (int) (progressWidth * progress);
        g.fillRect(PROGRESS_BAR_X_MARGIN, bounds.height - PROGRESS_BAR_Y_MARGIN, PROGRESS_BAR_X_MARGIN + done,
                PROGRESS_BAR_HEIGHT);
        if (progress >= 1.0f) {
            g.fillRect(PROGRESS_BAR_X_MARGIN, bounds.height - PROGRESS_BAR_Y_MARGIN,
                    bounds.width - PROGRESS_BAR_X_MARGIN, PROGRESS_BAR_HEIGHT);
        }

        splash.update();
    } catch (IllegalStateException ex) {
    }
}

From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java

/**Render default view if there is no timeframe yet*/
private void renderNoInfoView(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(new TexturePaint(STROKED, new Rectangle(0, 0, 3, 3)));
    g2d.fillRect(0, 0, getWidth(), getHeight());
    Font f = g2d.getFont().deriveFont(Font.BOLD, 14.0f);
    g2d.setFont(f);/*w  w w  .  j  av  a2  s . c  om*/
    Rectangle2D bounds = g2d.getFontMetrics().getStringBounds("Kein Zeitfenster aktiv", g);
    int dx = 10;
    if (getWidth() > bounds.getWidth()) {
        dx = (int) Math.rint((getWidth() - bounds.getWidth()) / 2);
    }

    int dy = 10;
    if (getHeight() > bounds.getHeight()) {
        dy = (int) Math.rint((getHeight() - bounds.getHeight()) / 2);
    }
    g2d.setColor(Color.black);
    g2d.drawString("Kein Zeitfenster aktiv", dx, dy);
}

From source file:savant.amino.AminoCanvas.java

@Override
public void paintComponent(Graphics g) {
    if (GenomeUtils.getGenome().isSequenceSet()) {

        double aminoWidth = track.transformXPos(3) - track.transformXPos(0);
        if (aminoWidth > 0.5) {

            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // We'll be drawing labels if a 'W' (tryptophan) will fit into the space of 3 bases.
            g2.setFont(g2.getFont().deriveFont(Font.PLAIN, 8));
            boolean labelled = g2.getFontMetrics().charWidth('W') < aminoWidth;

            try {
                List<Record> records = track.getDataInRange();
                if (records != null) {
                    for (Record r : records) {
                        RichIntervalRecord rr = (RichIntervalRecord) r;
                        int recordStart = rr.getInterval().getStart();
                        thickStart = rr.getThickStart();
                        int thickEnd = rr.getThickEnd() + 1;
                        LOG.debug(rr.getAlternateName() + ": thickStart=" + thickStart + ", thickEnd="
                                + thickEnd);

                        if (thickEnd > thickStart) {
                            sequence = GenomeUtils.getGenome().getSequence(
                                    NavigationUtils.getCurrentReferenceName(),
                                    RangeUtils.createRange(thickStart, thickEnd));

                            int pos = thickStart;
                            int leftovers = -1; // Left-overs from the previous block.
                            List<Block> blocks = rr.getBlocks();
                            if (blocks != null) {
                                for (Block b : blocks) {

                                    if (pos + 3 <= thickEnd) {
                                        // Block positions are relative to the start of the record.
                                        int blockStart = b.getPosition() + recordStart;
                                        int blockEnd = b.getEnd() + recordStart;
                                        LOG.debug(rr.getAlternateName() + ": blockStart=" + blockStart
                                                + ", blockEnd=" + blockEnd);

                                        AminoAcid a;

                                        // If we have leftovers, take care of them first.
                                        switch (leftovers) {
                                        case -1:
                                            // Fresh record with no leftovers.
                                            break;
                                        case 0:
                                            // No leftovers, so we can start immediately on the new block.
                                            pos = blockStart;
                                            if (pos < thickStart) {
                                                pos = thickStart;
                                            }
                                            break;
                                        case 1:
                                            // One base from previous block, two bases from current one.
                                            LOG.debug(rr.getAlternateName() + ": handling leftover "
                                                    + getBase(pos) + " at " + pos);
                                            if (rr.getStrand() == Strand.FORWARD) {
                                                a = AminoAcid.lookup(getBase(pos), getBase(blockStart),
                                                        getBase(blockStart + 1));
                                            } else {
                                                a = AminoAcid.lookup(getComplement(blockStart + 1),
                                                        getComplement(blockStart), getComplement(pos));
                                            }
                                            paintAminoAcid(g2, a, pos, 1, pos, labelled);
                                            paintAminoAcid(g2, a, blockStart, 2, blockStart - 1, labelled);
                                            pos = blockStart + 2;
                                            break;
                                        case 2:
                                            // Two bases from previous block, one base from current one.
                                            LOG.debug(rr.getAlternateName() + ": handling leftover "
                                                    + getBase(pos) + "," + getBase(pos + 1) + " at " + pos + ","
                                                    + (pos + 1));
                                            if (rr.getStrand() == Strand.FORWARD) {
                                                a = AminoAcid.lookup(getBase(pos), getBase(pos + 1),
                                                        getBase(blockStart));
                                            } else {
                                                a = AminoAcid.lookup(getComplement(blockStart),
                                                        getComplement(pos + 1), getComplement(pos));
                                            }
                                            paintAminoAcid(g2, a, pos, 2, pos, labelled);
                                            paintAminoAcid(g2, a, blockStart, 1, blockStart - 2, labelled);
                                            pos = blockStart + 1;
                                            break;
                                        }

                                        // Now, handle codons which are entirely contained within the block.
                                        while (pos + 3 <= blockEnd && pos + 3 <= thickEnd) {
                                            if (rr.getStrand() == Strand.FORWARD) {
                                                a = AminoAcid.lookup(getBase(pos), getBase(pos + 1),
                                                        getBase(pos + 2));
                                            } else {
                                                a = AminoAcid.lookup(getComplement(pos + 2),
                                                        getComplement(pos + 1), getComplement(pos));
                                            }
                                            paintAminoAcid(g2, a, pos, 3, pos, labelled);
                                            pos += 3;
                                        }
                                        leftovers = (blockEnd - pos) % 3;
                                        LOG.debug(rr.getAlternateName() + ": breaking out of loop: pos=" + pos
                                                + ", blockEnd=" + blockEnd + ", leftovers=" + leftovers);
                                    }/*from  w  w  w .  j  av  a  2s.co m*/
                                }
                            }
                        }
                    }
                }
            } catch (Exception x) {
                LOG.info("Unable to retrieve sequence.", x);
            }
        }
    }
}

From source file:com.celements.photo.image.GenerateThumbnail.java

private FontMetrics calcWatermarkFontSize(String watermark, int width, Graphics2D g2d) {
    FontMetrics metrics;/*from  w  ww. j av a 2 s. c o  m*/
    int fontSize = 1;
    do {
        metrics = g2d.getFontMetrics(new Font(g2d.getFont().getFontName(), Font.BOLD, fontSize));
        fontSize++;
    } while (metrics.stringWidth(watermark) < (0.8 * width));
    return metrics;
}