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:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private void drawInformationBox(Graphics2D g2, JXLayer<? extends V> layer) {
    if (JStock.instance().getJStockOptions()
            .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Hide) {
        return;/*from   w  w w  .j av  a 2s  .  c  om*/
    }

    final Font oldFont = g2.getFont();
    final Font paramFont = oldFont;
    final FontMetrics paramFontMetrics = g2.getFontMetrics(paramFont);
    final Font valueFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() + 1);
    final FontMetrics valueFontMetrics = g2.getFontMetrics(valueFont);
    final Font dateFont = oldFont.deriveFont((float) oldFont.getSize() - 1);
    final FontMetrics dateFontMetrics = g2.getFontMetrics(dateFont);

    final List<ChartData> chartDatas = this.chartJDialog.getChartDatas();
    List<String> values = new ArrayList<String>();
    final ChartData chartData = chartDatas.get(this.mainTraceInfo.getDataIndex());

    // Number formats are generally not synchronized. It is recommended to create separate format instances for each thread. 
    // If multiple threads access a format concurrently, it must be synchronized externally.
    // http://stackoverflow.com/questions/2213410/usage-of-decimalformat-for-the-following-case
    final DecimalFormat integerFormat = new DecimalFormat("###,###");

    // It is common to use OHLC for chat, instead of using PrevPrice.        
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.openPrice));
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.highPrice));
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.lowPrice));
    values.add(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(chartData.lastPrice));
    values.add(integerFormat.format(chartData.volume));

    final List<String> indicatorParams = new ArrayList<String>();
    final List<String> indicatorValues = new ArrayList<String>();
    final DecimalFormat decimalFormat = new DecimalFormat("0.00");
    for (TraceInfo indicatorTraceInfo : this.indicatorTraceInfos) {
        final int plotIndex = indicatorTraceInfo.getPlotIndex();
        final int seriesIndex = indicatorTraceInfo.getSeriesIndex();
        final int dataIndex = indicatorTraceInfo.getDataIndex();
        final String name = this.getLegendName(plotIndex, seriesIndex);
        final Number value = this.getValue(plotIndex, seriesIndex, dataIndex);
        if (name == null || value == null) {
            continue;
        }
        indicatorParams.add(name);
        indicatorValues.add(decimalFormat.format(value));
    }

    assert (values.size() == params.size());
    int index = 0;
    final int paramValueWidthMargin = 10;
    final int paramValueHeightMargin = 0;
    // Slightly larger than dateInfoHeightMargin, as font for indicator is
    // larger than date's.
    final int infoIndicatorHeightMargin = 8;
    int maxInfoWidth = -1;
    // paramFontMetrics will always "smaller" than valueFontMetrics.
    int totalInfoHeight = Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight()) * values.size()
            + paramValueHeightMargin * (values.size() - 1);
    for (String param : params) {
        final String value = values.get(index++);
        final int paramStringWidth = paramFontMetrics.stringWidth(param + ":") + paramValueWidthMargin
                + valueFontMetrics.stringWidth(value);
        if (maxInfoWidth < paramStringWidth) {
            maxInfoWidth = paramStringWidth;
        }
    }

    if (indicatorValues.size() > 0) {
        totalInfoHeight += infoIndicatorHeightMargin;
        totalInfoHeight += Math.max(paramFontMetrics.getHeight(), valueFontMetrics.getHeight())
                * indicatorValues.size() + paramValueHeightMargin * (indicatorValues.size() - 1);
        index = 0;
        for (String indicatorParam : indicatorParams) {
            final String indicatorValue = indicatorValues.get(index++);
            final int paramStringWidth = paramFontMetrics.stringWidth(indicatorParam + ":")
                    + paramValueWidthMargin + valueFontMetrics.stringWidth(indicatorValue);
            if (maxInfoWidth < paramStringWidth) {
                maxInfoWidth = paramStringWidth;
            }
        }
    }

    final Date date = new Date(chartData.timestamp);

    // Date formats are not synchronized. It is recommended to create separate format instances for each thread.
    // If multiple threads access a format concurrently, it must be synchronized externally.
    final SimpleDateFormat simpleDateFormat = this.simpleDataFormatThreadLocal.get();
    final String dateString = simpleDateFormat.format(date);
    final int dateStringWidth = dateFontMetrics.stringWidth(dateString);
    final int dateStringHeight = dateFontMetrics.getHeight();
    // We want to avoid information box from keep changing its width while
    // user moves along the mouse. This will prevent user from feeling,
    // information box is flickering, which is uncomfortable to user's eye.
    final int maxStringWidth = Math.max(dateFontMetrics.stringWidth(longDateString),
            Math.max(this.maxWidth, Math.max(dateStringWidth, maxInfoWidth)));
    if (maxStringWidth > this.maxWidth) {
        this.maxWidth = maxStringWidth;
    }
    final int dateInfoHeightMargin = 5;
    final int maxStringHeight = dateStringHeight + dateInfoHeightMargin + totalInfoHeight;

    final int padding = 5;
    final int boxPointMargin = 8;
    final int width = maxStringWidth + (padding << 1);
    final int height = maxStringHeight + (padding << 1);

    /* Get Border Rect Information. */
    /*
    fillRect(1, 1, 1, 1);   // O is rect pixel
            
    xxx
    xOx
    xxx
            
    drawRect(0, 0, 2, 2);   // O is rect pixel
            
    OOO
    OxO
    OOO
     */
    final int borderWidth = width + 2;
    final int borderHeight = height + 2;
    // On left side of the ball.
    final double suggestedBorderX = this.mainTraceInfo.getPoint().getX() - borderWidth - boxPointMargin;
    final double suggestedBorderY = this.mainTraceInfo.getPoint().getY() - (borderHeight >> 1);
    double bestBorderX = 0;
    double bestBorderY = 0;
    if (JStock.instance().getJStockOptions()
            .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Stay) {
        if (this.mainTraceInfo.getPoint()
                .getX() > ((int) (this.mainDrawArea.getX() + this.mainDrawArea.getWidth() + 0.5) >> 1)) {
            bestBorderX = this.mainDrawArea.getX();
            bestBorderY = this.mainDrawArea.getY();
        } else {
            bestBorderX = this.mainDrawArea.getX() + this.mainDrawArea.getWidth() - borderWidth;
            bestBorderY = this.mainDrawArea.getY();
        }
    } else {
        assert (JStock.instance().getJStockOptions()
                .getYellowInformationBoxOption() == JStockOptions.YellowInformationBoxOption.Follow);
        bestBorderX = suggestedBorderX > this.mainDrawArea.getX()
                ? (suggestedBorderX + borderWidth) < (this.mainDrawArea.getX() + this.mainDrawArea.getWidth())
                        ? suggestedBorderX
                        : this.mainDrawArea.getX() + this.mainDrawArea.getWidth() - borderWidth - boxPointMargin
                : this.mainTraceInfo.getPoint().getX() + boxPointMargin;
        bestBorderY = suggestedBorderY > this.mainDrawArea.getY()
                ? (suggestedBorderY + borderHeight) < (this.mainDrawArea.getY() + this.mainDrawArea.getHeight())
                        ? suggestedBorderY
                        : this.mainDrawArea.getY() + this.mainDrawArea.getHeight() - borderHeight
                                - boxPointMargin
                : this.mainDrawArea.getY() + boxPointMargin;
    }

    final double x = bestBorderX + 1;
    final double y = bestBorderY + 1;

    final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Composite oldComposite = g2.getComposite();
    final Color oldColor = g2.getColor();

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(COLOR_BORDER);
    g2.drawRoundRect((int) (bestBorderX + 0.5), (int) (bestBorderY + 0.5), borderWidth - 1, borderHeight - 1,
            15, 15);
    g2.setColor(COLOR_BACKGROUND);
    g2.setComposite(Utils.makeComposite(0.75f));
    g2.fillRoundRect((int) (x + 0.5), (int) (y + 0.5), width, height, 15, 15);
    g2.setComposite(oldComposite);
    g2.setColor(oldColor);

    int yy = (int) (y + padding + dateFontMetrics.getAscent() + 0.5);
    g2.setFont(dateFont);
    g2.setColor(COLOR_BLUE);
    g2.drawString(dateString, (int) (((width - dateFontMetrics.stringWidth(dateString)) >> 1) + x + 0.5), yy);

    index = 0;
    yy += dateFontMetrics.getDescent() + dateInfoHeightMargin + valueFontMetrics.getAscent();
    final String CLOSE_STR = GUIBundle.getString("StockHistory_Close");
    for (String param : params) {
        final String value = values.get(index++);
        g2.setColor(Color.BLACK);
        if (param.equals(CLOSE_STR)) {
            // It is common to use OHLC for chat, instead of using PrevPrice.
            final double changePrice = chartData.lastPrice - chartData.openPrice;
            if (changePrice > 0.0) {
                g2.setColor(JStockOptions.DEFAULT_HIGHER_NUMERICAL_VALUE_FOREGROUND_COLOR);
            } else if (changePrice < 0.0) {
                g2.setColor(JStockOptions.DEFAULT_LOWER_NUMERICAL_VALUE_FOREGROUND_COLOR);
            }
        }
        g2.setFont(paramFont);
        g2.drawString(param + ":", (int) (padding + x + 0.5), yy);
        g2.setFont(valueFont);
        g2.drawString(value, (int) (width - padding - valueFontMetrics.stringWidth(value) + x + 0.5), yy);
        // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent()
        yy += paramValueHeightMargin + valueFontMetrics.getHeight();
    }

    g2.setColor(Color.BLACK);
    yy -= paramValueHeightMargin;
    yy += infoIndicatorHeightMargin;

    index = 0;
    for (String indicatorParam : indicatorParams) {
        final String indicatorValue = indicatorValues.get(index++);
        g2.setFont(paramFont);
        g2.drawString(indicatorParam + ":", (int) (padding + x + 0.5), yy);
        g2.setFont(valueFont);
        g2.drawString(indicatorValue,
                (int) (width - padding - valueFontMetrics.stringWidth(indicatorValue) + x + 0.5), yy);
        // Same as yy += valueFontMetrics.getDescent() + paramValueHeightMargin + valueFontMetrics.getAscent()
        yy += paramValueHeightMargin + valueFontMetrics.getHeight();
    }

    g2.setColor(oldColor);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias);
    g2.setFont(oldFont);
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public void repaintGCSkew(List<Sequence> selclist, Graphics2D g2, int size, GeneGroup gg, String selspec) {
    g2.setColor(Color.white);/*from   w  w  w.  ja  v  a2  s .  c  o m*/
    g2.fillRect(0, 0, 1024, 1024);
    g2.setFont(g2.getFont().deriveFont(10.0f));
    int total = 0;
    int g = 0;
    int c = 0;
    double gcstotal = 0.0;
    for (Sequence ctg : selclist) {
        //Sequence ctg = clist.get( u );
        if (gg != null) {
            for (Tegeval tv : gg.getTegevals()) {
                if (tv.getContshort() == ctg) {
                    int i = tv.start;

                    int x1 = (int) (512.0 + (384.0 - 100.0) * Math.cos((i + total) * 2.0 * Math.PI / size));
                    int y1 = (int) (512.0 + (384.0 - 100.0) * Math.sin((i + total) * 2.0 * Math.PI / size));
                    int x2 = (int) (512.0 + (384.0 + 100.0) * Math.cos((i + total) * 2.0 * Math.PI / size));
                    int y2 = (int) (512.0 + (384.0 + 100.0) * Math.sin((i + total) * 2.0 * Math.PI / size));

                    g2.setColor(Color.green);
                    g2.drawLine(x1, y1, x2, y2);
                }
            }
        }

        double horn = total * 2.0 * Math.PI / size;
        double horn2 = (total + ctg.length()) * 2.0 * Math.PI / size;

        int x1 = (int) (512.0 + (384.0 - 100.0) * Math.cos(horn));
        int y1 = (int) (512.0 + (384.0 - 100.0) * Math.sin(horn));
        int x2 = (int) (512.0 + (384.0 + 100.0) * Math.cos(horn));
        int y2 = (int) (512.0 + (384.0 + 100.0) * Math.sin(horn));
        g2.setColor(Color.black);
        g2.drawLine(x1, y1, x2, y2);

        int xoff = (int) (512.0 + (384.0 + 100.0) * Math.cos(horn2));
        int yoff = (int) (512.0 + (384.0 + 100.0) * Math.sin(horn2));
        if (horn < Math.PI) {
            g2.translate(x2, y2);
            g2.rotate(horn + Math.PI / 2.0);
            g2.drawString(ctg.getName(), 0, 0);
            g2.rotate(-horn - Math.PI / 2.0);
            g2.translate(-x2, -y2);
        } else {
            g2.translate(xoff, yoff);
            g2.rotate(horn2 + Math.PI / 2.0);
            g2.drawString(ctg.getName(), -g2.getFontMetrics().stringWidth(ctg.getName()), 0);
            g2.rotate(-horn2 - Math.PI / 2.0);
            g2.translate(-xoff, -yoff);
        }

        for (int i = 0; i < ctg.length(); i += 500) {
            for (int k = i; k < Math.min(ctg.length(), i + 500); k++) {
                char chr = ctg.charAt(k);
                if (chr == 'g' || chr == 'G') {
                    g++;
                } else if (chr == 'c' || chr == 'C') {
                    c++;
                }
            }

            int gcount = 0;
            int ccount = 0;
            int acount = 0;
            int tcount = 0;
            for (int k = i; k < Math.min(ctg.length(), i + 10000); k++) {
                char chr = k - 5000 < 0 ? ctg.charAt(ctg.length() + (k - 5000)) : ctg.charAt(k - 5000);
                if (chr == 'g' || chr == 'G') {
                    gcount++;
                } else if (chr == 'c' || chr == 'C') {
                    ccount++;
                } else if (chr == 'a' || chr == 'A')
                    acount++;
                else if (chr == 't' || chr == 'T')
                    tcount++;
            }

            if (gcount > 0 || ccount > 0) {
                double gcskew = (gcount - ccount) / (double) (gcount + ccount);

                gcstotal += gcskew;

                x1 = (int) (512.0 + (384.0) * Math.cos((i + total) * 2.0 * Math.PI / size));
                y1 = (int) (512.0 + (384.0) * Math.sin((i + total) * 2.0 * Math.PI / size));
                x2 = (int) (512.0 + (384.0 + gcskew * 100.0) * Math.cos((i + total) * 2.0 * Math.PI / size));
                y2 = (int) (512.0 + (384.0 + gcskew * 100.0) * Math.sin((i + total) * 2.0 * Math.PI / size));

                if (gcskew >= 0)
                    g2.setColor(Color.blue);
                else
                    g2.setColor(Color.red);
                g2.drawLine(x1, y1, x2, y2);
            }

            if (acount > 0 || tcount > 0) {
                double atskew = (acount - tcount) / (double) (acount + tcount);

                x1 = (int) (512.0 + (300.0) * Math.cos((i + total) * 2.0 * Math.PI / size));
                y1 = (int) (512.0 + (300.0) * Math.sin((i + total) * 2.0 * Math.PI / size));
                x2 = (int) (512.0 + (300.0 + atskew * 100.0) * Math.cos((i + total) * 2.0 * Math.PI / size));
                y2 = (int) (512.0 + (300.0 + atskew * 100.0) * Math.sin((i + total) * 2.0 * Math.PI / size));

                if (atskew >= 0)
                    g2.setColor(Color.blue);
                else
                    g2.setColor(Color.red);
                g2.drawLine(x1, y1, x2, y2);
            }
        }
        total += ctg.length();
    }

    g2.setColor(Color.black);
    g2.setFont(g2.getFont().deriveFont(java.awt.Font.ITALIC).deriveFont(32.0f));
    String[] specsplit; // = selspec.split("_");

    if (selspec.contains("hermus"))
        specsplit = selspec.split("_");
    else {
        Matcher m = Pattern.compile("\\d").matcher(selspec);
        int firstDigitLocation = m.find() ? m.start() : 0;
        if (firstDigitLocation == 0)
            specsplit = new String[] { "Thermus", selspec };
        else
            specsplit = new String[] { "Thermus", selspec.substring(0, firstDigitLocation),
                    selspec.substring(firstDigitLocation) };
    }

    int k = 0;
    for (String spec : specsplit) {
        int strw = g2.getFontMetrics().stringWidth(spec);
        g2.drawString(spec, (1024 - strw) / 2, 1024 / 2 - specsplit.length * 32 / 2 + 32 + k * 32);
        k++;
    }

    /*double gcs = gcstotal/total; //(g-c)/(g+c);
    String gcstr = Double.toString( Math.round( gcs*1000000.0 ) );
    g2.drawString( gcstr+"ppm", 768, 512 );*/
}