Example usage for java.awt Graphics2D drawString

List of usage examples for java.awt Graphics2D drawString

Introduction

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

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, float x, float y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.

Usage

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

/**
 * Draws a single character./*from   ww w  .  j a  v  a 2  s .com*/
 *
 * @param g2d
 *            {@link Graphics2D} context
 * @param ch
 *            character to draw
 * @param x
 *            left x position of the character
 * @param boxWidth
 *            width of the box
 */
private void drawCharacter(Graphics2D g2d, char ch, int x, int boxWidth) {
    double degree = (rnd.nextDouble() * rotationAmplitude * 2) - rotationAmplitude;
    double scale = 1 - (rnd.nextDouble() * scaleAmplitude / 100);

    Graphics2D cg2d = (Graphics2D) g2d.create();
    cg2d.setFont(font.deriveFont(fontSize));

    cg2d.translate(x + (boxWidth / 2), height / 2);
    cg2d.rotate(degree * PI / 90);
    cg2d.scale(scale, scale);

    FontMetrics fm = cg2d.getFontMetrics();
    int charWidth = fm.charWidth(ch);
    int charHeight = fm.getAscent() + fm.getDescent();

    cg2d.drawString(String.valueOf(ch), -(charWidth / 2), fm.getAscent() - (charHeight / 2));

    cg2d.dispose();
}

From source file:org.optaplanner.examples.flightcrewscheduling.swingui.FlightCrewSchedulingWorldPanel.java

public void resetPanel(FlightCrewSolution solution) {
    translator = new LatitudeLongitudeTranslator();
    for (Airport airport : solution.getAirportList()) {
        translator.addCoordinates(airport.getLatitude(), airport.getLongitude());
    }//from ww w  .j  av a2 s . c o m

    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));
    g.setColor(TangoColorFactory.PLUM_2);
    for (Airport airport : solution.getAirportList()) {
        int x = translator.translateLongitudeToX(airport.getLongitude());
        int y = translator.translateLatitudeToY(airport.getLatitude());
        g.fillRect(x - 1, y - 1, 3, 3);
        g.drawString(StringUtils.abbreviate(airport.getCode(), 20), x + 3, y - 3);
    }
    g.setColor(TangoColorFactory.CHOCOLATE_1);
    for (Flight flight : solution.getFlightList()) {
        Airport departureAirport = flight.getDepartureAirport();
        Airport arrivalAirport = flight.getArrivalAirport();
        translator.drawRoute(g, departureAirport.getLongitude(), departureAirport.getLatitude(),
                arrivalAirport.getLongitude(), arrivalAirport.getLatitude(), true, false);
    }
    g.setFont(g.getFont().deriveFont((float) TEXT_SIZE));
    // Legend
    g.setColor(TangoColorFactory.PLUM_2);
    g.fillRect(6, (int) height - 11, 3, 3);
    g.drawString("Airport", 15, (int) height - 5);
    repaint();
}

From source file:MainClass.java

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

    Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f);
    float width = 300;
    float height = 50;
    GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g1);/*ww  w  .  ja  va  2  s.  c  o m*/
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);

    p1.setLocation(150, 200);
    p2.setLocation(250, 200);
    GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false);
    rect1.setRect(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g2);
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Acyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);
}

From source file:org.processmining.analysis.performance.sequence.Pattern.java

/**
 * Draws the pattern in the pattern diagram
 * // w  ww .  j a  v  a2s . com
 * @param lifeLines
 *            HashMap
 * @param startY
 *            int
 * @param g
 *            Graphics2D
 */
public void drawPattern(HashMap lifeLines, int startY, Graphics2D g) {
    g.setColor(Color.BLACK);
    g.drawString("Pattern " + patternNumber + ":", 10, startY + 5);
    g.setColor(color);
    // draw data-element blocks
    for (int i = 0; i < sortedDataEltBlocks.size(); i++) {
        DataElementBlock block = (DataElementBlock) sortedDataEltBlocks.get(i);
        try {
            block.drawBlock(((LifeLine) lifeLines.get(block.getDataElement())).getMiddle() - 10, color, g);
        } catch (NullPointerException ex) {
        }
    }
    // draw arrows
    ListIterator arrows = arrowList.listIterator();
    while (arrows.hasNext()) {
        Arrow arrow = (Arrow) arrows.next();
        arrow.drawArrow(lifeLines, color, g);
    }
}

From source file:savant.view.tracks.VariantTrackRenderer.java

@Override
public void drawLegend(Graphics2D g2, DrawingMode ignored) {
    int x = 6, y = 17;
    drawBaseLegend(g2, x, y, ColourKey.A, ColourKey.C, ColourKey.G, ColourKey.T);

    y += LEGEND_LINE_HEIGHT;//from  ww  w .  j  a v  a  2  s.  co  m
    g2.setColor(Color.BLACK);
    g2.fillRect(x, y - SWATCH_SIZE.height + 2, SWATCH_SIZE.width, SWATCH_SIZE.height);
    g2.setColor(Color.BLACK);
    g2.drawString("Deletion", x + SWATCH_SIZE.width + 3, y);
    x += 66;
    g2.setColor(Color.MAGENTA);
    g2.fillRect(x, y - SWATCH_SIZE.height + 2, SWATCH_SIZE.width, SWATCH_SIZE.height);
    g2.setColor(Color.BLACK);
    g2.drawString("Insertion", x + 12, y);
    y += LEGEND_LINE_HEIGHT;
    g2.drawString("Translucent = Heterozygous", 6, y);
}

From source file:MainClass.java

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

    String s = "www.java2s.com";
    Dimension d = getSize();/*from   w ww.  ja  v a 2s  . c  o  m*/

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font serifFont = new Font("Serif", Font.PLAIN, 48);
    Font sansSerifFont = new Font("Monospaced", Font.PLAIN, 48);

    AttributedString as = new AttributedString(s);
    as.addAttribute(TextAttribute.FONT, serifFont);
    as.addAttribute(TextAttribute.FONT, sansSerifFont, 2, 5);
    as.addAttribute(TextAttribute.FOREGROUND, Color.red, 2, 5);

    g2.drawString(as.getIterator(), 40, 80);

}

From source file:uk.co.real_logic.aeron.tools.perf_tools.AeronLatencyUnderLoadPublisher.java

private void plotSubset(final Graphics2D g, final int start, final int end, final String title,
        final double startX, final double width, final double stepY, final double mean) {
    final FontMetrics fm = g.getFontMetrics();
    final Color color = g.getColor();
    g.setColor(Color.black);//from w ww . java2 s.  c o m
    g.drawString(title, (int) (startX + width / 2 - fm.stringWidth(title) / 2), 975);
    final String tmp = String.format("Mean: %.3fus", mean);
    g.drawString(tmp, (int) (startX + width / 2 - fm.stringWidth(tmp) / 2), 990);
    g.setColor(color);
    final double stepX = width / (end - start);
    for (int i = start; i < end; i++) {
        final int posX = (int) (startX + stepX * (i - start));
        final int posY = 960 - ((int) (stepY * (timestamps[i] / 1000.0)) + 1);
        g.drawLine(posX, posY, posX, 960);
    }
}

From source file:org.squidy.designer.zoom.impl.InformationShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

    if (informationSource != null) {
        Graphics2D g = paintContext.getGraphics();

        PBounds bounds = getBoundsReference();
        double x = bounds.getX();
        double width = bounds.getWidth();

        String source = "Source: " + informationSource;

        g.setFont(fontName);//from   www . java2  s .  co  m
        g.drawString(name, (int) (x + 50), 230);

        g.setFont(fontSource);
        source = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), source, (int) width);
        g.drawString(source, (int) (x + width - FontUtils.getWidthOfText(g.getFontMetrics(), source)) - 20,
                130);
    }
}

From source file:TransparentText.java

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

    // the rendering quality.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // a red rectangle.
    Rectangle2D r = new Rectangle2D.Double(50, 50, 550, 100);
    g2.setPaint(Color.red);// www. ja v a 2 s .  c  om
    g2.fill(r);
    // a composite with transparency.
    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2.setComposite(c);
    // Draw some blue text.
    g2.setPaint(Color.blue);
    g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
    g2.drawString("Java Source and Support", 25, 130);
}

From source file:TrackerPanel.java

private void writeStats(Graphics2D g2d) {
    g2d.setColor(Color.WHITE);/*from   ww w . ja  v a 2s . c o m*/
    int panelHeight = getHeight();
    if (imageCount > 0) {
        double avgGrabTime = (double) totalTime / imageCount;
        g2d.drawString("Pic " + imageCount + "  " + df.format(avgGrabTime) + " ms", 5, panelHeight); // bottom left
    } else
        // no image yet
        g2d.drawString("Loading...", 5, panelHeight);
}