/*
 *  ==========================================================================
 *  DemoLetsDance.java : This applet creates a canvas and displays
 * 
 *  "Let's dance all around the World"
 * 
 *  using a number of texture-paint types and font outlines. The applet uses
 *  affine transformations (i.e., the AffineTransform class) to concatenate
 *  transformations in 2D graphics (see java.awt.geom).
 * 
 *  Adapted from : Pantham S., Pure JFC Swing, 1999.
 *  Modified by : Mark Austin                                      March, 2001
 *  ==========================================================================
 */

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;       //  Needed for affine transformations ...
import javax.swing.border.*;

public class DemoLetsDance extends JApplet {
    
    public void init() {

        // 1. Get the content pane

        Container container = getContentPane();
        container.setBackground( Color.white );

        // 2. Define the color canvas

        Canvas canvas = new DemoLetsDanceCanvas();

        container.add(canvas);
    }
}

// Create canvas to paint the "Let's Swing!" text

class DemoLetsDanceCanvas extends Canvas {
    Font font;
    FontMetrics fontMetrics;

    DemoLetsDanceCanvas() {
        font = new Font("Dialog", Font.BOLD, 40);
        fontMetrics = getFontMetrics(font);
    }

    public void paint(Graphics g) {

        // Set up the 2D graphics context

        Graphics2D g2D = (Graphics2D) g;

        // Yo, say ... lets Swing !

        sayDance(g2D, 30, 50);

        // Say "... all around"

        sayAround(g2D, 30, 140);

        // This code controls the rendering quality of the word
        // World. You can see the smoothening of the inclined lines.

        RenderingHints qualityHints = new RenderingHints(
                                          RenderingHints.KEY_ANTIALIASING,
                                          RenderingHints.VALUE_ANTIALIAS_ON);
        qualityHints.put(RenderingHints.KEY_RENDERING,
                         RenderingHints.VALUE_RENDER_QUALITY);
        g2D.setRenderingHints(qualityHints);
          
        // Say "... the World!" 

        sayWorld(g2D, fontMetrics.stringWidth("the World!") + 45, 100);
    }

    // This method draws the text "Let's dance" with gradient-paint

    public void sayDance(Graphics2D g2D, int x, int y) {

        // Assign font type

        g2D.setFont(font);
            
        // Create a gradient paint object and assign it
        // to the 2D graphics context

        GradientPaint gp = new GradientPaint(
                                30.0f, 50.0f,
                                Color.blue,
                                fontMetrics.stringWidth("Let's dance"),
                                fontMetrics.getHeight(),
                                Color.red);             
        g2D.setPaint(gp);

        // Draw the string Hello!

        g2D.drawString("Let's dance", x, y);                                                 

    }

    // This method draws the text "all around" with texture-paint type
    // fill pattern

    public void sayAround( Graphics2D g2D, int x, int y) {

        // Create a texture paint

        BufferedImage bi = new BufferedImage(5, 5,
                               BufferedImage.TYPE_INT_RGB);
        Graphics2D big = bi.createGraphics();
        big.setColor(Color.magenta);
        big.fillRect(0,0,5,5);
        big.setColor(Color.black);
        big.fillOval(0,0,5,5);
        Rectangle r  = new Rectangle(0, 0, 5, 5);
        TexturePaint tp = new TexturePaint(bi, r);
        g2D.setPaint(tp);
        g2D.drawString("all around", 30, 100);
    }

    // This method draws the outline of the text "the World!".

    public void sayWorld(Graphics2D g2D, int x, int y) {

        AffineTransform at = new AffineTransform();
        at.setToTranslation(x, y);
        at.shear(-0.5, 0.0);

        FontRenderContext frc = new FontRenderContext(at, false, false);

        // Text representation to say World!...

        TextLayout tl = new TextLayout("the World!", font, frc);

        // Obtain the outline of the text in object tl, and draw
        // it by invoking the draw() method in graphics context.

        Shape outline = tl.getOutline(null);

        // Set the outline color to blue

        g2D.setColor(Color.blue);

        BasicStroke wideStroke = new BasicStroke(2.0f);
        g2D.setStroke(wideStroke);

        // Draw the outline

        g2D.draw(outline);
    }
}