Example usage for java.awt FontMetrics stringWidth

List of usage examples for java.awt FontMetrics stringWidth

Introduction

In this page you can find the example usage for java.awt FontMetrics stringWidth.

Prototype

public int stringWidth(String str) 

Source Link

Document

Returns the total advance width for showing the specified String in this Font .

Usage

From source file:org.hyperic.image.chart.Chart.java

protected Rectangle draw(ChartGraphics g) {
    /////////////////////////////////////////////////////////////
    // Draw the chart outline and fills the interior

    // Fill chart background
    Rectangle rect = this.getInteriorRectangle(g);

    if (this.m_bNoData == false) {
        // Fill chart interior
        g.graphics.setColor(this.m_clrFrame);
        g.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);

        g.graphics.setColor(this.chartColor);
        g.graphics.fillRect(rect.x + this.lineWidth, rect.y + this.lineWidth, rect.width - (this.lineWidth),
                rect.height - (this.lineWidth));
    } else {/*from  w w  w.j a v a  2s . c  o m*/
        FontMetrics metrics = g.graphics.getFontMetrics(this.legendFont);

        g.graphics.setColor(this.m_clrLegendText);
        g.graphics.setFont(DEFAULT_LEGEND_PLAIN);
        g.graphics.drawString(this.m_strNoData, (this.width / 2) - (metrics.stringWidth(this.m_strNoData) / 2),
                this.yOffset + (this.height / 2) + (metrics.getAscent() / 2));
    }

    return rect;
}

From source file:org.underworldlabs.swing.plaf.base.AcceleratorToolTipUI.java

public void paint(Graphics g, JComponent c) {

    UIUtils.antialias(g);/*from w w w  .  ja va2  s  .  c o m*/

    Font font = c.getFont();
    FontMetrics metrics = c.getFontMetrics(font);

    Dimension size = c.getSize();
    if (c.isOpaque()) {
        g.setColor(c.getBackground());
        g.fillRect(0, 0, size.width + 20, size.height);
    }

    JToolTip toolTip = (JToolTip) c;
    String tipText = getTipText(toolTip);

    if (!MiscUtils.isNull(tipText)) {

        Insets insets = c.getInsets();
        Rectangle paintTextR = new Rectangle(insets.left, insets.top, size.width - (insets.left + insets.right),
                size.height - (insets.top + insets.bottom));

        Color foreground = c.getForeground();
        g.setColor(foreground);
        g.setFont(font);

        g.drawString(tipText, paintTextR.x + 3, paintTextR.y + metrics.getAscent());

        String acceleratorString = getAcceleratorStringForRender(toolTip);
        if (StringUtils.isNotBlank(acceleratorString)) {

            Font acceleratorFont = font.deriveFont(font.getSize() - 1f);
            g.setFont(acceleratorFont);
            g.setColor(GUIUtils.getSlightlyBrighter(foreground, 2.0f));

            g.drawString(acceleratorString, paintTextR.x + 6 + metrics.stringWidth(tipText),
                    paintTextR.y + metrics.getAscent());
        }

    }

}

From source file:odcplot.OdcPlot.java

private void initImage() {
    img = new BufferedImage(outX, outY, BufferedImage.TYPE_BYTE_INDEXED, getColorModel());
    grph = img.createGraphics();//from w  w  w .  ja  v  a2s .  co m
    grph.setBackground(Color.white);
    grph.clearRect(0, 0, outX, outY);
    if (outX < 600) {
        lblFontSize = 9;
        titleFontSize = 11;
        addLegend = false;
        addGPS = false;
    } else if (outX < 800) {
        lblFontSize = 12;
        titleFontSize = 14;
        addLegend = true;
        addGPS = true;
    } else if (outX < 1025) {
        lblFontSize = 14;
        titleFontSize = 16;
        addLegend = true;
        addGPS = true;
    } else {
        lblFontSize = 24;
        titleFontSize = 32;
        addLegend = true;
        addGPS = true;
    }

    String title;
    String utcDate = TimeAndDate.gpsAsUtcString(startGPS);
    String hrTime = TimeAndDate.hrTime(duration);
    String cname;
    if (useTestData) {
        cname = String.format("Testing at %1$.f Hz", sampleRate);
    } else {
        cname = channelName;
    }
    title = String.format("%1$s %2$s UTC (%3$,d) t=%4$s", cname, utcDate, startGPS, hrTime);

    lblFont = new Font("Liberation Sans", Font.PLAIN, lblFontSize);
    titleFont = new Font("Liberation Serif", Font.BOLD, titleFontSize);
    FontMetrics titleMetrics = grph.getFontMetrics(titleFont);
    labelMetrics = grph.getFontMetrics(lblFont);

    titleHeight = titleMetrics.getHeight();
    int titleWidth = titleMetrics.stringWidth(title);

    while (titleWidth > outX && lblFontSize > 7) {
        titleFontSize--;
        lblFontSize--;
        lblFont = new Font("Dialog", Font.PLAIN, lblFontSize);
        titleFont = new Font("Serif", Font.BOLD, titleFontSize);
        titleMetrics = grph.getFontMetrics(titleFont);
        labelMetrics = grph.getFontMetrics(lblFont);

        titleHeight = titleMetrics.getHeight();
        titleWidth = titleMetrics.stringWidth(title);

    }

    imgY0 = titleHeight + titleFontSize / 2;

    lblHeight = labelMetrics.getHeight();
    int lblWidth = Integer.MIN_VALUE;
    for (String lbl : bitNames) {
        int lw = labelMetrics.stringWidth(lbl);
        lblWidth = Math.max(lblWidth, lw);
    }
    imgX0 = lblWidth + 10;
    dimX = outX - imgX0 - 5;
    int bLblLines = addLegend ? 4 : 3;
    bLblLines = addGPS ? bLblLines + 1 : bLblLines;

    dimY = outY - imgY0 - lblHeight * bLblLines;

    int nsamples = (int) (duration * sampleRate);
    colPerSample = 1;

    if (nsamples < dimX) {
        // we have more pixels available than we have samples so making it pretty takes some work
        colPerSample = dimX / nsamples;
        dimX = colPerSample * nsamples;
    }
    sample2colFact = (double) (dimX - 1) / (double) nsamples;
    // create and clear the data buffer
    data = new byte[dimX][maxBits];
    for (int x = 0; x < dimX; x++) {
        for (int y = 0; y < maxBits; y++) {
            data[x][y] = 0;
        }
    }
    int tx;
    if (titleWidth < dimX) {
        tx = (dimX - titleWidth) / 2 + imgX0;
    } else if (titleWidth < outX) {
        tx = (outX - titleWidth) / 2;
    } else {
        tx = 0;
    }
    tx = tx < 0 ? 0 : tx;
    tx = tx + titleWidth > outX ? 0 : tx;

    int ty = 5 + titleMetrics.getAscent();
    grph.setPaint(Color.BLACK);
    grph.setFont(titleFont);
    grph.drawString(title, tx, ty);
    rast = img.getRaster();
}

From source file:edu.ku.brc.services.mapping.LocalityMapper.java

/**
 * Grabs a new map from the web service and appropriately adorns it
 * with labels and markers.//ww  w.  j av  a2s.  c  o  m
 *
 * @return a map image as an icon
 * @throws HttpException a network error occurred while grabbing the map from the service
 * @throws IOException a network error occurred while grabbing the map from the service
 */
protected Icon grabNewMap() throws HttpException, IOException {
    recalculateBoundingBox();

    if (!cacheValid) {
        //            Image mapImage = getMapFromService("mapus.jpl.nasa.gov",
        //                    "/wms.cgi?request=GetMap&srs=EPSG:4326&format=image/png&styles=visual",
        //                    "global_mosaic",
        //                    mapMinLat, mapMinLong, mapMaxLat, mapMaxLong, maxMapHeight, maxMapWidth);
        //
        //            Image overlayImage = getMapFromService("129.237.201.132",
        //                    "/cgi-bin/mapserv?map=/var/www/maps/specify.map&service=WMS&request=GetMap&srs=EPSG:4326&version=1.3.1&format=image/png&transparent=true",
        //                    "states,rivers",
        //                    mapMinLat, mapMinLong, mapMaxLat, mapMaxLong, maxMapHeight, maxMapWidth);

        Image mapImage = getMapFromService("lifemapper.org", //$NON-NLS-1$
                "/ogc?map=specify.map&service=WMS&request=GetMap&srs=EPSG:4326&version=1.3.1&STYLES=&format=image/png&transparent=TRUE", //$NON-NLS-1$
                "global_mosaic,states,rivers", //$NON-NLS-1$
                mapMinLat, mapMinLong, mapMaxLat, mapMaxLong, maxMapHeight, maxMapWidth);

        mapIcon = new ImageIcon(mapImage);
        //            overlayIcon = new ImageIcon(overlayImage);
        cacheValid = true;

        mapWidth = mapIcon.getIconWidth();
        mapHeight = mapIcon.getIconHeight();

        if (mapWidth < 0 || mapHeight < 0) {
            throw new IOException("Request for map failed.  Received map has negative width or height."); //$NON-NLS-1$
        }

        mapLatRange = mapMaxLat - mapMinLat;
        mapLongRange = mapMaxLong - mapMinLong;

        pixelPerLatRatio = mapHeight / mapLatRange;
        pixelPerLongRatio = mapWidth / mapLongRange;

        for (int i = 0; i < mapLocations.size(); ++i) {
            MapLocationIFace loc = mapLocations.get(i);
            Point iconLoc = determinePixelCoordsOfMapLocationIFace(loc);
            markerLocations.set(i, iconLoc);
        }

        cacheValid = true;
    }

    Icon icon = new Icon() {
        public void paintIcon(Component c, Graphics g, int x, int y) {
            // this helps keep the labels inside the map
            g.setClip(x, y, mapWidth, mapHeight);
            // log the x and y for the MouseMotionListener
            mostRecentPaintedX = x;
            mostRecentPaintedY = y;
            Point currentLocPoint = null;
            if (currentLoc != null) {
                currentLocPoint = determinePixelCoordsOfMapLocationIFace(currentLoc);
            }

            mapIcon.paintIcon(c, g, x, y);
            //                overlayIcon.paintIcon(c, g, x, y);

            Point lastLoc = null;
            for (int i = 0; i < mapLocations.size(); ++i) {
                Point markerLoc = markerLocations.get(i);
                String label = labels.get(i);
                boolean current = (currentLoc != null) && markerLoc.equals(currentLocPoint);

                if (markerLoc == null) {
                    log.error("A marker location is null"); //$NON-NLS-1$
                    continue;
                }
                if (!pointIsOnMapIcon(x + markerLoc.x, y + markerLoc.y)) {
                    log.error("A marker location is off the map"); //$NON-NLS-1$
                    continue;
                }
                if (showArrows && lastLoc != null) {
                    int x1 = x + lastLoc.x;
                    int y1 = y + lastLoc.y;
                    int x2 = x + markerLoc.x;
                    int y2 = y + markerLoc.y;
                    Color origColor = g.getColor();
                    if (current && !animationInProgress) {
                        g.setColor(getCurrentLocColor());
                    } else {
                        g.setColor(arrowColor);
                    }
                    GraphicsUtils.drawArrow(g, x1, y1, x2, y2, 2, 2);
                    g.setColor(origColor);
                }
                if (current) {
                    currentLocMarker.paintIcon(c, g, markerLoc.x + x, markerLoc.y + y);
                } else {
                    marker.paintIcon(c, g, markerLoc.x + x, markerLoc.y + y);
                }
                if (label != null) {
                    Color origColor = g.getColor();
                    FontMetrics fm = g.getFontMetrics();
                    int length = fm.stringWidth(label);
                    g.setColor(Color.WHITE);
                    g.fillRect(markerLoc.x + x - (length / 2), markerLoc.y + y - (fm.getHeight() / 2), length,
                            fm.getHeight());
                    g.setColor(labelColor);
                    GraphicsUtils.drawCenteredString(label, g, markerLoc.x + x, markerLoc.y + y);
                    g.setColor(origColor);
                }

                lastLoc = markerLoc;
            }
            if (showArrowAnimations && animationInProgress) {
                int startIndex = mapLocations.indexOf(animStartLoc);
                int endIndex = mapLocations.indexOf(animEndLoc);
                if (startIndex != -1 && endIndex != -1) {
                    Point startPoint = markerLocations.get(startIndex);
                    Point endPoint = markerLocations.get(endIndex);
                    Point arrowEnd = GraphicsUtils.getPointAlongLine(startPoint, endPoint, percent);
                    Color orig = g.getColor();
                    g.setColor(getCurrentLocColor());
                    GraphicsUtils.drawArrow(g, startPoint.x + x, startPoint.y + y, arrowEnd.x + x,
                            arrowEnd.y + y, 5, 3);
                    g.setColor(orig);
                }
            }
        }

        public int getIconWidth() {
            return mapWidth;
        }

        public int getIconHeight() {
            return mapHeight;
        }
    };

    return icon;
}

From source file:SuitaDetails.java

private void initComponents(ArrayList<String[]> descriptions) {
    global = new JPanel();
    global.setBackground(Color.WHITE);
    initGlobal();// w  w w  .  j a  v  a2s  . c  o m
    initTCOptions();
    initSummary();
    definitions.clear();
    border = BorderFactory.createTitledBorder("Global options");
    setBorder(border);
    scroll = new JScrollPane();
    //         setMinimumSize(new Dimension(10,10));
    //         setMaximumSize(new Dimension(1000,1000));
    //         setPreferredSize(new Dimension(100,100));
    defsContainer = new JPanel();
    setLayout(new BorderLayout());
    defsContainer.setBackground(Color.WHITE);
    defsContainer.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
    defsContainer.setLayout(new BoxLayout(defsContainer, BoxLayout.Y_AXIS));
    defsContainer.add(suiteoptions);
    scroll.setViewportView(global);
    add(scroll, BorderLayout.CENTER);
    JLabel l = new JLabel("test");
    FontMetrics metrics = l.getFontMetrics(l.getFont());
    int width = 0;
    for (int i = 0; i < descriptions.size(); i++) {
        if (width < metrics.stringWidth(descriptions.get(i)[RunnerRepository.LABEL])) {
            width = metrics.stringWidth(descriptions.get(i)[RunnerRepository.LABEL]);
        }
    }
    for (int i = 0; i < descriptions.size(); i++) {
        String button = descriptions.get(i)[RunnerRepository.SELECTED];
        DefPanel define = new DefPanel(descriptions.get(i)[RunnerRepository.LABEL], button,
                descriptions.get(i)[RunnerRepository.ID], width, i, this);
        definitions.add(define);
        defsContainer.add(define);
    }
}

From source file:SuitaDetails.java

public void setParent(Item parent) {
    //if(this.parent==parent)return;
    this.parent = parent;
    if (parent != null && parent.getType() == 2) {
        try {/*from   ww  w.  jav  a2 s . co  m*/
            setComboTBs();
            tsuite.setText(parent.getName());
            KeyListener k[] = combo.getKeyListeners();
            for (KeyListener t : k) {
                tsuite.removeKeyListener(t);
            }
            tsuite.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent ev) {
                    String name = tsuite.getText();
                    FontMetrics metrics = getGraphics().getFontMetrics(new Font("TimesRoman", Font.BOLD, 14));
                    int width = metrics.stringWidth(name) + 40;
                    getItemParent().setName(name);
                    getItemParent().getRectangle().setSize(width,
                            (int) getItemParent().getRectangle().getHeight());
                    if (getItemParent().isVisible())
                        RunnerRepository.window.mainpanel.p1.sc.g.updateLocations(getItemParent());
                    RunnerRepository.window.mainpanel.p1.sc.g.repaint();
                }
            });
            panicdetect.setSelected(parent.isPanicdetect());
        } catch (Exception e) {
            System.out.println("There was a problem in getting ep list");
            e.printStackTrace();
        }
        for (int i = 0; i < definitions.size(); i++) {
            definitions.get(i).setParent(parent);
        }
    }
    if (parent != null && parent.getType() == 1) {
        if (parent.isRunnable())
            runnable.setSelected(true);
        else
            runnable.setSelected(false);
        if (parent.isOptional())
            optional.setSelected(true);
        else
            optional.setSelected(false);
        if (parent.isPrerequisite())
            prerequisites.setSelected(true);
        else
            prerequisites.setSelected(false);
        if (parent.isTeardown())
            teardown.setSelected(true);
        else
            teardown.setSelected(false);
        ttcname.setText(getItemParent().getName());
        if (parent.isClearcase()) {
            tview.setText(RunnerRepository.window.mainpanel.getP5().view);
        } else {
            tview.setText("");
        }
        KeyListener k[] = ttcname.getKeyListeners();
        for (KeyListener t : k) {
            ttcname.removeKeyListener(t);
        }
        ttcname.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ev) {
                String name = ttcname.getText();
                FontMetrics metrics = getGraphics().getFontMetrics(new Font("TimesRoman", Font.BOLD, 14));
                int width = metrics.stringWidth(name) + 20;
                getItemParent().setName(name);
                getItemParent().getRectangle().setSize(width, (int) getItemParent().getRectangle().getHeight());
                if (getItemParent().isVisible())
                    RunnerRepository.window.mainpanel.p1.sc.g.updateLocations(getItemParent());
                RunnerRepository.window.mainpanel.p1.sc.g.repaint();
            }
        });
        ActionListener[] s = runnable.getActionListeners();
        for (ActionListener a : s) {
            runnable.removeActionListener(a);
        }
        runnable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                if (runnable.isSelected())
                    getItemParent().setRunnable(true);
                else
                    getItemParent().setRunnable(false);
                RunnerRepository.window.mainpanel.p1.sc.g.repaint();
            }
        });
        s = optional.getActionListeners();
        for (ActionListener a : s) {
            optional.removeActionListener(a);
        }
        optional.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                if (optional.isSelected()) {
                    getItemParent().setOptional(true);
                    RunnerRepository.window.mainpanel.p1.sc.g.unsetPrerequisite(getItemParent());
                    prerequisites.setSelected(false);
                } else
                    getItemParent().setOptional(false);
                RunnerRepository.window.mainpanel.p1.sc.g.repaint();
            }
        });
        s = prerequisites.getActionListeners();
        for (ActionListener a : s) {
            prerequisites.removeActionListener(a);
        }
        prerequisites.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                if (prerequisites.isSelected()) {
                    RunnerRepository.window.mainpanel.p1.sc.g.setPreRequisites(getItemParent());
                } else {
                    RunnerRepository.window.mainpanel.p1.sc.g.unsetPrerequisite(getItemParent());
                }
            }
        });

        s = teardown.getActionListeners();
        for (ActionListener a : s) {
            teardown.removeActionListener(a);
        }
        teardown.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                if (teardown.isSelected()) {
                    //getItemParent().setPrerequisite(false);
                    RunnerRepository.window.mainpanel.p1.sc.g.setTeardown(getItemParent());
                } else {
                    RunnerRepository.window.mainpanel.p1.sc.g.unsetTeardown(getItemParent());
                }
            }
        });
        prop.setParent(getItemParent());
        param.setParent(getItemParent());
    }
}

From source file:org.sakaiproject.sitestats.impl.chart.ChartServiceImpl.java

private byte[] generateNoDataChart(int width, int height) {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setBackground(parseColor(M_sm.getChartBackgroundColor()));
    g2d.clearRect(0, 0, width - 1, height - 1);
    g2d.setColor(parseColor("#cccccc"));
    g2d.drawRect(0, 0, width - 1, height - 1);
    Font f = new Font("SansSerif", Font.PLAIN, 12);
    g2d.setFont(f);/*from ww w .ja  v  a  2  s  . c o  m*/
    FontMetrics fm = g2d.getFontMetrics(f);
    String noData = msgs.getString("no_data");
    int noDataWidth = fm.stringWidth(noData);
    int noDataHeight = fm.getHeight();
    g2d.setColor(parseColor("#555555"));
    g2d.drawString(noData, width / 2 - noDataWidth / 2, height / 2 - noDataHeight / 2 + 2);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(img, "png", out);
    } catch (IOException e) {
        LOG.warn("Error occurred while generating SiteStats chart image data", e);
    }
    return out.toByteArray();
}

From source file:edu.ku.brc.ui.UIRegistry.java

/**
 * Writes a string message into the BufferedImage on GlassPane and sets the main component's visibility to false and
 * shows the GlassPane./*from   w ww.  j  a  v  a 2 s  .c o  m*/
 * @param msg the message
 * @param pointSize the Font point size for the message to be writen in
 */
public static GhostGlassPane writeGlassPaneMsg(final String msg, final int pointSize) {
    GhostGlassPane glassPane = getGlassPane();
    if (glassPane != null) {
        glassPane.finishDnD();
    }

    glassPane.setMaskingEvents(true);

    Component mainComp = get(MAINPANE);
    if (mainComp != null && glassPane != null) {
        JFrame frame = (JFrame) get(FRAME);
        frameRect = frame.getBounds();

        int y = 0;
        JMenuBar menuBar = null;
        Dimension size = mainComp.getSize();
        if (UIHelper.getOSType() != UIHelper.OSTYPE.MacOSX) {
            menuBar = frame.getJMenuBar();
            size.height += menuBar.getSize().height;
            y += menuBar.getSize().height;
        }
        BufferedImage buffer = getGlassPaneBufferedImage(size.width, size.height);
        Graphics2D g2 = buffer.createGraphics();
        if (menuBar != null) {
            menuBar.paint(g2);
        }
        g2.translate(0, y);
        mainComp.paint(g2);
        g2.translate(0, -y);

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(new Color(255, 255, 255, 128));
        g2.fillRect(0, 0, size.width, size.height);

        g2.setFont(new Font((new JLabel()).getFont().getName(), Font.BOLD, pointSize));
        FontMetrics fm = g2.getFontMetrics();

        int tw = fm.stringWidth(msg);
        int th = fm.getHeight();
        int tx = (size.width - tw) / 2;
        int ty = (size.height - th) / 2;

        int expand = 20;
        int arc = expand * 2;
        g2.setColor(Color.WHITE);
        g2.fillRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.DARK_GRAY);
        g2.drawRoundRect(tx - (expand / 2), ty - fm.getAscent() - (expand / 2), tw + expand, th + expand, arc,
                arc);

        g2.setColor(Color.BLACK);
        g2.drawString(msg, tx, ty);
        g2.dispose();

        glassPane.setImage(buffer);
        glassPane.setPoint(new Point(0, 0), GhostGlassPane.ImagePaintMode.ABSOLUTE);
        glassPane.setOffset(new Point(0, 0));

        glassPane.setVisible(true);
        mainComp.setVisible(false);

        //Using paintImmediately fixes problems with glass pane not showing, such as for workbench saves initialed
        //during workbench or app shutdown. Don't know if there is a better way to fix it.
        //glassPane.repaint();
        glassPane.paintImmediately(glassPane.getBounds());
        showingGlassPane = true;
    }

    return glassPane;
}

From source file:org.sakaiproject.sitestats.impl.ServerWideReportManagerImpl.java

private byte[] generateNoDataChart(int width, int height) {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();

    g2d.setBackground(parseColor(statsManager.getChartBackgroundColor()));
    g2d.clearRect(0, 0, width - 1, height - 1);
    g2d.setColor(parseColor("#cccccc"));
    g2d.drawRect(0, 0, width - 1, height - 1);
    Font f = new Font("SansSerif", Font.PLAIN, 12);
    g2d.setFont(f);/*from  www .j  a  va 2 s.c  o m*/
    FontMetrics fm = g2d.getFontMetrics(f);
    String noData = msgs.getString("no_data");
    int noDataWidth = fm.stringWidth(noData);
    int noDataHeight = fm.getHeight();
    g2d.setColor(parseColor("#555555"));
    g2d.drawString(noData, width / 2 - noDataWidth / 2, height / 2 - noDataHeight / 2 + 2);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(img, "png", out);
    } catch (IOException e) {
        log.warn("Error occurred while generating SiteStats chart image data", e);
    }
    return out.toByteArray();
}

From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java

private void drawBusyBox(Graphics2D g2, JXLayer<? extends V> layer) {
    final Font oldFont = g2.getFont();
    final Font font = oldFont;
    final FontMetrics fontMetrics = g2.getFontMetrics(font);

    // Not sure why. Draw GIF image on JXLayer, will cause endless setDirty
    // being triggered by system.
    //final Image image = ((ImageIcon)Icons.BUSY).getImage();
    //final int imgWidth = Icons.BUSY.getIconWidth();
    //final int imgHeight = Icons.BUSY.getIconHeight();
    //final int imgMessageWidthMargin = 5;
    final int imgWidth = 0;
    final int imgHeight = 0;
    final int imgMessageWidthMargin = 0;

    final String message = MessagesBundle.getString("info_message_retrieving_latest_stock_price");
    final int maxWidth = imgWidth + imgMessageWidthMargin + fontMetrics.stringWidth(message);
    final int maxHeight = Math.max(imgHeight, fontMetrics.getHeight());

    final int padding = 5;
    final int width = maxWidth + (padding << 1);
    final int height = maxHeight + (padding << 1);
    final int x = (int) this.drawArea.getX() + (((int) this.drawArea.getWidth() - width) >> 1);
    final int y = (int) this.drawArea.getY() + (((int) this.drawArea.getHeight() - height) >> 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);/*w w  w . j  a  va2  s .  co  m*/
    g2.drawRoundRect(x - 1, y - 1, width + 1, height + 1, 15, 15);
    g2.setColor(COLOR_BACKGROUND);
    g2.setComposite(Utils.makeComposite(0.75f));
    g2.fillRoundRect(x, y, width, height, 15, 15);
    g2.setComposite(oldComposite);
    g2.setColor(oldColor);

    //g2.drawImage(image, x + padding, y + ((height - imgHeight) >> 1), layer.getView());

    g2.setFont(font);
    g2.setColor(COLOR_BLUE);

    int yy = y + ((height - fontMetrics.getHeight()) >> 1) + fontMetrics.getAscent();
    g2.setFont(font);
    g2.setColor(COLOR_BLUE);
    g2.drawString(message, x + padding + imgWidth + imgMessageWidthMargin, yy);
    g2.setColor(oldColor);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias);
    g2.setFont(oldFont);
}