Java 8 AlphaComposite - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 AlphaComposite class.

This code example shows how to use the following methods:getAlpha, getRule
        // 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);
    } 
 
    /** 
     * Serialize a <code>TexturePaint</code>, restore it, and check for 
     * equality.  Since this class is not serializable, we expect null as the 
     * result. 
     */ 
    public void testTexturePaintSerialization() { 
 
        final Paint p1 = new TexturePaint(
                new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB),
                new Rectangle2D.Double(0, 0, 5, 5));
        Paint p2 = null;
5
CodeRank
This code example shows how to use the following methods:getAlpha, getRule
            if (composite instanceof Serializable) {
                stream.writeObject(composite);
            } 
            else if (composite instanceof AlphaComposite) {
                final AlphaComposite ac = (AlphaComposite) composite;
                stream.writeInt(ac.getRule());
                stream.writeFloat(ac.getAlpha());
            } 
        } 
        else { 
            stream.writeBoolean(true);
        } 
    } 
 
    /** 
     * Reads a <code>Shape</code> object that has been serialised by the 
     * {@link #writeShape(Shape, ObjectOutputStream)} method. 
     * 
     * @param stream  the input stream (<code>null</code> not permitted). 
     * 
5
CodeRank
This code example shows how to use the following methods:derive
        data[0] = 0x0000007f;
        data[1] = 0x0000007f;
        data[2] = 0xff00007f;
        data[3] = 0xff00007f;
        Graphics2D g = vi.createGraphics();
        g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
        g.drawImage(bi, 0, 0, null);
 
        bi = vi.getSnapshot();
        if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
            throw new RuntimeException("Test FAILED: color at 0x0 ="+
                Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
                Integer.toHexString(bi.getRGB(1,1)));
        } 
 
        System.out.println("Test PASSED.");
    } 
} 
5
CodeRank
This code example shows how to use the following methods:derive
 
		// gets the current clipping area 
		Rectangle clip = g.getClipBounds();
 
		// sets a 65% translucent composite 
		AlphaComposite alpha = AlphaComposite.SrcOver.derive(0.65f);
		Composite composite = g2.getComposite();
		g2.setComposite(alpha);
 
		// fills the background 
		g2.setColor(getBackground());
		g2.fillRect(clip.x, clip.y, clip.width, clip.height);
 
		// centers the progress bar on screen 
		FontMetrics metrics = g.getFontMetrics();
		int x = (getWidth() - BAR_WIDTH) / 2;
		int y = (getHeight() - BAR_HEIGHT - metrics.getDescent()) / 2;
 
		// draws the text 
		g2.setColor(TEXT_COLOR);
5
CodeRank
This code example shows how to use the following methods:getAlpha, getRule
            stream.writeObject(composite.getClass());
            if (composite instanceof Serializable) {
                stream.writeObject(composite);
            } else if (composite instanceof AlphaComposite) {
                AlphaComposite ac = (AlphaComposite) composite;
                stream.writeInt(ac.getRule());
                stream.writeFloat(ac.getAlpha());
            } 
        } else { 
            stream.writeBoolean(true);
        } 
    } 
 
    /** 
     * Reads a {@code Shape} object that has been serialised by the 
     * {@link #writeShape(Shape, ObjectOutputStream)} method. 
     * 
     * @param stream  the input stream ({@code null} not permitted). 
     * 
     * @return The shape object (possibly {@code null}). 
5
CodeRank
This code example shows how to use the following methods:derive
	Image original = GraphicsUtil.createImage(OecLogoBgWhite.class, LOGO_WIDTH, LOGO_HEIGHT);
	BufferedImage result = new BufferedImage(LOGO_WIDTH, LOGO_HEIGHT, BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = result.createGraphics();
 
	// draw original image with grey shade 
	g.setComposite(AlphaComposite.SrcOver.derive(0.3f));
	g.drawImage(original, 0, 0, null);
 
	g.dispose();
	return result;
    } 
 
} 
5
CodeRank
This code example shows how to use the following methods:derive
   * @param y the Y coordinate of the icon's top-left corner 
   */ 
  @Override 
  public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.SrcAtop.derive(alpha));
    icon.paintIcon(c, g2, x, y);
    g2.dispose();
  } 
 
  /** 
   * Gets the width of the bounding rectangle of this <CODE>AlphaIcon</CODE>. 
   * Returns the width of the wrapped icon. 
   * 
   * @return the width in pixels 
   */ 
  @Override 
  public int getIconWidth() { 
    return icon.getIconWidth();
  } 
5
CodeRank
This code example shows how to use the following methods:getAlpha
    } 
 
    @Override 
    public void paint (Graphics2D gfx)
    { 
        if (_alphaComposite.getAlpha() < 1.0f) {
            Composite ocomp = gfx.getComposite();
            gfx.setComposite(_alphaComposite);
            super.paint(gfx);
            gfx.setComposite(ocomp);
 
        } else { 
            super.paint(gfx);
        } 
    } 
 
    /** 
     * Sets the alpha value of this sprite. 
     */ 
    public void setAlpha (float alpha)
5
CodeRank
This code example shows how to use the following methods:getAlpha, getRule
    /** 
     * Convert an AlphaComposite object to a string. 
     */ 
    public static String compositeToString( AlphaComposite value )
    { 
        int ivalue = value.getRule();
        float fvalue = value.getAlpha();
        return Integer.toString( ivalue ) + ":" + Float.toString( fvalue );
    } 
 
    /** 
     * Convert a String object back to an AlphaComposite object. 
     */ 
    public static AlphaComposite compositeFromString( String value )
    { 
        String[] subValues = value.split( ":" );
        return 
            AlphaComposite.getInstance( PrimitiveXMLEncodeDecode
                                           .intFromString( subValues[0] ),
                                        (float) PrimitiveXMLEncodeDecode 
4
CodeRank
This code example shows how to use the following methods:derive
      image = ((ImageIcon) icon).getImage();
    } else { 
      image = null;
    } 
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.SrcAtop.derive(alpha));
    icon.paintIcon(c, g2, x, y);
    g2.dispose();
  } 
 
  /** 
   * Gets the width of the bounding rectangle of this <CODE>AlphaImageIcon</CODE>. 
   * Overridden to return the width of the wrapped icom. 
   * 
   * @return the width in pixels 
   */ 
  @Override 
  public int getIconWidth() { 
    return icon.getIconWidth();
  } 
4
CodeRank
See Code Examples for Java 8 AlphaComposite Methods: