use of java.awt.LinearGradientPaint in project jdk8u_jdk by JetBrains.
the class TransformedPaintTest method createPaint.
private Paint createPaint(PaintType type, int startx, int starty, int w, int h) {
// make sure that the blue color doesn't show up when filling a
// w by h rect
w++;
h++;
int endx = startx + w;
int endy = starty + h;
Rectangle2D.Float r = new Rectangle2D.Float(startx, starty, w, h);
switch(type) {
case COLOR:
return Color.red;
case GRADIENT:
return new GradientPaint(startx, starty, Color.red, endx, endy, Color.green);
case LINEAR_GRADIENT:
return new LinearGradientPaint(startx, starty, endx, endy, new float[] { 0.0f, 0.999f, 1.0f }, new Color[] { Color.red, Color.green, Color.blue });
case RADIAL_GRADIENT:
return new RadialGradientPaint(startx, starty, (float) Math.sqrt(w * w + h * h), new float[] { 0.0f, 0.999f, 1.0f }, new Color[] { Color.red, Color.green, Color.blue }, CycleMethod.NO_CYCLE);
case TEXTURE:
{
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setPaint(createPaint(PaintType.LINEAR_GRADIENT, 0, 0, w, h));
g.fillRect(0, 0, w, h);
return new TexturePaint(bi, r);
}
}
return Color.green;
}
use of java.awt.LinearGradientPaint in project jdk8u_jdk by JetBrains.
the class LinearGradientPrintingTest method doPaint.
public void doPaint(Graphics2D g2d) {
g2d.translate(DIM * 0.2, DIM * 0.2);
Shape s = new Rectangle2D.Float(0, 0, DIM * 2, DIM * 2);
Point2D.Double p1 = new Point2D.Double(0.0, 0.0);
Point2D.Double p2 = new Point2D.Double(DIM / 2.0, DIM / 2.0);
// LinearGradientPaint
//g2d.translate(DIM*2.2, 0);
Color[] colors = { Color.red, Color.blue };
float[] fractions = { 0.0f, 1.0f };
LinearGradientPaint lgp = new LinearGradientPaint(p1, p2, fractions, colors, LinearGradientPaint.CycleMethod.NO_CYCLE);
g2d.setPaint(lgp);
g2d.fill(s);
g2d.translate(DIM * 2.2, 0);
Color[] colors1 = { Color.red, Color.blue, Color.green, Color.white };
float[] fractions1 = { 0.0f, 0.3f, 0.6f, 1.0f };
LinearGradientPaint lgp1 = new LinearGradientPaint(p1, p2, fractions1, colors1, LinearGradientPaint.CycleMethod.REFLECT);
g2d.setPaint(lgp1);
g2d.fill(s);
g2d.translate(-DIM * 2.2, DIM * 2.2);
Color[] colors2 = { Color.red, Color.blue, Color.green, Color.white };
float[] fractions2 = { 0.0f, 0.3f, 0.6f, 1.0f };
LinearGradientPaint lgp2 = new LinearGradientPaint(p1, p2, fractions2, colors2, LinearGradientPaint.CycleMethod.REPEAT);
g2d.setPaint(lgp2);
g2d.fill(s);
}
use of java.awt.LinearGradientPaint in project jdk8u_jdk by JetBrains.
the class BufferedPaints method setLinearGradientPaint.
/********************** LinearGradientPaint support *************************/
/**
* This method uses techniques that are nearly identical to those
* employed in setGradientPaint() above. The primary difference
* is that at the native level we use a fragment shader to manually
* apply the plane equation constants to the current fragment position
* to calculate the gradient position in the range [0,1] (the native
* code for GradientPaint does the same, except that it uses OpenGL's
* automatic texture coordinate generation facilities).
*
* One other minor difference worth mentioning is that
* setGradientPaint() calculates the plane equation constants
* such that the gradient end points are positioned at 0.25 and 0.75
* (for reasons discussed in the comments for that method). In
* contrast, for LinearGradientPaint we setup the equation constants
* such that the gradient end points fall at 0.0 and 1.0. The
* reason for this difference is that in the fragment shader we
* have more control over how the gradient values are interpreted
* (depending on the paint's CycleMethod).
*/
private static void setLinearGradientPaint(RenderQueue rq, SunGraphics2D sg2d, LinearGradientPaint paint, boolean useMask) {
boolean linear = (paint.getColorSpace() == ColorSpaceType.LINEAR_RGB);
Color[] colors = paint.getColors();
int numStops = colors.length;
Point2D pt1 = paint.getStartPoint();
Point2D pt2 = paint.getEndPoint();
AffineTransform at = paint.getTransform();
at.preConcatenate(sg2d.transform);
if (!linear && numStops == 2 && paint.getCycleMethod() != CycleMethod.REPEAT) {
// delegate to the optimized two-color gradient codepath
boolean isCyclic = (paint.getCycleMethod() != CycleMethod.NO_CYCLE);
setGradientPaint(rq, at, colors[0], colors[1], pt1, pt2, isCyclic, useMask);
return;
}
int cycleMethod = paint.getCycleMethod().ordinal();
float[] fractions = paint.getFractions();
int[] pixels = convertToIntArgbPrePixels(colors, linear);
// calculate plane equation constants
double x = pt1.getX();
double y = pt1.getY();
at.translate(x, y);
// now gradient point 1 is at the origin
x = pt2.getX() - x;
y = pt2.getY() - y;
double len = Math.sqrt(x * x + y * y);
at.rotate(x, y);
// now gradient point 2 is on the positive x-axis
at.scale(len, 1);
// now gradient point 1 is at (0.0, 0), point 2 is at (1.0, 0)
float p0, p1, p3;
try {
at.invert();
p0 = (float) at.getScaleX();
p1 = (float) at.getShearX();
p3 = (float) at.getTranslateX();
} catch (java.awt.geom.NoninvertibleTransformException e) {
p0 = p1 = p3 = 0.0f;
}
// assert rq.lock.isHeldByCurrentThread();
rq.ensureCapacity(20 + 12 + (numStops * 4 * 2));
RenderBuffer buf = rq.getBuffer();
buf.putInt(SET_LINEAR_GRADIENT_PAINT);
buf.putInt(useMask ? 1 : 0);
buf.putInt(linear ? 1 : 0);
buf.putInt(cycleMethod);
buf.putInt(numStops);
buf.putFloat(p0);
buf.putFloat(p1);
buf.putFloat(p3);
buf.put(fractions);
buf.put(pixels);
}
use of java.awt.LinearGradientPaint in project jdk8u_jdk by JetBrains.
the class RenderTests method makeLinear.
private LinearGradientPaint makeLinear(int numColors, boolean alpha) {
float interval = 1.0f / (numColors - 1);
float[] fractions = new float[numColors];
for (int i = 0; i < fractions.length; i++) {
fractions[i] = i * interval;
}
Color[] colors = makeGradientColors(numColors, alpha);
return new LinearGradientPaint(0.0f, 0.0f, 10.0f, 10.0f, fractions, colors, CycleMethod.REFLECT);
}
use of java.awt.LinearGradientPaint in project playn by threerings.
the class JavaGradient method createLinear.
static JavaGradient createLinear(float x0, float y0, float x1, float y1, float[] positions, int[] colors) {
Point2D.Float start = new Point2D.Float(x0, y0);
Point2D.Float end = new Point2D.Float(x1, y1);
Color[] javaColors = convertColors(colors);
LinearGradientPaint p = new LinearGradientPaint(start, end, positions, javaColors);
return new JavaGradient(p);
}
Aggregations