Java Graphics Draw String drawGradientText(Graphics g, String text, int x, int y, Color c)

Here you can find the source of drawGradientText(Graphics g, String text, int x, int y, Color c)

Description

draw Gradient Text

License

Open Source License

Declaration

public static void drawGradientText(Graphics g, String text, int x, int y, Color c) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

public class Main {
    public static void drawGradientText(Graphics g, String text, int x, int y, Color c) {
        g.setFont(new Font("Calibri", 13, Font.PLAIN));
        Graphics2D g2 = (Graphics2D) g;
        Color color3 = new Color(51, 51, 51, 205);
        Font font1 = new Font("Arial", 0, 12);
        g.setFont(font1);//from  w  w  w  .ja  v  a  2 s  .c o  m
        FontMetrics FONTMETRICS = g.getFontMetrics();

        Rectangle textBox = new Rectangle(x, y - g.getFont().getSize(),
                (int) FONTMETRICS.getStringBounds(text, g).getWidth() + 8,
                (int) FONTMETRICS.getStringBounds(text, g).getHeight() + 5);

        Paint defaultPaint = g2.getPaint();

        g2.setPaint(new RadialGradientPaint(
                new Point.Double(textBox.x + textBox.width / 2.0D, textBox.y + textBox.height / 2.0D),
                (float) (textBox.getWidth() / 2.0D), new float[] { 0.5F, 1.0F },
                new Color[] { new Color(color3.getRed(), color3.getGreen(), color3.getBlue(), 175),
                        new Color(0.0F, 0.0F, 0.0F, 0.8F) }));

        g.fillRect(textBox.x, textBox.y + 12, textBox.width, textBox.height);
        g2.setPaint(defaultPaint);
        g.setColor(Color.BLACK);
        g.drawRect(textBox.x, textBox.y + 12, textBox.width, textBox.height);
        g.setColor(c);
        g.drawString(text, x + 4, y + 15);
        for (int i = 0; i < text.length(); i++) {
            if (Character.isDigit(text.charAt(i))) {
                g.drawString("" + text.charAt(i), x + FONTMETRICS.stringWidth(text.substring(0, i)) + 4, y + 15);
            }
        }
    }
}

Related

  1. drawClippedString(Graphics g, String t, int x, int y, int width)
  2. drawEmphasizedString(Graphics g, Color foreground, Color emphasis, String s, int underlinedIndex, int x, int y)
  3. drawFitText(Graphics2D g2, int x, int y, int width, int height, String text)
  4. drawFormattedString(Graphics2D g2, String htmlStr, int x, int y, int w, int h)
  5. drawGradient(Graphics g, JComponent c, String prefix)
  6. drawLine(String pLine, Graphics2D pG, int pX, int pY, int pWidth, String pJustification)
  7. drawLineDrawChar(Graphics g, int x, int y, int bi, char c, int charWidth, int charHeight)
  8. drawMessage(Graphics2D g, String message)
  9. drawRightJustifiedText(String text, int right, int y, Graphics g)