import java.awt.image.*;
import java.awt.*;

public class RotateString {
    public static synchronized void draw(Graphics g, String str, int x, int y,
					 double angle,
					 Component c) {
	FontMetrics fm = g.getFontMetrics();
	int w, h;
	int ascent;
	Image base_image, rotated_image;
	Graphics base_g;
	ImagePixel ip;

	w = fm.stringWidth(str);
	ascent = fm.getMaxAscent();
	h = ascent + fm.getMaxDescent();

	base_image = c.createImage(w, h);
	base_g = base_image.getGraphics();
	base_g.setColor(Color.black);
	base_g.fillRect(0, 0, w, h);
	base_g.setColor(Color.white);
	base_g.drawString(str, 0, ascent);
	ip = new ImagePixel(base_image, c);
	base_g.dispose();

	int sx, sy, idx;
	int raster[] = ip.raster;
	int tx, ty;

        double sin = Math.sin(angle);
        double cos = Math.cos(angle);
	double xoff = -ascent * Math.sin(angle) + x + 0.5;
	double yoff = -ascent * Math.cos(angle) + y + 0.5;

	idx = 0;
	for(sy = 0; sy < h; sy++) {
	    double tx_offset = sin * sy + xoff;
	    double ty_offset = cos * sy + yoff;
	    for(sx = 0; sx < w; sx++) {
		if(raster[idx++] == 0xffffffff) {
		    tx = (int)(cos * sx + tx_offset);
		    ty = (int)(ty_offset - sin * sx);
		    g.drawLine(tx, ty, tx, ty);
		}
	    }
	}
    }
}
