[cairo-commit] cairo-java/test/kapow Kapow.java,1.6,1.7
Jeffrey Morgan
commit at pdx.freedesktop.org
Mon Mar 14 12:17:47 PST 2005
Committed by: kuzman
Update of /cvs/cairo/cairo-java/test/kapow
In directory gabe:/tmp/cvs-serv19575/test/kapow
Modified Files:
Kapow.java
Log Message:
cleaned up the kapow example
Index: Kapow.java
===================================================================
RCS file: /cvs/cairo/cairo-java/test/kapow/Kapow.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- Kapow.java 14 Mar 2005 16:24:06 -0000 1.6
+++ Kapow.java 14 Mar 2005 20:17:44 -0000 1.7
@@ -16,6 +16,7 @@
import org.freedesktop.cairo.Format;
import org.freedesktop.cairo.Pattern;
import org.freedesktop.cairo.PngSurface;
+import org.freedesktop.cairo.Point;
import org.freedesktop.cairo.RGBColor;
import org.freedesktop.cairo.TextExtents;
@@ -28,20 +29,20 @@
private String filename = "kapow.png";
public int IMAGE_WIDTH = 460;
public int IMAGE_HEIGHT = 308;
-
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;
+ private int RAND_MAX = Integer.MAX_VALUE;
+ private RGBColor black = new RGBColor(0,0,0);
+ private RGBColor white = new RGBColor(1,1,1);
+ private RGBColor red = new RGBColor(1,0,0);
+ private RGBColor yellow = new RGBColor(1,1,0.2);
+ private RGBColor purple = new RGBColor(0,0,0.4);
public Kapow(String text) throws Exception {
cr = new Cairo();
@@ -54,7 +55,7 @@
cr.translate(SHADOW_OFFSET, SHADOW_OFFSET);
makeStarPath();
cr.setAlpha(0.5);
- cr.setRGBColor(0,0,0);
+ cr.setRGBColor(black);
cr.fill();
cr.restore();
@@ -62,33 +63,34 @@
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);
+ pattern.addColorStop(0, yellow, 1);
+ pattern.addColorStop(1, red, 1);
cr.setPattern(pattern);
cr.fill();
makeStarPath();
- cr.setRGBColor(0,0,0);
+ cr.setRGBColor(black);
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());
+ Point point = new Point();
+ point.setX(IMAGE_WIDTH / 2 - (extents.getWidth() / 2 + extents.getXBearing()));
+ point.setY(IMAGE_HEIGHT / 2 - (extents.getHeight() / 2 + extents.getYBearing()));
- makeTextPath(x, y, text);
+ makeTextPath(point, 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);
+ pattern.addColorStop(0, white, 1);
+ pattern.addColorStop(1, purple, 1);
cr.setPattern(pattern);
cr.fill();
- makeTextPath(x, y, text);
- cr.setRGBColor(new RGBColor(0, 0, 0));
+ makeTextPath(point, text);
+ cr.setRGBColor(black);
cr.stroke();
cr.showPage();
@@ -97,41 +99,38 @@
}
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;
+ for (int i = 0; i < SPIKES * 2; i++) {
+ Point p = new Point();
+ p.setX(IMAGE_WIDTH / 2 + Math.cos(Math.PI * i / SPIKES) * X_INNER_RADIUS +
+ Math.random() * X_FUZZ / RAND_MAX);
+ p.setY(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);
+ cr.moveTo(p);
else
- cr.lineTo(x, y);
+ cr.lineTo(p);
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;
+ p.setX(IMAGE_WIDTH / 2 + Math.cos(Math.PI * i / SPIKES) * X_OUTER_RADIUS +
+ Math.random() * X_FUZZ / RAND_MAX);
+ p.setY(IMAGE_HEIGHT / 2 + Math.sin(Math.PI * i / SPIKES) * Y_OUTER_RADIUS +
+ Math.random() * Y_FUZZ / RAND_MAX);
- cr.lineTo(x, y);
+ cr.lineTo(p);
}
cr.closePath();
}
- private void makeTextPath(double x, double y, String text) {
+ private void makeTextPath(Point point, String text) {
path = new MyPath();
- cr.moveTo(x, y);
+ cr.moveTo(point);
cr.textPath(text);
cr.getPathFlat(path);
}
private static void usage(){
- System.out.println("USAGE: gij kapow.Kapow <text>");
+ System.out.println("USAGE: java kapow.Kapow <text>");
System.out.println("You must specify the text to print.");
System.exit(1);
}
@@ -151,13 +150,13 @@
cr.newPath();
firstTime = false;
}
- double[] newPoint = bendIt(x, y);
- cr.moveTo(newPoint[0], newPoint[1]);
+ Point bent = bendIt(x, y);
+ cr.moveTo(bent);
}
public void lineTo(double x, double y) {
- double[] newPoint = bendIt(x, y);
- cr.lineTo(newPoint[0], newPoint[1]);
+ Point bent = bendIt(x, y);
+ cr.lineTo(bent);
}
public void curveTo(double x1, double y1, double x2, double y2, double x3, double y3) {}
@@ -166,20 +165,19 @@
cr.closePath();
}
- private double[] bendIt(double x, double y) {
+ private Point bendIt(double x, double y) {
double cx = IMAGE_WIDTH / 2;
double cy = 500;
- double angle, radius, t;
- double[] retval = new double[2];
-
- angle = Math.PI / 2 - (x - cx) / IMAGE_WIDTH;
- t = 3 * Math.PI / 4 - angle + 0.05;
+ Point bent = new Point();
+
+ double angle = Math.PI / 2 - (x - cx) / IMAGE_WIDTH;
+ double t = 3 * Math.PI / 4 - angle + 0.05;
angle = 3 * Math.PI / 4 - Math.pow(t, 1.8);
- radius = cy - (IMAGE_HEIGHT / 2 + (y - IMAGE_HEIGHT / 2) * t * 2);
+ double radius = cy - (IMAGE_HEIGHT / 2 + (y - IMAGE_HEIGHT / 2) * t * 2);
- retval[0] = cx + Math.cos(angle) * radius;
- retval[1] = cy - Math.sin(angle) * radius;
- return retval;
+ bent.setX(cx + Math.cos(angle) * radius);
+ bent.setY(cy - Math.sin(angle) * radius);
+ return bent;
}
}
}
More information about the cairo-commit
mailing list