Share on Google+Share on Google+

Display the Shadow of the string

To display the shadow, we have used the class GradientPaint to draw a box. The g.setStroke(new BasicStroke(15)) draw the wide lines.

Display the Shadow of the string


This section illustrates you to display the shadow of the specified string.

To display the shadow, we have used the class GradientPaint to draw a box. The g.setStroke(new BasicStroke(15)) draw the wide lines. The class BasicStroke provides an stylistic way to draw an outline. The Font class defines the font.

The AffineTransform class provides translations, scaling, flipping, rotations, and shearing. The getScaleInstance(30.0, 30.0) returns a transform representing a scaling transformation. The class GlyphVector  contains the geometric information in a transformed coordinate.

The method createGlyphVector() returns a new GlyphVector object, created with the specified String and the specified FontRenderContext. The method getFontRenderContext() gets the FontRenderContext by the FontMetrics object to measure text. The getGlyphOutline() method retrieves the outline . To outline the letters with a 5-pixel wide line, we have used the method setStroke(new BasicStroke(5.0f)).

To display the shadows for the letters specified, we have used the objects of Paint class and AffineTransform class. The method getShearInstance( -1.0, 0.0) returns a shearing transformation. By using g.translate(65, 270), we move to the first letter H

Following code displays the shadow of the letters. 


The method setPaint(paint) sets the shadow. The method g.draw() draws the outline of the letter specified.

Here is the code of

import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;

public class DisplayShadow extends JPanel {
  static final int WIDTH = 700, HEIGHT = 275;

  public String getName() {
  return "yes";
  public int getWidth() {
  return WIDTH;
  public int getHeight() {
  return HEIGHT;
  public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.setPaint(new GradientPaint(00,Color.cyan, WIDTH, HEIGHT,;
  g2d.fillRect(00, WIDTH, HEIGHT); 
  g2d.setPaint(new GradientPaint(00, Color.cyan, 2020,, true));
  g2d.setStroke(new BasicStroke(10));
  g2d.drawRect(2020, WIDTH - 20, HEIGHT - 20); 
  Font font = new Font("Book Antiqua", Font.BOLD, 10); 
  Font font2 = 
  GlyphVector glyphVector = font2.createGlyphVector(
  Shape h = glyphVector.getGlyphOutline(0); 
  Shape e = glyphVector.getGlyphOutline(1); 
  Shape l1 = glyphVector.getGlyphOutline(2); 
  Shape l2 = glyphVector.getGlyphOutline(3);
  Shape o= glyphVector.getGlyphOutline(4);

g2d.setStroke(new BasicStroke(5.0f));
  Paint paint = new Color(000100);
  AffineTransform affineTransform = AffineTransform.getShearInstance(
  g2d.translate(-15, -20); 




 public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.getContentPane().add(new DisplayShadow());

Output will be displayed as:

Download Source Code



Posted on: October 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Display the Shadow of the string  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image