下面列出了java.awt.AlphaComposite#getInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void paint(Graphics g) {
// Create an image for the button graphics if necessary
if (buttonImage == null || buttonImage.getWidth() != getWidth() ||
buttonImage.getHeight() != getHeight()) {
buttonImage = getGraphicsConfiguration().
createCompatibleImage(getWidth(), getHeight());
}
Graphics gButton = buttonImage.getGraphics();
gButton.setClip(g.getClip());
// Have the superclass render the button for us
super.paint(gButton);
// Make the graphics object sent to this paint() method translucent
Graphics2D g2d = (Graphics2D)g;
AlphaComposite newComposite =
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(newComposite);
// Copy the button's image to the destination graphics, translucently
g2d.drawImage(buttonImage, 0, 0, null);
}
static public Composite getComposite( byte mode, float alpha )
{
final Composite composite;
switch ( mode )
{
case Displayable.COMPOSITE_ADD:
composite = AddARGBComposite.getInstance( alpha );
break;
case Displayable.COMPOSITE_SUBTRACT:
composite = SubtractARGBComposite.getInstance( alpha );
break;
case Displayable.COMPOSITE_MULTIPLY:
composite = MultiplyARGBComposite.getInstance( alpha );
break;
case Displayable.COMPOSITE_DIFFERENCE:
composite = DifferenceARGBComposite.getInstance( alpha );
break;
case Displayable.COMPOSITE_COLOR_YCBCR:
composite = ColorYCbCrComposite.getInstance( alpha );
break;
default:
composite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha );
}
return composite;
}
@Override
public void paintGhost(InstancePainter painter) {
Graphics g = painter.getGraphics();
Color fg = g.getColor();
int v = fg.getRed() + fg.getGreen() + fg.getBlue();
Composite oldComposite = null;
if (g instanceof Graphics2D && v > 50) {
oldComposite = ((Graphics2D) g).getComposite();
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
((Graphics2D) g).setComposite(c);
}
paintBase(painter, g);
if (oldComposite != null) {
((Graphics2D) g).setComposite(oldComposite);
}
}
/**
* Creates a new instance of DefaultPolarItemRenderer
*/
public DefaultPolarItemRenderer() {
this.seriesFilled = new BooleanList();
this.drawOutlineWhenFilled = true;
this.fillComposite = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.3f);
this.useFillPaint = false; // use item paint for fills by default
this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0);
this.shapesVisible = true;
this.connectFirstAndLastPoint = true;
this.toolTipGeneratorList = new ObjectList();
this.urlGenerator = null;
this.legendItemToolTipGenerator = null;
this.legendItemURLGenerator = null;
}
/**
* Creates a blend composite
*
* @param blendMode Desired blend mode
* @param constantAlpha Constant alpha, must be in the inclusive range [0.0...1.0] or it will be clipped.
* @return a blend composite.
*/
public static Composite getInstance(BlendMode blendMode, float constantAlpha)
{
if (constantAlpha < 0)
{
LOG.warn("using 0 instead of incorrect Alpha " + constantAlpha);
constantAlpha = 0;
}
else if (constantAlpha > 1)
{
LOG.warn("using 1 instead of incorrect Alpha " + constantAlpha);
constantAlpha = 1;
}
if (blendMode == BlendMode.NORMAL)
{
return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, constantAlpha);
}
else
{
return new BlendComposite(blendMode, constantAlpha);
}
}
/**
* Paints the current state (i.e. the state corresponding to the current
* phase) of the given component.
*
* @param g graphics context.
* @param comp component to paint.
*/
private void paintComponent(Graphics g, Component comp) {
Rectangle bounds = currentBounds(comp);
float alpha = currentAlpha(comp);
Graphics gg = g.create(bounds.x, bounds.y, bounds.width, bounds.height);
if (alpha != 1f) {
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
((Graphics2D)gg).setComposite(alphaComposite);
}
comp.setBounds(bounds);
comp.validate();
// Intentionally using print instead of paint.
// Print doesn't use double buffering and it solves some mysterious
// problems with modified clip during painting of containers.
// BTW: animated transitions library also uses print()
if (comp instanceof JComponent) {
comp.print(gg);
} else {
java.awt.peer.ComponentPeer peer = FakePeerSupport.getPeer(comp);
if (peer != null) {
peer.paint(gg);
}
}
gg.dispose();
}
/**
* Creates a new instance of DefaultPolarItemRenderer
*/
public DefaultPolarItemRenderer() {
this.seriesFilled = new BooleanList();
this.drawOutlineWhenFilled = true;
this.fillComposite = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.3f);
this.useFillPaint = false; // use item paint for fills by default
this.legendLine = new Line2D.Double(-7.0, 0.0, 7.0, 0.0);
this.shapesVisible = true;
this.connectFirstAndLastPoint = true;
this.toolTipGeneratorList = new ObjectList();
this.urlGenerator = null;
this.legendItemToolTipGenerator = null;
this.legendItemURLGenerator = null;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// �A���t�@�l
AlphaComposite composite = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.5f);
g2.setColor(Color.RED);
g2.fillOval(50, 50, 100, 100);
// �A���t�@�l���Z�b�g�i�Ȍ�̕`��͔������ɂȂ�j
g2.setComposite(composite);
g2.setColor(Color.BLUE);
g2.fillRect(90, 90, 100, 100);
}
/**
* Creates a new instance.
*
* @param gc the graphics context.
*/
public SWTGraphics2D(GC gc) {
super();
this.gc = gc;
this.hints = new RenderingHints(null);
this.composite = AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f);
setStroke(new BasicStroke());
}
/**
* Serialize an <code>AlphaComposite</code>, restore it, and check for
* equality.
*/
public void testAlphaCompositeSerialization() {
final Composite c1 = AlphaComposite.getInstance(2, 0.345f);
Composite c2 = null;
try {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ObjectOutputStream out = new ObjectOutputStream(buffer);
SerialUtilities.writeComposite(c1, out);
out.close();
final ByteArrayInputStream bias = new ByteArrayInputStream(
buffer.toByteArray());
final ObjectInputStream in = new ObjectInputStream(bias);
c2 = SerialUtilities.readComposite(in);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
// we want to check that the two objects are equal, but can't rely on
// AlphaComposite's equals() method because it is just the default
// method inherited from Object...
final AlphaComposite ac1 = (AlphaComposite) c1;
final AlphaComposite ac2 = (AlphaComposite) c2;
assertEquals(ac1.getRule(), ac2.getRule());
assertEquals(ac1.getAlpha(), ac2.getAlpha(), 0.001f);
}
/**
* Creates a new instance.
*
* @param gc the graphics context.
*/
public SWTGraphics2D(GC gc) {
super();
this.gc = gc;
this.hints = new RenderingHints(null);
this.composite = AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f);
setStroke(new BasicStroke());
}
public void setTransparency(float transparency) {
if ((transparency > 1.0) || (transparency < 0))
transparency = 1.0f;
int type = AlphaComposite.SRC_OVER;
alphaComp = (AlphaComposite.getInstance(type, transparency));
alphaCompOriginal = (AlphaComposite.getInstance(type, 1.0f));
}
private void rotateIconImage() {
final int W = 24;
final int H = 24;
final BufferedImage image = ImageHelper.getCompatibleBufferedImage(
W, H, BufferedImage.TRANSLUCENT
);
Graphics2D g2d = image.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// This makes a big difference to quality of rotated image if rotation is not a factor of 90 degrees.
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC) ;
g2d.setTransform(AffineTransform.getRotateInstance(
Math.toRadians(INTERVAL * position),
W * 0.5d, H * 0.5d)
);
g2d.setPaint(tintColor);
g2d.fillOval(2, 2, W-4, H-4);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OUT);
g2d.setComposite(ac);
g2d.fillOval((W - 8) / 2, 3, 8, 8);
g2d.dispose();
super.setIcon(new ImageIcon(image));
}
/**
* {@inheritDoc}
*/
@Override
public void setGlobalAlpha(final double globalAlpha) {
if (LOG.isDebugEnabled()) {
LOG.debug("[" + id_ + "] setGlobalAlpha(" + globalAlpha + ")");
}
if (globalAlpha >= 0 && globalAlpha <= 1) {
globalAlpha_ = (float) globalAlpha;
final AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, globalAlpha_);
graphics2D_.setComposite(composite);
}
}
/**
* Creates a new instance.
*
* @param gc the graphics context.
*/
public SWTGraphics2D(GC gc) {
super();
this.gc = gc;
this.hints = new RenderingHints(null);
this.composite = AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f);
setStroke(new BasicStroke());
}
public static BufferedImage indicateCircle(BufferedImage source,
Color color, int lineWidth, DoubleCircle circle) {
try {
if (!circle.isValid()) {
return source;
}
int width = source.getWidth();
int height = source.getHeight();
int imageType = source.getType();
if (imageType == BufferedImage.TYPE_CUSTOM) {
imageType = BufferedImage.TYPE_INT_ARGB;
}
BufferedImage target = new BufferedImage(width, height, imageType);
Graphics2D g = target.createGraphics();
g.drawImage(source, 0, 0, width, height, null);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
g.setComposite(ac);
g.setColor(color);
BasicStroke stroke = new BasicStroke(lineWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
1f, new float[]{lineWidth, lineWidth}, 0f);
g.setStroke(stroke);
g.drawOval((int) circle.getCenterX() - (int) circle.getRadius(), (int) circle.getCenterY() - (int) circle.getRadius(),
2 * (int) circle.getRadius(), 2 * (int) circle.getRadius());
g.dispose();
return target;
} catch (Exception e) {
logger.error(e.toString());
return source;
}
}
public void paint(Graphics _g) {
// System.out.println("paint()");
if(isPainting) return;
isPainting = true;
synchronized(paintLock) {
zoom();
if(use2D) {
if(alphaHalf == null) {
alphaHalf = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .7f);
}
}
Graphics g = _g;
makeOff(false);
if(memG == null) return;
long start = System.currentTimeMillis();
paintBg(memG);
bgTime = System.currentTimeMillis() - start;
if(use2D) {
((Graphics2D)memG).
setRenderingHint(RenderingHints.KEY_ANTIALIASING,
bAntiAlias
? RenderingHints.VALUE_ANTIALIAS_ON
: RenderingHints.VALUE_ANTIALIAS_OFF);
}
paintBundles(memG);
spriteTime = System.currentTimeMillis() - bgTime - start;
paintServices(memG);
fgTime = System.currentTimeMillis() - spriteTime - start;
if(bShowBundleLegend) {
paintBundleStateLegend(memG);
}
SpinItem infoItem = getInfoItem();
if(infoItem != null) {
if(bShowBundleInfo) {
infoItem.paintInfo(memG, 10, size.height - 100);
}
}
if(bShowConsole) {
console.paint(memG);
}
if(bShowFrameRate) {
paintFrameRate(memG);
}
if(bShowHelp) {
paintHelp(memG, size.width/2-100, 50);
}
if(bShowDeps) {
paintDeps(memG,
(hotX > size.width / 2) ? 40 : (size.width / 2),
20);
}
if(bSearchMode) {
paintSearch(memG, 10, size.height - 140);
}
if(use2D) {
((Graphics2D)memG).
setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
g.drawImage(memImage, 0, 0, null);
}
isPainting = false;
}
private AlphaComposite makeComposite(double alpha) {
int type = AlphaComposite.SRC_OVER;
return (AlphaComposite.getInstance(type, (float) alpha));
}
/**
* combineBufferedImage.
* SCIPIO: 2017-07-10: modified to take a typeReferenceImage instance of bufImgType, so we have the full
* information to replicate the original image type, needed for indexed images.
*/
public static BufferedImage combineBufferedImage(Image image1, Image image2, BufferedImage typeReferenceImage) {
// Full image loading
image1 = new ImageIcon(image1).getImage();
image2 = new ImageIcon(image2).getImage();
// New BufferedImage creation
// SCIPIO: indexed images fix
BufferedImage bufferedImage;
Graphics2D g;
if (image1 instanceof BufferedImage && ImagePixelType.isTypeImageOpFriendly(((BufferedImage) image1).getType())) {
// still create a copy to avoid modifying the original
bufferedImage = ImageTransform.cloneBufferedImage((BufferedImage) image1);
g = bufferedImage.createGraphics();
} else {
bufferedImage = ImageTransform.createBufferedImage(ImageType.DEFAULT_IMAGEOP.getImageTypeInfoFor(typeReferenceImage), image1.getWidth(null),
image1.getHeight(null));
g = bufferedImage.createGraphics();
g.drawImage(image1, null, null);
}
// Draw Image combine
Point2D center = new Point2D.Float(bufferedImage.getHeight() / 2f, bufferedImage.getWidth() / 2f);
AffineTransform at = AffineTransform.getTranslateInstance(center.getX( ) - (image2.getWidth(null) / 2f), center.getY( ) - (image2.getHeight(null) / 2f));
g.transform(at);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(image2, 0, 0, null);
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .35f);
g.setComposite(c);
at = AffineTransform.getTranslateInstance(center.getX( ) - (bufferedImage.getWidth(null) / 2f), center.getY( ) - (bufferedImage.getHeight(null) / 2f));
g.setTransform(at);
g.drawImage(bufferedImage, 0, 0, null);
g.dispose();
// SCIPIO: new: we convert to the target type only at the very end, in separate step, so the previous operations don't suffer from color loss
if (ImageType.imageMatchesRequestedType(bufferedImage, typeReferenceImage)) {
return bufferedImage;
} else {
BufferedImage resultImage = ImageTransform.createCompatibleBufferedImage(typeReferenceImage, bufferedImage.getWidth(null), bufferedImage.getHeight(null));
ImageTransform.copyToBufferedImage(bufferedImage, resultImage);
return( resultImage );
}
}
public BufferedImage drawData() {
try {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g = image.createGraphics();
if (bgColor != null) {
g.setColor(bgColor);
g.fillRect(0, 0, width, height);
}
if (fontSize <= 0) {
fontSize = 20;
}
dataFont = new Font(null, Font.PLAIN, fontSize);
commentsFont = new Font(null, Font.BOLD, fontSize + 8);
if (Color.BLACK.equals(bgColor)) {
textColor = Color.WHITE;
} else {
textColor = Color.BLACK;
}
// Title / Bottom
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
g.setComposite(ac);
if (title == null) {
title = message("ChromaticityDiagram");
}
g.setFont(commentsFont);
g.setColor(textColor);
g.drawString(title, margins + 400, 50);
g.setFont(dataFont);
g.drawString(message("ChromaticityDiagramComments"), 20, endH + 55);
backGround();
outlines();
whitePoints();
primariesLines();
calculate();
g.dispose();
return image;
} catch (Exception e) {
logger.error(e.toString());
return null;
}
}