下面列出了怎么用java.awt.MultipleGradientPaint.CycleMethod的API类实例代码及写法,或者点击链接到github查看源代码。
@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)
{
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);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == cmbPaint) {
paintType = (PaintType)cmbPaint.getSelectedItem();
} else if (source == cmbCycle) {
cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();
} else if (source == cmbSpace) {
colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();
} else if (source == cmbShape) {
shapeType = (ShapeType)cmbShape.getSelectedItem();
} else if (source == cmbXform) {
xformType = (XformType)cmbXform.getSelectedItem();
} else if (source == cbAntialias) {
antialiasHint = cbAntialias.isSelected() ?
RenderingHints.VALUE_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_OFF;
} else if (source == cbRender) {
renderHint = cbRender.isSelected() ?
RenderingHints.VALUE_RENDER_QUALITY :
RenderingHints.VALUE_RENDER_SPEED;
}
gradientPanel.updatePaint();
}
private AngleGradientPaintContext(ImDrag imDrag,
Color startColor, Color endColor,
ColorModel cm, CycleMethod cycleMethod) {
this.imDrag = imDrag;
this.cycleMethod = cycleMethod;
startAlpha = startColor.getAlpha();
startRed = startColor.getRed();
startGreen = startColor.getGreen();
startBlue = startColor.getBlue();
endAlpha = endColor.getAlpha();
endRed = endColor.getRed();
endGreen = endColor.getGreen();
endBlue = endColor.getBlue();
this.cm = cm;
drawAngle = imDrag.getDrawAngle();
}
@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)
{
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);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == cmbPaint) {
paintType = (PaintType)cmbPaint.getSelectedItem();
} else if (source == cmbCycle) {
cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();
} else if (source == cmbSpace) {
colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();
} else if (source == cmbShape) {
shapeType = (ShapeType)cmbShape.getSelectedItem();
} else if (source == cmbXform) {
xformType = (XformType)cmbXform.getSelectedItem();
} else if (source == cbAntialias) {
antialiasHint = cbAntialias.isSelected() ?
RenderingHints.VALUE_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_OFF;
} else if (source == cbRender) {
renderHint = cbRender.isSelected() ?
RenderingHints.VALUE_RENDER_QUALITY :
RenderingHints.VALUE_RENDER_SPEED;
}
gradientPanel.updatePaint();
}
/**
* 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
public void paintDecorationArea(Graphics2D graphics, Component comp, DecorationAreaType
decorationAreaType, Shape contour, SubstanceColorScheme colorScheme) {
Component parent = SubstanceCoreUtilities.getHeaderParent(comp);
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[] { colorScheme.getMidColor(), colorScheme.getLightColor(),
colorScheme.getMidColor() },
CycleMethod.REPEAT);
Graphics2D g2d = (Graphics2D) graphics.create();
g2d.setPaint(gradientBottom);
g2d.fill(contour);
g2d.dispose();
}
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == cmbPaint) {
paintType = (PaintType)cmbPaint.getSelectedItem();
} else if (source == cmbCycle) {
cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();
} else if (source == cmbSpace) {
colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();
} else if (source == cmbShape) {
shapeType = (ShapeType)cmbShape.getSelectedItem();
} else if (source == cmbXform) {
xformType = (XformType)cmbXform.getSelectedItem();
} else if (source == cbAntialias) {
antialiasHint = cbAntialias.isSelected() ?
RenderingHints.VALUE_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_OFF;
} else if (source == cbRender) {
renderHint = cbRender.isSelected() ?
RenderingHints.VALUE_RENDER_QUALITY :
RenderingHints.VALUE_RENDER_SPEED;
}
gradientPanel.updatePaint();
}
@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);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == cmbPaint) {
paintType = (PaintType)cmbPaint.getSelectedItem();
} else if (source == cmbCycle) {
cycleMethod = (CycleMethod)cmbCycle.getSelectedItem();
} else if (source == cmbSpace) {
colorSpace = (ColorSpaceType)cmbSpace.getSelectedItem();
} else if (source == cmbShape) {
shapeType = (ShapeType)cmbShape.getSelectedItem();
} else if (source == cmbXform) {
xformType = (XformType)cmbXform.getSelectedItem();
} else if (source == cbAntialias) {
antialiasHint = cbAntialias.isSelected() ?
RenderingHints.VALUE_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_OFF;
} else if (source == cbRender) {
renderHint = cbRender.isSelected() ?
RenderingHints.VALUE_RENDER_QUALITY :
RenderingHints.VALUE_RENDER_SPEED;
}
gradientPanel.updatePaint();
}
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);
}
private RadialGradientPaint makeRadial(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 RadialGradientPaint(0.0f, 0.0f, 10.0f,
fractions, colors,
CycleMethod.REFLECT);
}
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);
}
@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);
}
private void testOne(BufferedImage refImg, VolatileImage testImg) {
Graphics2D gref = refImg.createGraphics();
Graphics2D gtest = testImg.createGraphics();
Paint paint =
makePaint(PaintType.RADIAL, CycleMethod.REPEAT,
ColorSpaceType.SRGB, XformType.IDENTITY, 7);
Object aahint = hints[0];
renderTest(gref, paint, aahint);
renderTest(gtest, paint, aahint);
Toolkit.getDefaultToolkit().sync();
compareImages(refImg, testImg.getSnapshot(),
TOLERANCE, 0, "");
gref.dispose();
gtest.dispose();
}
private LinearGradientPaint createLinearGradientPaint(LinearGradient grad)
{
Point2D start = new Point2D.Float(grad.getX1(), grad.getY1());
Point2D end = new Point2D.Float(grad.getEfficientX2(), grad.getEfficientY2());
float[] dists = new float[grad.getStops().size()];
java.awt.Color[] colors = new java.awt.Color[grad.getStops().size()];
stopsToPaintValues(grad, dists, colors);
return new LinearGradientPaint(start, end, dists, colors,
grad.isRepeating() ? CycleMethod.REPEAT : CycleMethod.NO_CYCLE,
ColorSpaceType.SRGB,
new AffineTransform());
}
private BufferedImage createPlayerVisionImage(int circleRadius) {
float circleDiameter = circleRadius * 2.0f;
Shape circle = new Ellipse2D.Float(0, 0, circleDiameter, circleDiameter);
BufferedImage image = new BufferedImage((int)circleDiameter, (int)circleDiameter, BufferedImage.TYPE_INT_ARGB);
Graphics2D ga = (Graphics2D) image.createGraphics();
Point2D center = new Point2D.Float(circleRadius, circleRadius);
Point2D focus = new Point2D.Float(circleRadius, circleRadius);
float[] dist = {0.0f, 0.70f, 0.95f};
Color transparentBlack = new Color(0, 0, 0, 0);
Color[] colors = {transparentBlack, transparentBlack, Color.BLACK};
RadialGradientPaint p =
new RadialGradientPaint(center, circleRadius, focus,
dist, colors,
CycleMethod.NO_CYCLE);
ga.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ga.setPaint(p);
ga.fill(circle);
ga.setStroke(new BasicStroke(2));
ga.draw(circle);
ga.dispose();
return image;
}
@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);
}
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;
}
@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);
}
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);
}
private RadialGradientPaint makeRadial(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 RadialGradientPaint(0.0f, 0.0f, 10.0f,
fractions, colors,
CycleMethod.REFLECT);
}
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;
}
private void testOne(BufferedImage refImg, VolatileImage testImg) {
Graphics2D gref = refImg.createGraphics();
Graphics2D gtest = testImg.createGraphics();
Paint paint =
makePaint(PaintType.RADIAL, CycleMethod.REPEAT,
ColorSpaceType.SRGB, XformType.IDENTITY, 7);
Object aahint = hints[0];
renderTest(gref, paint, aahint);
renderTest(gtest, paint, aahint);
Toolkit.getDefaultToolkit().sync();
compareImages(refImg, testImg.getSnapshot(),
TOLERANCE, 0, "");
gref.dispose();
gtest.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);
}
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;
}
@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();
}