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.
g.fill(affineTransform.createTransformedShape()) |
The method setPaint(paint) sets the shadow. The method g.draw() draws
the outline of the letter specified.
Here is the code of DisplayShadow.java
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(0, 0,Color.cyan, WIDTH, HEIGHT,
Color.red));
g2d.fillRect(0, 0, WIDTH, HEIGHT);
g2d.setPaint(new GradientPaint(0, 0, Color.cyan, 20, 20,
Color.red, true));
g2d.setStroke(new BasicStroke(10));
g2d.drawRect(20, 20, WIDTH - 20, HEIGHT - 20);
Font font = new Font("Book Antiqua", Font.BOLD, 10);
Font font2 =
font.deriveFont(AffineTransform.getScaleInstance(10.0, 10.0));
GlyphVector glyphVector = font2.createGlyphVector(
g2d.getFontRenderContext(),"HELLO");
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(0, 0, 0, 100);
AffineTransform affineTransform = AffineTransform.getShearInstance(
-1.0, 0.0);
affineTransform.scale(1.0, 0.5);
g2d.translate(65, 270);
g2d.setPaint(paint);
g2d.translate(15, 20);
g2d.fill(affineTransform.createTransformedShape(h));
g2d.translate(-15, -20);
g2d.setPaint(Color.red);
g2d.fill(h);
g2d.setPaint(Color.black);
g2d.draw(h);
g2d.translate(67, 0);
g2d.setPaint(paint);
g2d.fill(affineTransform.createTransformedShape(e));
g2d.setColor(Color.cyan);
g2d.fill(e);
g2d.setPaint(Color.black);
g2d.draw(e);
g2d.translate(69, 0);
g2d.setPaint(paint);
g2d.fill(affineTransform.createTransformedShape(l1));
g2d.setColor(Color.red);
g2d.fill(l1);
g2d.setPaint(Color.black);
g2d.draw(l1);
g2d.translate(71, 0);
g2d.setPaint(paint);
g2d.fill(affineTransform.createTransformedShape(l2));
g2d.setColor(Color.cyan);
g2d.fill(l2);
g2d.setPaint(Color.black);
g2d.draw(l2);
g2d.translate(73, 0);
g2d.setPaint(paint);
g2d.fill(affineTransform.createTransformedShape(o));
g2d.setColor(Color.red);
g2d.fill(o);
g2d.setPaint(Color.black);
g2d.draw(o);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new DisplayShadow());
frame.setSize(800, 400);
frame.show();
}
}
|
Output will be displayed as:
Download Source Code