Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/svg/RadialGradientFill.java
1463 views
1
package org.newdawn.slick.svg;
2
3
import org.newdawn.slick.geom.Shape;
4
import org.newdawn.slick.geom.TexCoordGenerator;
5
import org.newdawn.slick.geom.Transform;
6
import org.newdawn.slick.geom.Vector2f;
7
8
/**
9
* A filler to apply a SVG radial gradient across a shape
10
*
11
* @author kevin
12
*/
13
public class RadialGradientFill implements TexCoordGenerator {
14
/** The centre of the gradient */
15
private Vector2f centre;
16
/** The radius before the gradient is complete */
17
private float radius;
18
/** The gradient to apply */
19
private Gradient gradient;
20
/** The shape being filled */
21
private Shape shape;
22
23
/**
24
* Create a new fill for a radial gradient
25
*
26
* @param shape The shape being filled
27
* @param trans The transform given for the shape in the SVG
28
* @param gradient The gradient to apply across the shape
29
*/
30
public RadialGradientFill(Shape shape, Transform trans, Gradient gradient) {
31
this.gradient = gradient;
32
33
radius = gradient.getR();
34
float x = gradient.getX1();
35
float y = gradient.getY1();
36
37
float[] c = new float[] {x,y};
38
gradient.getTransform().transform(c, 0, c, 0, 1);
39
trans.transform(c, 0, c, 0, 1);
40
float[] rt = new float[] {x,y-radius};
41
gradient.getTransform().transform(rt, 0, rt, 0, 1);
42
trans.transform(rt, 0, rt, 0, 1);
43
44
centre = new Vector2f(c[0],c[1]);
45
Vector2f dis = new Vector2f(rt[0],rt[1]);
46
radius = dis.distance(centre);
47
}
48
49
/**
50
* @see org.newdawn.slick.geom.TexCoordGenerator#getCoordFor(float, float)
51
*/
52
public Vector2f getCoordFor(float x, float y) {
53
float u = centre.distance(new Vector2f(x,y));
54
u /= radius;
55
56
if (u > 0.99f) {
57
u = 0.99f;
58
}
59
60
return new Vector2f(u,0);
61
}
62
63
}
64
65