下面列出了java.awt.MultipleGradientPaint.CycleMethod#REPEAT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Paints the background of non-title decoration areas.
*
* @param graphics
* Graphics context.
* @param parent
* Component ancestor for computing the correct offset of the background painting.
* @param comp
* Component.
* @param width
* Width.
* @param height
* Height.
* @param scheme
* Color scheme for painting the title background.
*/
private void paintExtraBackground(Graphics2D graphics, Container parent, Component comp,
int width, int height, SubstanceColorScheme scheme) {
Point offset = SubstanceCoreUtilities.getOffsetInRootPaneCoords(comp);
JRootPane rootPane = SwingUtilities.getRootPane(parent);
// fix for bug 234 - Window doesn't have a root pane.
JLayeredPane layeredPane = rootPane.getLayeredPane();
Insets layeredPaneInsets = (layeredPane != null) ? layeredPane.getInsets() : null;
int pWidth = (layeredPane == null) ? parent.getWidth()
: layeredPane.getWidth() - layeredPaneInsets.left - layeredPaneInsets.right;
if (pWidth != 0) {
LinearGradientPaint gradientBottom = new LinearGradientPaint(-offset.x, 0,
-offset.x + pWidth, 0, new float[] { 0.0f, 0.5f, 1.0f },
new Color[] { scheme.getMidColor(), scheme.getLightColor(),
scheme.getMidColor() },
CycleMethod.REPEAT);
Graphics2D g2d = (Graphics2D) graphics.create();
g2d.setPaint(gradientBottom);
g2d.fillRect(-offset.x, 0, pWidth, height);
g2d.dispose();
}
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
D3DSurfaceData dstData = (D3DSurfaceData)sg2d.surfaceData;
D3DGraphicsDevice gd = (D3DGraphicsDevice)
dstData.getDeviceConfiguration().getDevice();
if (gd.isCapPresent(CAPS_LCD_SHADER)) {
// we can delegate to the optimized two-color gradient
// codepath, which should be faster
return true;
}
}
return super.isPaintValid(sg2d);
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
// we can delegate to the optimized two-color gradient
// codepath, which does not require fragment shader support
return true;
}
return super.isPaintValid(sg2d);
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
// we can delegate to the optimized two-color gradient
// codepath, which does not require fragment shader support
return true;
}
return super.isPaintValid(sg2d);
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
// we can delegate to the optimized two-color gradient
// codepath, which does not require fragment shader support
return true;
}
return super.isPaintValid(sg2d);
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
// we can delegate to the optimized two-color gradient
// codepath, which does not require fragment shader support
return true;
}
return super.isPaintValid(sg2d);
}
@Override
public void paintBorder(Graphics g, Component c, float width, float height, Shape contour,
Shape innerContour, SubstanceColorScheme borderScheme) {
if (contour == null)
return;
Graphics2D graphics = (Graphics2D) g.create();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
Color[] drawColors = new Color[this.fractions.length];
for (int i = 0; i < this.fractions.length; i++) {
ColorSchemeSingleColorQuery colorQuery = this.colorQueries[i];
drawColors[i] = colorQuery.query(borderScheme);
}
// System.out.println("\t" + interpolationScheme1.getDisplayName()
// + " -> [" + cyclePos + "] "
// + interpolationScheme2.getDisplayName());
float strokeWidth = SubstanceSizeUtils.getBorderStrokeWidth();
// issue 433 - the "c" can be null when painting
// the border of a tree icon used outside the
// JTree context.
boolean isSpecialButton = (c != null) && c.getClass()
.isAnnotationPresent(SubstanceInternalArrowButton.class);
int joinKind = isSpecialButton ? BasicStroke.JOIN_MITER : BasicStroke.JOIN_ROUND;
int capKind = isSpecialButton ? BasicStroke.CAP_SQUARE : BasicStroke.CAP_BUTT;
graphics.setStroke(new BasicStroke(strokeWidth, capKind, joinKind));
MultipleGradientPaint gradient = new LinearGradientPaint(0, 0, 0, height, this.fractions,
drawColors, CycleMethod.REPEAT);
graphics.setPaint(gradient);
graphics.draw(contour);
graphics.dispose();
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
// we can delegate to the optimized two-color gradient
// codepath, which does not require fragment shader support
return true;
}
return super.isPaintValid(sg2d);
}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
if (paint.getFractions().length == 2 &&
paint.getCycleMethod() != CycleMethod.REPEAT &&
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
{
// we can delegate to the optimized two-color gradient
// codepath, which does not require fragment shader support
return true;
}
return super.isPaintValid(sg2d);
}
/**
* Helper function to index into the gradients array. This is necessary
* because each interval has an array of colors with uniform size 255.
* However, the color intervals are not necessarily of uniform length, so
* a conversion is required.
*
* @param position the unmanipulated position, which will be mapped
* into the range 0 to 1
* @returns integer color to display
*/
protected final int indexIntoGradientsArrays(float position) {
// first, manipulate position value depending on the cycle method
if (cycleMethod == CycleMethod.NO_CYCLE) {
if (position > 1) {
// upper bound is 1
position = 1;
} else if (position < 0) {
// lower bound is 0
position = 0;
}
} else if (cycleMethod == CycleMethod.REPEAT) {
// get the fractional part
// (modulo behavior discards integer component)
position = position - (int)position;
//position should now be between -1 and 1
if (position < 0) {
// force it to be in the range 0-1
position = position + 1;
}
} else { // cycleMethod == CycleMethod.REFLECT
if (position < 0) {
// take absolute value
position = -position;
}
// get the integer part
int part = (int)position;
// get the fractional part
position = position - part;
if ((part & 1) == 1) {
// integer part is odd, get reflected color instead
position = 1 - position;
}
}
// now, get the color based on this 0-1 position...
if (isSimpleLookup) {
// easy to compute: just scale index by array size
return gradient[(int)(position * fastGradientArraySize)];
} else {
// more complicated computation, to save space
// for all the gradient interval arrays
for (int i = 0; i < gradients.length; i++) {
if (position < fractions[i+1]) {
// this is the array we want
float delta = position - fractions[i];
// this is the interval we want
int index = (int)((delta / normalizedIntervals[i])
* (GRADIENT_SIZE_INDEX));
return gradients[i][index];
}
}
}
return gradients[gradients.length - 1][GRADIENT_SIZE_INDEX];
}
/**
* 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);
}
/**
* 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);
}
/**
* Retrieves a single crayon of the specified color and dimensions for the crayon panel in color
* chooser.
*
* @param mainColor
* Crayon main color.
* @param width
* Crayon width.
* @param height
* Crayon height.
* @return Crayon image.
*/
private static BufferedImage getSingleCrayon(Color mainColor, int width, int height) {
BufferedImage image = SubstanceCoreUtilities.getBlankImage(width, height);
int baseTop = (int) (0.2 * height);
Graphics2D graphics = (Graphics2D) image.getGraphics().create();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int r = mainColor.getRed();
int g = mainColor.getGreen();
int b = mainColor.getBlue();
// light coefficient
double lc = 0.8;
int lr = (int) (r + (255 - r) * lc);
int lg = (int) (g + (255 - g) * lc);
int lb = (int) (b + (255 - b) * lc);
// dark coefficient
double dc = 0.05;
int dr = (int) ((1.0 - dc) * r);
int dg = (int) ((1.0 - dc) * g);
int db = (int) ((1.0 - dc) * b);
Color lightColor = new Color(lr, lg, lb);
Color darkColor = new Color(dr, dg, db);
LinearGradientPaint paint = new LinearGradientPaint(0, 0, width, 0,
new float[] { 0.0f, 0.3f, 0.5f, 0.9f, 1.0f },
new Color[] { lightColor, darkColor, darkColor, lightColor, lightColor },
CycleMethod.REPEAT);
graphics.setPaint(paint);
graphics.fillRect(0, baseTop, width, height);
int dbwr = lr;
int dbwg = lg;
int dbwb = lb;
int lbwr = 128 + dr / 4;
int lbwg = 128 + dg / 4;
int lbwb = 128 + db / 4;
Color lightStripeColor = new Color(lbwr, lbwg, lbwb);
Color darkStripeColor = new Color(dbwr, dbwg, dbwb);
int stripeTop = (int) (0.35 * height);
int stripeHeight = (int) (0.04 * height);
LinearGradientPaint stripePaint = new LinearGradientPaint(0, 0, width, 0,
new float[] { 0.0f, 0.3f, 0.5f, 0.9f, 1.0f },
new Color[] { lightStripeColor, darkStripeColor, darkStripeColor, lightStripeColor,
lightStripeColor },
CycleMethod.REPEAT);
graphics.setPaint(stripePaint);
graphics.fillRect(0, stripeTop, width, stripeHeight);
graphics.setColor(lightStripeColor);
graphics.drawRect(0, stripeTop, width - 1, stripeHeight);
// create cap path
GeneralPath capPath = new GeneralPath();
capPath.moveTo(0.5f * width - 3, 4);
capPath.quadTo(0.5f * width, 0, 0.5f * width + 3, 4);
capPath.lineTo(width - 3, baseTop);
capPath.lineTo(2, baseTop);
capPath.lineTo(0.5f * width - 3, 4);
graphics.setClip(capPath);
RadialGradientPaint capPaint = new RadialGradientPaint(width / 2, baseTop, baseTop,
width / 2, 4 * baseTop / 3, new float[] { 0.0f, 0.1f, 1.0f },
new Color[] { mainColor, mainColor, lightColor }, CycleMethod.NO_CYCLE);
graphics.setPaint(capPaint);
graphics.fillRect(0, 0, width, baseTop);
graphics.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
graphics.setClip(null);
graphics.setColor(new Color(64 + dr / 2, 64 + dg / 2, 64 + db / 2, 200));
graphics.drawRect(0, baseTop, width - 1, height - baseTop - 1);
graphics.draw(capPath);
graphics.dispose();
return image;
}
/**
* 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);
}
/**
* Helper function to index into the gradients array. This is necessary
* because each interval has an array of colors with uniform size 255.
* However, the color intervals are not necessarily of uniform length, so
* a conversion is required.
*
* @param position the unmanipulated position, which will be mapped
* into the range 0 to 1
* @returns integer color to display
*/
protected final int indexIntoGradientsArrays(float position) {
// first, manipulate position value depending on the cycle method
if (cycleMethod == CycleMethod.NO_CYCLE) {
if (position > 1) {
// upper bound is 1
position = 1;
} else if (position < 0) {
// lower bound is 0
position = 0;
}
} else if (cycleMethod == CycleMethod.REPEAT) {
// get the fractional part
// (modulo behavior discards integer component)
position = position - (int)position;
//position should now be between -1 and 1
if (position < 0) {
// force it to be in the range 0-1
position = position + 1;
}
} else { // cycleMethod == CycleMethod.REFLECT
if (position < 0) {
// take absolute value
position = -position;
}
// get the integer part
int part = (int)position;
// get the fractional part
position = position - part;
if ((part & 1) == 1) {
// integer part is odd, get reflected color instead
position = 1 - position;
}
}
// now, get the color based on this 0-1 position...
if (isSimpleLookup) {
// easy to compute: just scale index by array size
return gradient[(int)(position * fastGradientArraySize)];
} else {
// more complicated computation, to save space
// for all the gradient interval arrays
for (int i = 0; i < gradients.length; i++) {
if (position < fractions[i+1]) {
// this is the array we want
float delta = position - fractions[i];
// this is the interval we want
int index = (int)((delta / normalizedIntervals[i])
* (GRADIENT_SIZE_INDEX));
return gradients[i][index];
}
}
}
return gradients[gradients.length - 1][GRADIENT_SIZE_INDEX];
}
/**
* 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);
}
/**
* 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);
}
/**
* Helper function to index into the gradients array. This is necessary
* because each interval has an array of colors with uniform size 255.
* However, the color intervals are not necessarily of uniform length, so
* a conversion is required.
*
* @param position the unmanipulated position, which will be mapped
* into the range 0 to 1
* @returns integer color to display
*/
protected final int indexIntoGradientsArrays(float position) {
// first, manipulate position value depending on the cycle method
if (cycleMethod == CycleMethod.NO_CYCLE) {
if (position > 1) {
// upper bound is 1
position = 1;
} else if (position < 0) {
// lower bound is 0
position = 0;
}
} else if (cycleMethod == CycleMethod.REPEAT) {
// get the fractional part
// (modulo behavior discards integer component)
position = position - (int)position;
//position should now be between -1 and 1
if (position < 0) {
// force it to be in the range 0-1
position = position + 1;
}
} else { // cycleMethod == CycleMethod.REFLECT
if (position < 0) {
// take absolute value
position = -position;
}
// get the integer part
int part = (int)position;
// get the fractional part
position = position - part;
if ((part & 1) == 1) {
// integer part is odd, get reflected color instead
position = 1 - position;
}
}
// now, get the color based on this 0-1 position...
if (isSimpleLookup) {
// easy to compute: just scale index by array size
return gradient[(int)(position * fastGradientArraySize)];
} else {
// more complicated computation, to save space
// for all the gradient interval arrays
for (int i = 0; i < gradients.length; i++) {
if (position < fractions[i+1]) {
// this is the array we want
float delta = position - fractions[i];
// this is the interval we want
int index = (int)((delta / normalizedIntervals[i])
* (GRADIENT_SIZE_INDEX));
return gradients[i][index];
}
}
}
return gradients[gradients.length - 1][GRADIENT_SIZE_INDEX];
}
/**
* Helper function to index into the gradients array. This is necessary
* because each interval has an array of colors with uniform size 255.
* However, the color intervals are not necessarily of uniform length, so
* a conversion is required.
*
* @param position the unmanipulated position, which will be mapped
* into the range 0 to 1
* @returns integer color to display
*/
protected final int indexIntoGradientsArrays(float position) {
// first, manipulate position value depending on the cycle method
if (cycleMethod == CycleMethod.NO_CYCLE) {
if (position > 1) {
// upper bound is 1
position = 1;
} else if (position < 0) {
// lower bound is 0
position = 0;
}
} else if (cycleMethod == CycleMethod.REPEAT) {
// get the fractional part
// (modulo behavior discards integer component)
position = position - (int)position;
//position should now be between -1 and 1
if (position < 0) {
// force it to be in the range 0-1
position = position + 1;
}
} else { // cycleMethod == CycleMethod.REFLECT
if (position < 0) {
// take absolute value
position = -position;
}
// get the integer part
int part = (int)position;
// get the fractional part
position = position - part;
if ((part & 1) == 1) {
// integer part is odd, get reflected color instead
position = 1 - position;
}
}
// now, get the color based on this 0-1 position...
if (isSimpleLookup) {
// easy to compute: just scale index by array size
return gradient[(int)(position * fastGradientArraySize)];
} else {
// more complicated computation, to save space
// for all the gradient interval arrays
for (int i = 0; i < gradients.length; i++) {
if (position < fractions[i+1]) {
// this is the array we want
float delta = position - fractions[i];
// this is the interval we want
int index = (int)((delta / normalizedIntervals[i])
* (GRADIENT_SIZE_INDEX));
return gradients[i][index];
}
}
}
return gradients[gradients.length - 1][GRADIENT_SIZE_INDEX];
}
/**
* 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);
}