[cairo-commit] cairo-java/test/kapow Kapow.java,NONE,1.1

Jeffrey Morgan commit at pdx.freedesktop.org
Tue Mar 8 03:58:35 PST 2005


Committed by: kuzman

Update of /cvs/cairo/cairo-java/test/kapow
In directory gabe:/tmp/cvs-serv4217/test/kapow

Added Files:
	Kapow.java 
Log Message:
Renamed package and started new example

--- NEW FILE: Kapow.java ---
/*
 * Java-Gnome Bindings Library
 *
 * Copyright 1998-2005 the Java-Gnome Team, all rights reserved.
 *
 * The Java-Gnome bindings library is free software distributed under
 * the terms of the GNU Library General Public License version 2.
 */

package kapow;

import org.freedesktop.cairo.Cairo;
import org.freedesktop.cairo.FontSlant;
import org.freedesktop.cairo.FontWeight;
import org.freedesktop.cairo.Format;
import org.freedesktop.cairo.Pattern;
import org.freedesktop.cairo.PngSurface;
import org.freedesktop.cairo.RGBColor;
import org.freedesktop.cairo.TextExtents;

/**
 */
public class Kapow {
	
	private Cairo cr;
	private String filename = "kapow.png";
	private int IMAGE_WIDTH = 384;
	private int IMAGE_HEIGHT = 256;

	private int SPIKES = 10;
	private int SHADOW_OFFSET = 10;

	private int X_FUZZ = 16;
	private int Y_FUZZ = 16;

	private double X_OUTER_RADIUS = (IMAGE_WIDTH / 2 - X_FUZZ - SHADOW_OFFSET);
	private double Y_OUTER_RADIUS = (IMAGE_HEIGHT / 2 - Y_FUZZ - SHADOW_OFFSET);
	
	private double X_INNER_RADIUS = (X_OUTER_RADIUS * 0.7);
	private double Y_INNER_RADIUS = (Y_OUTER_RADIUS * 0.7);
	
	private int RAND_MAX = 2147483647;

	public Kapow(String text) throws Exception {
		cr = new Cairo();
		PngSurface surface = 
			new PngSurface(filename, Format.ARGB32, IMAGE_WIDTH, IMAGE_HEIGHT);
		cr.setTargetSurface(surface);

		cr.setLineWidth(2);
		cr.save();
		cr.translate(SHADOW_OFFSET, SHADOW_OFFSET);
		makeStarPath();
		cr.setAlpha(0.5);
		cr.setRGBColor(0,0,0);
		cr.fill();
		cr.restore();

		makeStarPath();
		cr.setAlpha(2);
		Pattern pattern = new Pattern(IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2,
					10, IMAGE_WIDTH / 2, IMAGE_HEIGHT / 2, 230);
		pattern.addColorStop(0, new RGBColor(1, 1, 0.2), 1);
		pattern.addColorStop(1, new RGBColor(1, 0, 0), 1);
		cr.setPattern(pattern);
		cr.fill();

		makeStarPath();
		cr.setRGBColor(0,0,0);
		cr.stroke();

		cr.selectFont("Sans", FontSlant.NORMAL, FontWeight.BOLD);
		cr.scaleFont(50);
		TextExtents extents = cr.getTextExtents(text);
		double x = IMAGE_WIDTH / 2 - (extents.getWidth() / 2 + extents.getXBearing());
		double y = IMAGE_HEIGHT / 2 - (extents.getHeight() / 2 + extents.getYBearing());

		makeTextPath(x, y, text);

		pattern = new Pattern(IMAGE_WIDTH / 2 - 10, IMAGE_HEIGHT / 4,
					IMAGE_WIDTH / 2 + 10, 3 * IMAGE_HEIGHT / 4);
		pattern.addColorStop(0, new RGBColor(1, 1, 1), 1);
		pattern.addColorStop(1, new RGBColor(0, 0, 0.4), 1);
		cr.setPattern(pattern);

		cr.fill();

		makeTextPath(x, y, text);
		cr.setRGBColor(new RGBColor(0, 0, 0));
		cr.stroke();
		
		cr.showPage();
		cr.dispose();
		surface.close();
	}
	
	private void makeStarPath() {
	    double x;
	    double y;
	    int i;

	    for (i = 0; i < SPIKES * 2; i++) {
	    	x = IMAGE_WIDTH / 2 + Math.cos(Math.PI * i / SPIKES) * X_INNER_RADIUS +
	    			Math.random() * X_FUZZ / RAND_MAX;
	    	y = IMAGE_HEIGHT / 2 + Math.sin(Math.PI * i / SPIKES) * Y_INNER_RADIUS +
	    			Math.random() * Y_FUZZ / RAND_MAX;
	    	
	    	if (0 == i)
	    		cr.moveTo(x, y);
	    	else
	    		cr.lineTo(x, y);

	    	i++;
	    	x = IMAGE_WIDTH / 2 + Math.cos(Math.PI * i / SPIKES) * X_OUTER_RADIUS +
					Math.random() * X_FUZZ / RAND_MAX;
	    	y = IMAGE_HEIGHT / 2 + Math.sin(Math.PI * i / SPIKES) * Y_OUTER_RADIUS +
					Math.random() * Y_FUZZ / RAND_MAX;

	    	cr.lineTo(x, y);
	    }
	    cr.closePath();
	}
	
	private void makeTextPath(double x, double y, String text) {
		
	}

	public static void main(String[] args) {

		
	}
}




More information about the cairo-commit mailing list