Example usage for java.awt Graphics2D setFont

List of usage examples for java.awt Graphics2D setFont

Introduction

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

Prototype

public abstract void setFont(Font font);

Source Link

Document

Sets this graphics context's font to the specified font.

Usage

From source file:be.fedict.eid.idp.sp.PhotoServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");
    response.setContentType("image/jpg");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    response.setHeader("Pragma", "no-cache, no-store"); // http 1.0
    response.setDateHeader("Expires", -1);
    ServletOutputStream out = response.getOutputStream();
    HttpSession session = request.getSession();

    byte[] photoData = (byte[]) session.getAttribute(PHOTO_SESSION_ATTRIBUTE);

    if (null != photoData) {
        BufferedImage photo = ImageIO.read(new ByteArrayInputStream(photoData));
        if (null == photo) {
            /*//  w w  w . j a v  a  2 s.  c  om
             * In this case we render a photo containing some error message.
             */
            photo = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = (Graphics2D) photo.getGraphics();
            RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.setRenderingHints(renderingHints);
            graphics.setColor(Color.WHITE);
            graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
            graphics.setColor(Color.RED);
            graphics.setFont(new Font("Dialog", Font.BOLD, 20));
            graphics.drawString("Photo Error", 0, 200 / 2);
            graphics.dispose();
            ImageIO.write(photo, "jpg", out);
        } else {
            out.write(photoData);
        }
    }
    out.close();
}

From source file:be.fedict.eid.applet.service.PhotoServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");
    response.setContentType("image/jpg");
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    response.setHeader("Pragma", "no-cache, no-store"); // http 1.0
    response.setDateHeader("Expires", -1);
    ServletOutputStream out = response.getOutputStream();
    HttpSession session = request.getSession();
    byte[] photoData = (byte[]) session.getAttribute(IdentityDataMessageHandler.PHOTO_SESSION_ATTRIBUTE);
    if (null != photoData) {
        BufferedImage photo = ImageIO.read(new ByteArrayInputStream(photoData));
        if (null == photo) {
            /*//from  w  w  w .  j  av a 2s.  com
             * In this case we render a photo containing some error message.
             */
            photo = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = (Graphics2D) photo.getGraphics();
            RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            graphics.setRenderingHints(renderingHints);
            graphics.setColor(Color.WHITE);
            graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
            graphics.setColor(Color.RED);
            graphics.setFont(new Font("Dialog", Font.BOLD, 20));
            graphics.drawString("Photo Error", 0, 200 / 2);
            graphics.dispose();
            ImageIO.write(photo, "jpg", out);
        } else {
            out.write(photoData);
        }
    }
    out.close();
}

From source file:net.sourceforge.processdash.ui.lib.chart.StandardDiscItemRenderer.java

protected void drawDiscLabel(Graphics2D g2, Ellipse2D shape, Paint labelPaint, Font labelFont, String label) {
    g2.setFont(labelFont);
    g2.setPaint(labelPaint);//from ww  w .j  a v a  2s  .  c om
    FontMetrics m = g2.getFontMetrics();
    int height = m.getAscent();
    double halfHeight = height / 2.0;

    double radius = shape.getWidth() / 2;
    double availableRadius = radius - getLabelPadding();
    double halfWidth = Math.sqrt(availableRadius * availableRadius - halfHeight * halfHeight);

    int width = (int) Math.floor(halfWidth * 2 + 0.99);

    Rectangle viewR = new Rectangle(width, height);
    Rectangle iconR = new Rectangle();
    Rectangle textR = new Rectangle();
    String text = SwingUtilities.layoutCompoundLabel(m, label, null, SwingConstants.CENTER,
            SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.TRAILING, viewR, iconR, textR, 0);
    if (text.equals(label) || text.length() >= 3 + minLabelChars) {
        double x = shape.getCenterX() - halfWidth + textR.x;
        double y = shape.getCenterY() + halfHeight + textR.y;
        g2.drawString(text, (float) x, (float) y);
    }
}

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 ww  w . j  a  va 2 s. c  om*/
                                }
                            }
                        }
                    }
                }
            } catch (Exception x) {
                LOG.info("Unable to retrieve sequence.", x);
            }
        }
    }
}

From source file:test.unit.be.fedict.eid.applet.service.KmlGeneratorTest.java

@Test
public void identityWithAddressAndPhotoKml() throws Exception {
    // setup//from   ww  w. java 2 s . co  m
    Identity identity = new Identity();
    identity.name = "Test Name";
    identity.firstName = "Test First name";
    identity.dateOfBirth = new GregorianCalendar();
    identity.gender = Gender.MALE;

    Address address = new Address();
    address.streetAndNumber = "Test Street 1A";
    address.zip = "1234";
    address.municipality = "Test Municipality";

    BufferedImage image = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHints(renderingHints);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
    graphics.setFont(new Font("Dialog", Font.BOLD, 20));
    graphics.setColor(Color.BLACK);
    graphics.drawString("Test Photo", 0, 200 / 2);
    graphics.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] photo = baos.toByteArray();

    EIdData eIdData = new EIdData();
    eIdData.identity = identity;
    eIdData.address = address;
    eIdData.photo = photo;

    // operate
    byte[] document = this.testedInstance.generateKml(eIdData);

    // verify
    assertNotNull(document);
    assertTrue(document.length > 0);

    toTmpFile(document);
}

From source file:test.unit.be.fedict.eid.applet.service.PdfGeneratorTest.java

@Test
public void identityWithAddressAndPhotoPdf() throws Exception {
    // setup//from   w  w  w. j  ava2 s. co  m
    Identity identity = new Identity();
    identity.name = "Test Name";
    identity.firstName = "Test First name";
    identity.dateOfBirth = new GregorianCalendar();
    identity.gender = Gender.MALE;

    Address address = new Address();
    address.streetAndNumber = "Test Street 1A";
    address.zip = "1234";
    address.municipality = "Test Municipality";

    BufferedImage image = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHints(renderingHints);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
    graphics.setFont(new Font("Dialog", Font.BOLD, 20));
    graphics.setColor(Color.BLACK);
    graphics.drawString("Test Photo", 0, 200 / 2);
    graphics.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] photo = baos.toByteArray();

    EIdData eIdData = new EIdData();
    eIdData.identity = identity;
    eIdData.address = address;
    eIdData.photo = photo;

    // operate
    byte[] document = this.testedInstance.generatePdf(eIdData);

    // verify
    assertNotNull(document);
    assertTrue(document.length > 0);

    toTmpFile(document);
}

From source file:test.unit.be.fedict.eid.applet.service.VcardGeneratorTest.java

@Test
public void identityWithAddressAndPhotoVcard() throws Exception {
    // setup//from  www .  ja v  a 2s .  c  o  m
    Identity identity = new Identity();
    identity.name = "Test Name";
    identity.firstName = "Test First name";
    identity.dateOfBirth = new GregorianCalendar();
    identity.gender = Gender.MALE;

    Address address = new Address();
    address.streetAndNumber = "Test Street 1A";
    address.zip = "1234";
    address.municipality = "Test Municipality";

    BufferedImage image = new BufferedImage(140, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.setRenderingHints(renderingHints);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(1, 1, 140 - 1 - 1, 200 - 1 - 1);
    graphics.setFont(new Font("Dialog", Font.BOLD, 20));
    graphics.setColor(Color.BLACK);
    graphics.drawString("Test Photo", 0, 200 / 2);
    graphics.dispose();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", baos);
    byte[] photo = baos.toByteArray();

    EIdData eIdData = new EIdData();
    eIdData.identity = identity;
    eIdData.address = address;
    eIdData.photo = photo;

    // operate
    byte[] document = this.testedInstance.generateVcard(eIdData);

    // verify
    assertNotNull(document);
    assertTrue(document.length > 0);

    toTmpFile(document);
}

From source file:org.esa.snap.graphbuilder.rcp.dialogs.support.GraphPanel.java

/**
 * Draw the graphical representation of the Graph
 *
 * @param g        the Graphics//from   w w  w.j av a2  s  .c o m
 * @param nodeList the list of graphNodes
 */
private void DrawGraph(Graphics2D g, List<GraphNode> nodeList) {

    g.setFont(font);
    if (showRightClickHelp) {
        drawHelp(g);
    }

    for (GraphNode n : nodeList) {

        if (n == selectedNode)
            n.drawNode(g, selColor);
        else
            n.drawNode(g, opColor);
    }

    // first pass sets the Size in drawNode according to string length
    for (GraphNode n : nodeList) {
        // connect source nodes
        g.setColor(Color.red);
        final NodeSource[] nSources = n.getNode().getSources();
        for (NodeSource nSource : nSources) {
            final GraphNode srcNode = graphEx.getGraphNodeList().findGraphNode(nSource.getSourceNodeId());
            if (srcNode != null)
                n.drawConnectionLine(g, srcNode);
        }
    }

    if (showHeadHotSpot && selectedNode != null) {
        selectedNode.drawHeadHotspot(g, Color.red);
    }
    if (showTailHotSpot && selectedNode != null) {
        selectedNode.drawTailHotspot(g, Color.red);
    }
    if (connectingSourceFromHead && connectSourceTargetNode != null) {
        final Point p1 = connectSourceTargetNode.getPos();
        final Point p2 = connectingSourcePos;
        if (p1 != null && p2 != null) {
            g.setColor(Color.red);
            g.drawLine(p1.x, p1.y + connectSourceTargetNode.getHalfNodeHeight(), p2.x, p2.y);
        }
    } else if (connectingSourceFromTail && connectSourceTargetNode != null) {
        final Point p1 = connectSourceTargetNode.getPos();
        final Point p2 = connectingSourcePos;
        if (p1 != null && p2 != null) {
            g.setColor(Color.red);
            g.drawLine(p1.x + connectSourceTargetNode.getWidth(),
                    p1.y + connectSourceTargetNode.getHalfNodeHeight(), p2.x, p2.y);
        }
    }
}

From source file:image.writer.ImageWriterExample1.java

/**
 * Paints a few things on a Graphics2D instance.
 * @param g2d the Graphics2D instance//  ww w .  j av  a2s  .c om
 * @param pageNum a page number
 */
protected void paintSome(Graphics2D g2d, int pageNum) {
    //Paint a bounding box
    g2d.drawRect(0, 0, 400, 200);

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    int c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(70, 90, 50, 50);
        copy.rotate(-2 * Math.PI / (double) c, 70, 90);
    }
    copy.dispose();

    //Some text
    copy = (Graphics2D) g2d.create();
    copy.rotate(-0.25);
    copy.setColor(Color.RED);
    copy.setFont(new Font("sans-serif", Font.PLAIN, 36));
    copy.drawString("Hello world!", 140, 140);
    copy.setColor(Color.RED.darker());
    copy.setFont(new Font("serif", Font.PLAIN, 36));
    copy.drawString("Hello world!", 140, 180);
    copy.dispose();

    //Try attributed text
    AttributedString aString = new AttributedString("This is attributed text.");
    aString.addAttribute(TextAttribute.FAMILY, "SansSerif");
    aString.addAttribute(TextAttribute.FAMILY, "Serif", 8, 18);
    aString.addAttribute(TextAttribute.FOREGROUND, Color.orange, 8, 18);
    g2d.drawString(aString.getIterator(), 250, 170);

    g2d.drawString("Page: " + pageNum, 250, 190);
}

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);
    Rectangle2D bounds = g2d.getFontMetrics().getStringBounds("Kein Zeitfenster aktiv", g);
    int dx = 10;/*from w  ww  .j  a v  a2  s. c o  m*/
    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);
}