Java Code Examples for java.awt.AlphaComposite

The following are top voted examples for showing how to use java.awt.AlphaComposite. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: jmt   File: Convex2DGraph.java   Source Code and License 7 votes vote down vote up
/**
 * It draw a temporary point when a point is moved in another place
 * 
 * @param g
 *            The graphic object
 * @param p
 *            The position of the point
 * @param c
 *            The color of the point
 * @param size
 *            The size of the point
 */
public void drawShadowPoint(Graphics2D g, Point p, Color c, int size) {
	g.setColor(c);

	int fontSize = 7 + getPointSize();
	Font f = new Font("Arial", Font.PLAIN, fontSize);
	g.setFont(f);
	double x = Math.max(p.getX(), GRAPH_LEFT_MARGIN);
	double y = Math.min(p.getY(), getHeight() - GRAPH_BOTTOM_MARGIN);

	g.drawString("(" + FORMAT_3_DEC.format(getTrueX(x)) + ", "
			+ FORMAT_3_DEC.format(getTrueY(y)) + ")",
			(int) (x - (fontSize * 3)), (int) y - 5 - getPointSize());

	g.drawOval((int) x - (((size / 2))), (int) y - (((size / 2))), size,
			size);

	g.setColor(Color.GRAY);
	Composite oldComp = g.getComposite();
	Composite alphaComp = AlphaComposite.getInstance(
			AlphaComposite.SRC_OVER, 0.3f);
	g.setComposite(alphaComp);
	g.fillOval((int) x - (((size / 2))), (int) y - (((size / 2))), size,
			size);
	g.setComposite(oldComp);
}
 
Example 2
Project: jmt   File: PainterConvex2D.java   Source Code and License 7 votes vote down vote up
/**
 * Draw a semi-trasparet area
 * @param g The graphic object
 * @param dragPoint The first point
 * @param beginPoint The second point
 * @param c The color of the area
 */
public void drawDragArea(Graphics2D g, Point dragPoint, Point beginPoint, Color c) {
	g.setColor(c);

	Polygon poly = new Polygon();

	poly.addPoint((int) beginPoint.getX(), (int) beginPoint.getY());
	poly.addPoint((int) beginPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) beginPoint.getY());

	//Set the widths of the shape's outline
	Stroke oldStro = g.getStroke();
	Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	g.setStroke(stroke);
	g.drawPolygon(poly);
	g.setStroke(oldStro);

	//Set the trasparency of the iside of the rectangle
	Composite oldComp = g.getComposite();
	Composite alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
	g.setComposite(alphaComp);
	g.fillPolygon(poly);
	g.setComposite(oldComp);

}
 
Example 3
Project: openjdk-jdk10   File: DrawImage.java   Source Code and License 6 votes vote down vote up
/**
 * Return a non-accelerated BufferedImage of the requested type with the
 * indicated subimage of the original image located at 0,0 in the new image.
 * If a bgColor is supplied, composite the original image over that color
 * with a SrcOver operation, otherwise make a SrcNoEa copy.
 * <p>
 * Returned BufferedImage is not accelerated for two reasons:
 * <ul>
 * <li> Types of the image and surface are predefined, because these types
 *      correspond to the TransformHelpers, which we know we have. And
 *      acceleration can change the type of the surface
 * <li> Image will be used only once and acceleration caching wouldn't help
 * </ul>
 */
private BufferedImage makeBufferedImage(Image img, Color bgColor, int type,
                                        int sx1, int sy1, int sx2, int sy2)
{
    final int width = sx2 - sx1;
    final int height = sy2 - sy1;
    final BufferedImage bimg = new BufferedImage(width, height, type);
    final SunGraphics2D g2d = (SunGraphics2D) bimg.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    bimg.setAccelerationPriority(0);
    if (bgColor != null) {
        g2d.setColor(bgColor);
        g2d.fillRect(0, 0, width, height);
        g2d.setComposite(AlphaComposite.SrcOver);
    }
    g2d.copyImage(img, 0, 0, sx1, sy1, width, height, null, null);
    g2d.dispose();
    return bimg;
}
 
Example 4
Project: jdk8u-jdk   File: ImageTests.java   Source Code and License 6 votes vote down vote up
public void modifyTest(TestEnvironment env) {
    int size = env.getIntValue(sizeList);
    Image src = tsit.getImage(env, size, size);
    Graphics g = src.getGraphics();
    if (hasGraphics2D) {
        ((Graphics2D) g).setComposite(AlphaComposite.Src);
    }
    if (size == 1) {
        g.setColor(colorsets[transparency][4]);
        g.fillRect(0, 0, 1, 1);
    } else {
        int mid = size/2;
        g.setColor(colorsets[transparency][0]);
        g.fillRect(0, 0, mid, mid);
        g.setColor(colorsets[transparency][1]);
        g.fillRect(mid, 0, size-mid, mid);
        g.setColor(colorsets[transparency][2]);
        g.fillRect(0, mid, mid, size-mid);
        g.setColor(colorsets[transparency][3]);
        g.fillRect(mid, mid, size-mid, size-mid);
    }
    g.dispose();
    env.setSrcImage(src);
}
 
Example 5
Project: incubator-netbeans   File: BalloonManager.java   Source Code and License 6 votes vote down vote up
@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    
    g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    
    Composite oldC = g2d.getComposite();
    Shape s = getMask( getWidth(), getHeight() );

    g2d.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.25f*currentAlpha ) );
    g2d.setColor( Color.black );
    g2d.fill( getShadowMask(s) );
    
    g2d.setColor( UIManager.getColor( "ToolTip.background" ) ); //NOI18N
    g2d.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, currentAlpha ) );
    Point2D p1 = s.getBounds().getLocation();
    Point2D p2 = new Point2D.Double(p1.getX(), p1.getY()+s.getBounds().getHeight());
    if( isMouseOverEffect )
        g2d.setPaint( new GradientPaint( p2, getMouseOverGradientStartColor(), p1, getMouseOverGradientFinishColor() ) );
    else
        g2d.setPaint( new GradientPaint( p2, getDefaultGradientStartColor(), p1, getDefaultGradientFinishColor() ) );
    g2d.fill(s);
    g2d.setColor( Color.black );
    g2d.draw(s);
    g2d.setComposite( oldC );
}
 
Example 6
Project: openjdk-jdk10   File: IncorrectClipSurface2SW.java   Source Code and License 6 votes vote down vote up
private static void draw(Shape clip, Shape to, Image vi, BufferedImage bi,
                         int scale) {
    Graphics2D big = bi.createGraphics();
    big.setComposite(AlphaComposite.Src);
    big.setClip(clip);
    Rectangle toBounds = to.getBounds();
    int x1 = toBounds.x;

    int y1 = toBounds.y;
    int x2 = x1 + toBounds.width;
    int y2 = y1 + toBounds.height;
    big.drawImage(vi, x1, y1, x2, y2, 0, 0, toBounds.width / scale,
                  toBounds.height / scale, null);
    big.dispose();
    vi.flush();
}
 
Example 7
Project: jdk8u-jdk   File: PeekMetrics.java   Source Code and License 6 votes vote down vote up
/**
 * Record information about drawing done
 * with the supplied <code>Composite</code>.
 */
private void checkAlpha(Composite composite) {

    if (composite instanceof AlphaComposite) {
        AlphaComposite alphaComposite = (AlphaComposite) composite;
        float alpha = alphaComposite.getAlpha();
        int rule = alphaComposite.getRule();

        if (alpha != 1.0
                || (rule != AlphaComposite.SRC
                    && rule != AlphaComposite.SRC_OVER)) {

            mHasCompositing = true;
        }

    } else {
        mHasCompositing = true;
    }

}
 
Example 8
Project: BasicsProject   File: GifCaptcha.java   Source Code and License 6 votes vote down vote up
/**
 * 画随机码图
 * @param fontcolor 随机字体颜色
 * @param strs 字符数组
 * @param flag 透明度使用
 * @return BufferedImage
 */
private BufferedImage graphicsImage(Color[] fontcolor,char[] strs,int flag)
{
    BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
    //或得图形上下文
    //Graphics2D g2d=image.createGraphics();
    Graphics2D g2d = (Graphics2D)image.getGraphics();
    //利用指定颜色填充背景
    g2d.setColor(Color.WHITE);
    g2d.fillRect(0, 0, width, height);
    AlphaComposite ac3;
    int h  = height - ((height - font.getSize()) >>1) ;
    int w = width/len;
    g2d.setFont(font);
    for(int i=0;i<len;i++)
    {
        ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i));
        g2d.setComposite(ac3);
        g2d.setColor(fontcolor[i]);
        g2d.drawOval(num(width), num(height), 5+num(10), 5+num(10));
        g2d.drawString(strs[i]+"", (width-(len-i)*w)+(w-font.getSize())+1, h-4);
    }
    g2d.dispose();
    return image;
}
 
Example 9
Project: smile_1.5.0_java7   File: Graphics.java   Source Code and License 6 votes vote down vote up
/**
 * Fill polygon. The coordinates are in logical coordinates. This also supports
 * basic alpha compositing rules for combining source and destination
 * colors to achieve blending and transparency effects with graphics and images.
 *
 * @param alpha the constant alpha to be multiplied with the alpha of the
 *     source. alpha must be a floating point number in the inclusive range
 *     [0.0, 1.0].
 */
public void fillPolygon(float alpha, double[]... coord) {
    int[][] c = new int[coord.length][2];
    for (int i = 0; i < coord.length; i++) {
        c[i] = projection.screenProjection(coord[i]);
    }

    int[] x = new int[c.length];
    for (int i = 0; i < c.length; i++) {
        x[i] = c[i][0];
    }
    int[] y = new int[c.length];
    for (int i = 0; i < c.length; i++) {
        y[i] = c[i][1];
    }

    Composite cs = g2d.getComposite();
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

    g2d.fillPolygon(x, y, c.length);

    g2d.setComposite(cs);
}
 
Example 10
Project: sbc-qsystem   File: FInfoDialog.java   Source Code and License 6 votes vote down vote up
private Image resizeToBig(Image originalImage, int biggerWidth, int biggerHeight) {
    final BufferedImage resizedImage = new BufferedImage(biggerWidth, biggerHeight,
        BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = resizedImage.createGraphics();

    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.drawImage(originalImage, 0, 0, biggerWidth, biggerHeight, this);
    g.dispose();

    return resizedImage;
}
 
Example 11
Project: OpenJSharp   File: SunGraphics2D.java   Source Code and License 6 votes vote down vote up
final void validateColor() {
    int eargb;
    if (imageComp == CompositeType.Clear) {
        eargb = 0;
    } else {
        eargb = foregroundColor.getRGB();
        if (compositeState <= COMP_ALPHA &&
            imageComp != CompositeType.SrcNoEa &&
            imageComp != CompositeType.SrcOverNoEa)
        {
            AlphaComposite alphacomp = (AlphaComposite) composite;
            int a = Math.round(alphacomp.getAlpha() * (eargb >>> 24));
            eargb = (eargb & 0x00ffffff) | (a << 24);
        }
    }
    this.eargb = eargb;
    this.pixel = surfaceData.pixelFor(eargb);
}
 
Example 12
Project: VASSAL-src   File: SymbolSet.java   Source Code and License 6 votes vote down vote up
/**
 * Get image corresponding to this symbol. Generates the image and applies
 * optional mask if not already done so.
 * @param rect2 width and height are taken from this for otherwise invalid masks
 */
private BufferedImage getImage(Rectangle rect2) {
  if (img == null) {
    if ( isMask && (rect.width <= 0 || rect.height <= 0
        || rect.width+rect.x > bitmap.getWidth()
        || rect.height+rect.y > bitmap.getHeight() )) {
      // Images with invalid masks appear to be completely transparent.
      // This is a hassle generating new ones all the time, but there's nothing
      // to say that the real mask can't be different sizes at every call,
      // and anything else seems like overkill -- so this is an ugly kludge.
      // Hopefully, this crime against nature doesn't happen very often.
      return new BufferedImage(rect2.width, rect2.height, BufferedImage.TYPE_INT_ARGB);
    }
    img = bitmap.getSubimage(rect.x, rect.y, rect.width, rect.height);
    if (getMask() != null) {
      final BufferedImage bi = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB);
      final Graphics2D g = bi.createGraphics();
      g.drawImage(img, null, 0, 0);
      g.setComposite(AlphaComposite.DstAtop);
      g.drawImage(getMask().getImage(rect), null, 0, 0);
      img = bi;
    }
  }
  return img;
}
 
Example 13
Project: openjdk-jdk10   File: UnmanagedDrawImagePerformance.java   Source Code and License 6 votes vote down vote up
private static long test(Image bi, Image vi, AffineTransform atfm) {
    final Polygon p = new Polygon();
    p.addPoint(0, 0);
    p.addPoint(SIZE, 0);
    p.addPoint(0, SIZE);
    p.addPoint(SIZE, SIZE);
    p.addPoint(0, 0);
    Graphics2D g2d = (Graphics2D) vi.getGraphics();
    g2d.clip(p);
    g2d.transform(atfm);
    g2d.setComposite(AlphaComposite.SrcOver);
    final long start = System.nanoTime();
    g2d.drawImage(bi, 0, 0, null);
    final long time = System.nanoTime() - start;
    g2d.dispose();
    return time;
}
 
Example 14
Project: TrabalhoFinalEDA2   File: mxUtils.java   Source Code and License 6 votes vote down vote up
/**
 * Clears the given area of the specified graphics object with the given
 * color or makes the region transparent.
 */
public static void clearRect(Graphics2D g, Rectangle rect, Color background)
{
	if (background != null)
	{
		g.setColor(background);
		g.fillRect(rect.x, rect.y, rect.width, rect.height);
	}
	else
	{
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR,
				0.0f));
		g.fillRect(rect.x, rect.y, rect.width, rect.height);
		g.setComposite(AlphaComposite.SrcOver);
	}
}
 
Example 15
Project: VASSAL-src   File: WizardSupport.java   Source Code and License 6 votes vote down vote up
public void setBackgroundImage(Image image) {
  if (image != null) {
    final ImageIcon icon = new ImageIcon(image);
    logoSize = new Dimension(icon.getIconWidth(), icon.getIconHeight());
    final BufferedImage img =
      ImageUtils.createCompatibleTranslucentImage(logoSize.width,
                                                  logoSize.height);
    Graphics2D g = img.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
    icon.paintIcon(null, g, 0, 0);
    g.dispose();
    UIManager.put("wizard.sidebar.image", img); //$NON-NLS-1$
  }
}
 
Example 16
Project: brModelo   File: Tabela.java   Source Code and License 6 votes vote down vote up
private void FillCampos(Graphics2D g, Rectangle r, boolean normal) {
    Composite originalComposite = g.getComposite();
    float alfa = 1f - getAlfa();
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alfa));
    Paint bkpp = g.getPaint();
    g.setColor(getMaster().getBackground()); //# Não: isDisablePainted()? disabledColor : 
    if (!normal) {
        if (isGradiente()) {
            g.setColor(getGradienteStartColor());
        } else {
            g.setColor(getForeColor());
        }
    }
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlfa()));
    g.fill(r);

    g.setPaint(bkpp);
    g.setComposite(originalComposite);
}
 
Example 17
Project: StreamDeckCore   File: IconHelper.java   Source Code and License 6 votes vote down vote up
public static BufferedImage createResizedCopy(BufferedImage originalImage) {
	int scaledWidth = StreamDeck.ICON_SIZE;
	int scaledHeight = StreamDeck.ICON_SIZE;
	if (originalImage.getWidth() != originalImage.getHeight()) {
		float scalerWidth = ((float) StreamDeck.ICON_SIZE) / originalImage.getWidth();
		float scalerHeight = ((float) StreamDeck.ICON_SIZE) / originalImage.getWidth();
		if (scalerWidth < scaledHeight)
			scaledHeight = Math.round(scalerWidth * originalImage.getHeight());
		else
			scaledWidth = Math.round(scalerHeight * originalImage.getWidth());

	}
	int imageType = BufferedImage.TYPE_INT_ARGB;
	BufferedImage scaledBI = new BufferedImage(StreamDeck.ICON_SIZE, StreamDeck.ICON_SIZE, imageType);
	Graphics2D g = scaledBI.createGraphics();
	if (true) {
		g.setComposite(AlphaComposite.Src);
	}
	g.drawImage(originalImage, (StreamDeck.ICON_SIZE - scaledWidth) / 2, (StreamDeck.ICON_SIZE - scaledHeight) / 2,
			scaledWidth, scaledHeight, null);
	g.dispose();
	return scaledBI;
}
 
Example 18
Project: parabuild-ci   File: Plot.java   Source Code and License 6 votes vote down vote up
/**
 * Draws the background image (if there is one) aligned within the 
 * specified area.
 * 
 * @param g2  the graphics device.
 * @param area  the area.
 * 
 * @see #getBackgroundImage()
 * @see #getBackgroundImageAlignment()
 * @see #getBackgroundImageAlpha()
 */
protected void drawBackgroundImage(Graphics2D g2, Rectangle2D area) {
    if (this.backgroundImage != null) {
        Composite originalComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
                this.backgroundImageAlpha));
        Rectangle2D dest = new Rectangle2D.Double(0.0, 0.0,
                this.backgroundImage.getWidth(null), 
                this.backgroundImage.getHeight(null));
        Align.align(dest, area, this.backgroundImageAlignment);
        g2.drawImage(this.backgroundImage, (int) dest.getX(), 
                (int) dest.getY(), (int) dest.getWidth() + 1, 
                (int) dest.getHeight() + 1, null);
        g2.setComposite(originalComposite);
    }
}
 
Example 19
Project: QN-ACTR-Release   File: PainterConvex2D.java   Source Code and License 6 votes vote down vote up
/**
 * Draw a semi-trasparet area
 * @param g The graphic object
 * @param dragPoint The first point
 * @param beginPoint The second point
 * @param c The color of the area
 */
public void drawDragArea(Graphics2D g, Point dragPoint, Point beginPoint, Color c) {
	g.setColor(c);

	Polygon poly = new Polygon();

	poly.addPoint((int) beginPoint.getX(), (int) beginPoint.getY());
	poly.addPoint((int) beginPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) dragPoint.getY());
	poly.addPoint((int) dragPoint.getX(), (int) beginPoint.getY());

	//Set the widths of the shape's outline
	Stroke oldStro = g.getStroke();
	Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
	g.setStroke(stroke);
	g.drawPolygon(poly);
	g.setStroke(oldStro);

	//Set the trasparency of the iside of the rectangle
	Composite oldComp = g.getComposite();
	Composite alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
	g.setComposite(alphaComp);
	g.fillPolygon(poly);
	g.setComposite(oldComp);

}
 
Example 20
Project: openjdk-jdk10   File: SunGraphics2D.java   Source Code and License 6 votes vote down vote up
void validateColor() {
    int eargb;
    if (imageComp == CompositeType.Clear) {
        eargb = 0;
    } else {
        eargb = foregroundColor.getRGB();
        if (compositeState <= COMP_ALPHA &&
            imageComp != CompositeType.SrcNoEa &&
            imageComp != CompositeType.SrcOverNoEa)
        {
            AlphaComposite alphacomp = (AlphaComposite) composite;
            int a = Math.round(alphacomp.getAlpha() * (eargb >>> 24));
            eargb = (eargb & 0x00ffffff) | (a << 24);
        }
    }
    this.eargb = eargb;
    this.pixel = surfaceData.pixelFor(eargb);
}
 
Example 21
Project: BrainControl   File: LevelRenderer.java   Source Code and License 6 votes vote down vote up
public LevelRenderer(Level level, GraphicsConfiguration graphicsConfiguration, int width, int height) {
		this.width = width;
		this.height = height;

		this.level = level;
		image = graphicsConfiguration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		
		g = (Graphics2D) image.getGraphics();
		g.setComposite(AlphaComposite.Src);
		
//		//ENABLE PARTIAL TRANSPARENCY FOR LEVEL (AND BACKGROUND?)
//		AlphaComposite ac = java.awt.AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
//        g.setComposite(ac);		

		updateArea(0, 0, width, height);
	}
 
Example 22
Project: Tarski   File: mxGraphHandler.java   Source Code and License 6 votes vote down vote up
/**
 *
 */
public void paint(Graphics g) {
  if (isVisible() && previewBounds != null) {
    if (dragImage != null) {
      // LATER: Clipping with mxUtils doesnt fix the problem
      // of the drawImage being painted over the scrollbars
      Graphics2D tmp = (Graphics2D) g.create();

      if (graphComponent.getPreviewAlpha() < 1) {
        tmp.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            graphComponent.getPreviewAlpha()));
      }

      tmp.drawImage(dragImage.getImage(), previewBounds.x, previewBounds.y,
          dragImage.getIconWidth(), dragImage.getIconHeight(), null);
      tmp.dispose();
    } else if (!imagePreview) {
      mxSwingConstants.PREVIEW_BORDER.paintBorder(graphComponent, g, previewBounds.x,
          previewBounds.y, previewBounds.width, previewBounds.height);
    }
  }
}
 
Example 23
Project: geomapapp   File: MaskToWorldWindTiler.java   Source Code and License 6 votes vote down vote up
private static BufferedImage resizeImage(BufferedImage sample, double sampleMinLat, double sampleMaxLat, double tileMinLat, double tileMaxLat) {
	BufferedImage img = new BufferedImage(TILE_SIZE, TILE_SIZE, BufferedImage.TYPE_INT_ARGB);
	for (int x = 0; x < TILE_SIZE; x++)
		for (int y = 0; y < TILE_SIZE; y++)
			img.setRGB(x, y, 0x80000000);
	
	double tileScale = TILE_SIZE / (tileMinLat - tileMaxLat);
	
	// s for source; d for destination; all measurements in pixels
	int sx1 = 0;
	int sy1 = 0;
	int sx2 = sample.getWidth();
	int sy2 = sample.getHeight();
	
	int dx1 = 0;
	int dy1 = (int) ((sampleMaxLat - tileMaxLat) * tileScale);
	int dx2 = TILE_SIZE;
	int dy2 = (int) ((sampleMinLat - tileMaxLat) * tileScale);
	
	Graphics2D g = img.createGraphics();
	g.setComposite( AlphaComposite.Src );
	g.drawImage(sample, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
	
	return img;
}
 
Example 24
Project: openjdk-jdk10   File: ImageTests.java   Source Code and License 6 votes vote down vote up
public void modifyTest(TestEnvironment env) {
    int size = env.getIntValue(sizeList);
    Image src = tsit.getImage(env, size, size);
    Graphics g = src.getGraphics();
    if (hasGraphics2D) {
        ((Graphics2D) g).setComposite(AlphaComposite.Src);
    }
    if (size == 1) {
        g.setColor(colorsets[transparency][4]);
        g.fillRect(0, 0, 1, 1);
    } else {
        int mid = size/2;
        g.setColor(colorsets[transparency][0]);
        g.fillRect(0, 0, mid, mid);
        g.setColor(colorsets[transparency][1]);
        g.fillRect(mid, 0, size-mid, mid);
        g.setColor(colorsets[transparency][2]);
        g.fillRect(0, mid, mid, size-mid);
        g.setColor(colorsets[transparency][3]);
        g.fillRect(mid, mid, size-mid, size-mid);
    }
    g.dispose();
    env.setSrcImage(src);
}
 
Example 25
Project: freecol   File: DeclarationPanel.java   Source Code and License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void paintComponent(Graphics g) {
    if (points == null || points.length == 0) {
        return;
    }
    if (isOpaque()) {
        super.paintComponent(g);
    }

    g.setColor(Color.BLACK);
    ((Graphics2D)g).setComposite(AlphaComposite
        .getInstance(AlphaComposite.SRC_OVER, 0.75f));

    for (int i = 0; i < counter-1; i++) {
        Point p1 = points[i];
        Point p2 = points[i+1];
        g.drawLine((int) p1.getX(), (int) p1.getY(),
            (int) p2.getX(), (int) p2.getY());
    }
}
 
Example 26
Project: jdk8u-jdk   File: DrawImage.java   Source Code and License 6 votes vote down vote up
/**
 * Return a non-accelerated BufferedImage of the requested type with the
 * indicated subimage of the original image located at 0,0 in the new image.
 * If a bgColor is supplied, composite the original image over that color
 * with a SrcOver operation, otherwise make a SrcNoEa copy.
 * <p>
 * Returned BufferedImage is not accelerated for two reasons:
 * <ul>
 * <li> Types of the image and surface are predefined, because these types
 *      correspond to the TransformHelpers, which we know we have. And
 *      acceleration can change the type of the surface
 * <li> Image will be used only once and acceleration caching wouldn't help
 * </ul>
 */
BufferedImage makeBufferedImage(Image img, Color bgColor, int type,
                                int sx1, int sy1, int sx2, int sy2)
{
    final int width = sx2 - sx1;
    final int height = sy2 - sy1;
    final BufferedImage bimg = new BufferedImage(width, height, type);
    final SunGraphics2D g2d = (SunGraphics2D) bimg.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    bimg.setAccelerationPriority(0);
    if (bgColor != null) {
        g2d.setColor(bgColor);
        g2d.fillRect(0, 0, width, height);
        g2d.setComposite(AlphaComposite.SrcOver);
    }
    g2d.copyImage(img, 0, 0, sx1, sy1, width, height, null, null);
    g2d.dispose();
    return bimg;
}
 
Example 27
Project: brModelo   File: Desenhador.java   Source Code and License 6 votes vote down vote up
public void DrawImagem(Graphics2D g) {
    BufferedImage imgB = getImagem();
    if (imgB == null) {
        return;
    }
    Rectangle rec = getBounds();
    rec.grow(-2, -2);
    if (imgres == null) {
        imgres = imgB.getScaledInstance(rec.width, rec.height, Image.SCALE_SMOOTH);
    }

    Composite originalComposite = g.getComposite();
    if (alfa != 1f) {
        int type = AlphaComposite.SRC_OVER;
        g.setComposite(AlphaComposite.getInstance(type, alfa));
    }
    Image img = imgres;
    if (isDisablePainted()) {
        img = util.Utilidades.dye(new ImageIcon(imgres), disabledColor);
    }
    
    g.drawImage(img, rec.x, rec.y, null);
    g.setComposite(originalComposite);
}
 
Example 28
Project: OpenJSharp   File: ImageTests.java   Source Code and License 6 votes vote down vote up
public void modifyTest(TestEnvironment env) {
    int size = env.getIntValue(sizeList);
    Image src = tsit.getImage(env, size, size);
    Graphics g = src.getGraphics();
    if (hasGraphics2D) {
        ((Graphics2D) g).setComposite(AlphaComposite.Src);
    }
    if (size == 1) {
        g.setColor(colorsets[transparency][4]);
        g.fillRect(0, 0, 1, 1);
    } else {
        int mid = size/2;
        g.setColor(colorsets[transparency][0]);
        g.fillRect(0, 0, mid, mid);
        g.setColor(colorsets[transparency][1]);
        g.fillRect(mid, 0, size-mid, mid);
        g.setColor(colorsets[transparency][2]);
        g.fillRect(0, mid, mid, size-mid);
        g.setColor(colorsets[transparency][3]);
        g.fillRect(mid, mid, size-mid, size-mid);
    }
    g.dispose();
    env.setSrcImage(src);
}
 
Example 29
Project: openvisualtraceroute   File: GlassPane.java   Source Code and License 5 votes vote down vote up
/**
 * @see javax.swing.JComponent#paint(java.awt.Graphics)
 */
@Override
public void paint(final Graphics g) {
	// transparent panel
	if (g instanceof Graphics2D) {
		((Graphics2D) g).setComposite(AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.7f));
	}
	super.paint(g);
}
 
Example 30
Project: jdk8u-jdk   File: OpaqueImageToSurfaceBlitTest.java   Source Code and License 5 votes vote down vote up
public static void main(String[] args) {

        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
        vi.validate(gc);

        BufferedImage bi =
            new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
        int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
        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.");
    }
 
Example 31
Project: litiengine   File: OrthogonalMapRenderer.java   Source Code and License 5 votes vote down vote up
/**
 * Gets the layer image.
 *
 * @param layer
 *          the layer
 * @param map
 *          the map
 * @return the layer image
 */
private synchronized BufferedImage getLayerImage(final ITileLayer layer, final IMap map, boolean includeAnimationTiles) {
  // if we have already retrived the image, use the one from the cache to
  // draw the layer
  final String cacheKey = MessageFormat.format("{0}_{1}", getCacheKey(map), layer.getName());
  if (ImageCache.MAPS.containsKey(cacheKey)) {
    return ImageCache.MAPS.get(cacheKey);
  }
  final BufferedImage bufferedImage = ImageProcessing.getCompatibleImage(layer.getSizeInTiles().width * map.getTileSize().width, layer.getSizeInTiles().height * map.getTileSize().height);

  // we need a graphics 2D object to work with transparency
  final Graphics2D imageGraphics = bufferedImage.createGraphics();

  // set alpha value of the tiles by the layers value
  final AlphaComposite ac = java.awt.AlphaComposite.getInstance(AlphaComposite.SRC_OVER, layer.getOpacity());
  imageGraphics.setComposite(ac);

  layer.getTiles().parallelStream().forEach(tile -> {
    // get the tile from the tileset image
    final int index = layer.getTiles().indexOf(tile);
    if (tile.getGridId() == 0) {
      return;
    }

    if (!includeAnimationTiles && MapUtilities.hasAnimation(map, tile)) {
      return;
    }

    final Image tileTexture = getTileImage(map, tile);

    // draw the tile on the layer image
    final int x = index % layer.getSizeInTiles().width * map.getTileSize().width;
    final int y = index / layer.getSizeInTiles().width * map.getTileSize().height;
    RenderEngine.renderImage(imageGraphics, tileTexture, x, y);
  });

  ImageCache.MAPS.put(cacheKey, bufferedImage);
  return bufferedImage;
}
 
Example 32
Project: openjdk-jdk10   File: EffectUtils.java   Source Code and License 5 votes vote down vote up
/**
 * Clear a transparent image to 100% transparent
 *
 * @param img The image to clear
 */
static void clearImage(BufferedImage img) {
    Graphics2D g2 = img.createGraphics();
    g2.setComposite(AlphaComposite.Clear);
    g2.fillRect(0, 0, img.getWidth(), img.getHeight());
    g2.dispose();
}
 
Example 33
Project: openjdk-jdk10   File: CompositeType.java   Source Code and License 5 votes vote down vote up
/**
 * Return a CompositeType object for the specified AlphaComposite
 * rule.
 */
public static CompositeType forAlphaComposite(AlphaComposite ac) {
    switch (ac.getRule()) {
    case AlphaComposite.CLEAR:
        return Clear;
    case AlphaComposite.SRC:
        if (ac.getAlpha() >= 1.0f) {
            return SrcNoEa;
        } else {
            return Src;
        }
    case AlphaComposite.DST:
        return Dst;
    case AlphaComposite.SRC_OVER:
        if (ac.getAlpha() >= 1.0f) {
            return SrcOverNoEa;
        } else {
            return SrcOver;
        }
    case AlphaComposite.DST_OVER:
        return DstOver;
    case AlphaComposite.SRC_IN:
        return SrcIn;
    case AlphaComposite.DST_IN:
        return DstIn;
    case AlphaComposite.SRC_OUT:
        return SrcOut;
    case AlphaComposite.DST_OUT:
        return DstOut;
    case AlphaComposite.SRC_ATOP:
        return SrcAtop;
    case AlphaComposite.DST_ATOP:
        return DstAtop;
    case AlphaComposite.XOR:
        return AlphaXor;
    default:
        throw new InternalError("Unrecognized alpha rule");
    }
}
 
Example 34
Project: OpenJSharp   File: GraphicsPrimitive.java   Source Code and License 5 votes vote down vote up
protected static void convertTo(Blit ob,
                                SurfaceData srcImg, SurfaceData dstImg,
                                Region clip,
                                int dstX, int dstY, int w, int h)
{
    if (ob != null) {
        ob.Blit(srcImg, dstImg, AlphaComposite.Src, clip,
                0, 0, dstX, dstY, w, h);
    }
}
 
Example 35
Project: parabuild-ci   File: Plot.java   Source Code and License 5 votes vote down vote up
/**
 * Fills the specified area with the background paint.
 * 
 * @param g2  the graphics device.
 * @param area  the area.
 */
protected void fillBackground(Graphics2D g2, Rectangle2D area) {
    if (this.backgroundPaint != null) {
        Composite originalComposite = g2.getComposite();
        g2.setComposite(
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.backgroundAlpha)
        );
        g2.setPaint(this.backgroundPaint);
        g2.fill(area);
        g2.setComposite(originalComposite);
    }        
}
 
Example 36
Project: incubator-netbeans   File: AquaSlidingButtonUI.java   Source Code and License 5 votes vote down vote up
@Override
protected void paintIcon(Graphics g, AbstractButton b, Rectangle iconRect) {
    Graphics2D g2d = (Graphics2D) g;

    Composite comp = g2d.getComposite();
    if( b.getModel().isRollover() || b.getModel().isSelected() ) {
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    }
    super.paintIcon(g, b, iconRect);
    g2d.setComposite(comp);
}
 
Example 37
Project: openjdk-jdk10   File: JPEGsNotAcceleratedTest.java   Source Code and License 5 votes vote down vote up
public BufferedImage getDestImage() {
    BufferedImage destBI =
        new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D)destBI.getGraphics();
    g.setComposite(AlphaComposite.Src);
    g.setColor(Color.blue);
    g.fillRect(0, 0, TEST_W, TEST_H);
    return destBI;
}
 
Example 38
Project: OpenJSharp   File: SunCompositeContext.java   Source Code and License 5 votes vote down vote up
public SunCompositeContext(AlphaComposite ac,
                           ColorModel s, ColorModel d)
{
    if (s == null) {
        throw new NullPointerException("Source color model cannot be null");
    }
    if (d == null) {
        throw new NullPointerException("Destination color model cannot be null");
    }
    srcCM = s;
    dstCM = d;
    this.composite = ac;
    this.comptype = CompositeType.forAlphaComposite(ac);
}
 
Example 39
Project: incubator-netbeans   File: DnDSupport.java   Source Code and License 5 votes vote down vote up
private BufferedImage createContentImage( JComponent c, Dimension contentSize ) {
    GraphicsConfiguration cfg = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getDefaultScreenDevice().getDefaultConfiguration();

    boolean opaque = c.isOpaque();
    c.setOpaque(true);
    BufferedImage res = cfg.createCompatibleImage(contentSize.width, contentSize.height);
    Graphics2D g = res.createGraphics();
    g.setColor( c.getBackground() );
    g.fillRect(0, 0, contentSize.width, contentSize.height);
    g.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f ));
    c.paint(g);
    c.setOpaque(opaque);
    return res;
}
 
Example 40
Project: incubator-netbeans   File: ScaleFx.java   Source Code and License 5 votes vote down vote up
public void paint(Graphics g) {
    Rectangle bounds = getBounds();
    
    if (origImage == null) {
        if (comp == null) {
            return;
        }
        origImage = tryCreateImage();
        if (origImage == null) {
            return;
        }
    }
    Image img = origImage;
    Graphics2D g2d = (Graphics2D) g;
    Composite origComposite = g2d.getComposite();
    g2d.setComposite (AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    /*AffineTransform at = AffineTransform.getScaleInstance(
        (double)bounds.width / (double)scaleSource.width,
        (double)bounds.height / (double)scaleSource.height);
    g2d.setTransform(at);*/
    g2d.drawImage(img, 0, 0, bounds.width, bounds.height, null);
    //SwingUtilities.paintComponent(g, getComponent(0), this, 0, 0, bounds.width, bounds.height);
    //super.paint(g2d);
    
    if (origComposite != null) {
        g2d.setComposite(origComposite);
    }
}
 
Example 41
Project: OpenJSharp   File: BlitBg.java   Source Code and License 5 votes vote down vote up
@Override
public void BlitBg(SurfaceData srcData,
                   SurfaceData dstData,
                   Composite comp,
                   Region clip,
                   int bgArgb,
                   int srcx, int srcy,
                   int dstx, int dsty,
                   int width, int height)
{
    ColorModel dstModel = dstData.getColorModel();
    boolean bgHasAlpha = (bgArgb >>> 24) != 0xff;
    if (!dstModel.hasAlpha() && bgHasAlpha) {
        dstModel = ColorModel.getRGBdefault();
    }
    WritableRaster wr =
        dstModel.createCompatibleWritableRaster(width, height);
    boolean isPremult = dstModel.isAlphaPremultiplied();
    BufferedImage bimg =
        new BufferedImage(dstModel, wr, isPremult, null);
    SurfaceData tmpData = BufImgSurfaceData.createData(bimg);
    Color bgColor = new Color(bgArgb, bgHasAlpha);
    SunGraphics2D sg2d = new SunGraphics2D(tmpData, bgColor, bgColor,
                                           defaultFont);
    FillRect fillop = FillRect.locate(SurfaceType.AnyColor,
                                      CompositeType.SrcNoEa,
                                      tmpData.getSurfaceType());
    Blit combineop = Blit.getFromCache(srcData.getSurfaceType(),
                                       CompositeType.SrcOverNoEa,
                                       tmpData.getSurfaceType());
    Blit blitop = Blit.getFromCache(tmpData.getSurfaceType(), compositeType,
                                    dstData.getSurfaceType());
    fillop.FillRect(sg2d, tmpData, 0, 0, width, height);
    combineop.Blit(srcData, tmpData, AlphaComposite.SrcOver, null,
                   srcx, srcy, 0, 0, width, height);
    blitop.Blit(tmpData, dstData, comp, clip,
                0, 0, dstx, dsty, width, height);
}
 
Example 42
Project: brModelo   File: FormaArea.java   Source Code and License 5 votes vote down vote up
protected void PaintGradiente(Graphics2D g) { //, boolean round) {
        Paint bkp = g.getPaint();
        int dist = distSelecao;

        int W = getWidth() - dist;
        int H = 2 * alturaTexto - 1 - dist;
        boolean dv = getGDirecao() == VERTICAL;

        Composite originalComposite = g.getComposite();
        int type = AlphaComposite.SRC_OVER;

        g.setComposite(AlphaComposite.getInstance(type, alfa));

        Stroke bkps = g.getStroke();
        if (isDashed()) {
            g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{1, 2}, 0));
        }

//        GradientPaint GP = new GradientPaint(getLeft(), getTop(), Color.LIGHT_GRAY, dv ? getLeft() : getLeft() + W, dv ? getTop() + H : getTop(), Color.white, true);
//        g.setPaint(GP);
        int w = getWidth() - dist;
        int h = getHeight() - dist;
        int L = getLeft();
        int T = getTop();

        GradientPaint GP = new GradientPaint(L, T, getGradienteStartColor(), dv ? L : L + w, dv ? T + h : T, getGradienteEndColor(), true);
        g.setPaint(GP);

        g.fillRect(getLeft() + 1, getTop() + 1, W, H);
        g.setComposite(originalComposite);
        g.setPaint(bkp);
        g.setStroke(bkps);
    }
 
Example 43
Project: rapidminer   File: SOMModelPlotter.java   Source Code and License 5 votes vote down vote up
@Override
public void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	// painting only if approved
	if (show) {
		// init graphics
		Graphics2D g = (Graphics2D) graphics;

		int pixWidth = getWidth() - 2 * MARGIN;
		int pixHeight = getHeight() - 2 * MARGIN;

		// painting background
		g.drawImage(this.image, MARGIN, MARGIN, pixWidth, pixHeight, Color.WHITE, null);

		// painting transparent class overlay
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaLevel));
		g.drawImage(this.classImage, MARGIN, MARGIN, pixWidth, pixHeight, Color.WHITE, null);
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
		// painting points
		drawPoints(g);

		// painting Legend
		drawLegend(graphics, this.dataTable, colorColumn);

		// paint Tooltip
		drawToolTip((Graphics2D) graphics);
	}
}
 
Example 44
Project: openjdk-jdk10   File: PngPremultAlphaTest.java   Source Code and License 5 votes vote down vote up
public static void doTest(int type) {
    try {
        BufferedImage src = new BufferedImage(100, 100,
                                              type);
        Graphics2D g = src.createGraphics();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f));
        g.setColor(new Color(0x20, 0x40, 0x60));
        g.fillRect(0,0,100,100);

        int[] samples = new int[src.getData().getNumBands()];
        src.getData().getPixels(0,0,1,1,samples);
        for(int i=0; i<samples.length; i++) {
            System.out.println("sample["+i+"]="+Integer.toHexString(samples[i]));
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(src, format, baos);
        baos.close();


        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        BufferedImage dst = ImageIO.read(bais);

        isSameColors(src, dst);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed.");
    }
}
 
Example 45
Project: Progetto-C   File: EffectUtil.java   Source Code and License 5 votes vote down vote up
/**
 * Returns an image that can be used by effects as a temp image.
 * 
 * @return The scratch image used for temporary operations
 */
static public BufferedImage getScratchImage() {
	Graphics2D g = (Graphics2D)scratchImage.getGraphics();
	g.setComposite(AlphaComposite.Clear);
	g.fillRect(0, 0, GlyphPage.MAX_GLYPH_SIZE, GlyphPage.MAX_GLYPH_SIZE);
	g.setComposite(AlphaComposite.SrcOver);
	g.setColor(java.awt.Color.white);
	return scratchImage;
}
 
Example 46
Project: GlitchKernel   File: DataAsSound.java   Source Code and License 5 votes vote down vote up
@Override
public byte[] glitchPixels(byte[] inputImageBytes) throws Exception 
{
	int audioBitRate = ((Integer) getPixelGlitchParameters().get("bitRateBlend")).intValue();
	float bitRateBlend = (float) audioBitRate / 10;
	if(bitRateBlend < 0.1F || bitRateBlend > 0.9F)
	{
		return null;
	}
	
	BufferedImage inputImage = ImageUtil.getImageFromBytes(inputImageBytes);
	InputStream imageInputStream = new ByteArrayInputStream(inputImageBytes);
	AudioInputStream distortionAudioStream = new AudioInputStream(imageInputStream, new AudioFormat(AudioFormat.Encoding.ULAW, ThreadLocalRandom.current().nextInt(8000,  20000), 8, 5, 9, ThreadLocalRandom.current().nextInt(8000,  20000), true), inputImageBytes.length);
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	AudioSystem.write(distortionAudioStream, Type.WAVE, outputStream);
	BufferedImage outputImage = new BufferedImage(inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
	byte[] imageData = ((DataBufferByte) outputImage.getRaster().getDataBuffer()).getData();
	System.arraycopy(outputStream.toByteArray(),0,imageData,0,outputStream.toByteArray().length);
	int[] abgrOffsets = {3, 2, 1, 0}; 
	DataBuffer outputBuffer = new DataBufferByte(imageData, imageData.length);
    WritableRaster raster = Raster.createInterleavedRaster(outputBuffer, inputImage.getWidth(), inputImage.getHeight(), 4 * inputImage.getWidth(), 4, abgrOffsets, null);
    ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
    BufferedImage rasterizedImage = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    rasterizedImage = resizeImage(rasterizedImage, inputImage.getWidth() * 4, inputImage.getHeight() * 4);
    Graphics2D g2d = rasterizedImage.createGraphics();
    g2d.setComposite(AlphaComposite.SrcOver.derive(bitRateBlend));
    g2d.drawImage(inputImage, 0, 0, null);
    g2d.dispose();
    rasterizedImage = rasterizedImage.getSubimage(0, 0, inputImage.getWidth(), inputImage.getHeight());
	return ImageUtil.getImageBytes(rasterizedImage);
}
 
Example 47
Project: Tarski   File: mxUtils.java   Source Code and License 5 votes vote down vote up
/**
 * Clears the given area of the specified graphics object with the given color or makes the region
 * transparent.
 */
public static void clearRect(Graphics2D g, Rectangle rect, Color background) {
  if (background != null) {
    g.setColor(background);
    g.fillRect(rect.x, rect.y, rect.width, rect.height);
  } else {
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
    g.fillRect(rect.x, rect.y, rect.width, rect.height);
    g.setComposite(AlphaComposite.SrcOver);
  }
}
 
Example 48
Project: VASSAL-src   File: SetupStack.java   Source Code and License 5 votes vote down vote up
private BufferedImage featherDragImage(BufferedImage src,
                                         int w, int h, int b) {
// FIXME: duplicated from PieceMover!
      final BufferedImage dst =
        ImageUtils.createCompatibleTranslucentImage(w, h);

      final Graphics2D g = dst.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);

      // paint the rectangle occupied by the piece at specified alpha
      g.setColor(new Color(0xff, 0xff, 0xff, CURSOR_ALPHA));
      g.fillRect(0, 0, w, h);

      // feather outwards
      for (int f = 0; f < b; ++f) {
        final int alpha = CURSOR_ALPHA * (f + 1) / b;
        g.setColor(new Color(0xff, 0xff, 0xff, alpha));
        g.drawRect(f, f, w-2*f, h-2*f);
      }

      // paint in the source image
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
      g.drawImage(src, 0, 0, null);
      g.dispose();

      return dst;
    }
 
Example 49
Project: FreeCol   File: FreeColButtonUI.java   Source Code and License 5 votes vote down vote up
@Override
protected void paintButtonPressed(Graphics g, AbstractButton c) {
    if (c.isContentAreaFilled()) {
        Graphics2D g2d = (Graphics2D) g;
        Dimension size = c.getSize();
        Composite oldComposite = g2d.getComposite();
        Color oldColor = g2d.getColor();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, size.width, size.height);
        g2d.setComposite(oldComposite);
        g2d.setColor(oldColor);

    }
}
 
Example 50
Project: OpenJSharp   File: TranslucentWindowPainter.java   Source Code and License 5 votes vote down vote up
private static final Image clearImage(Image bb) {
    Graphics2D g = (Graphics2D)bb.getGraphics();
    int w = bb.getWidth(null);
    int h = bb.getHeight(null);

    g.setComposite(AlphaComposite.Src);
    g.setColor(new Color(0, 0, 0, 0));
    g.fillRect(0, 0, w, h);

    return bb;
}
 
Example 51
Project: VASSAL-src   File: PieceMover.java   Source Code and License 5 votes vote down vote up
private BufferedImage featherDragImage(BufferedImage src,
                                           int w, int h, int b) {
// FIXME: This should be redone so that we draw the feathering onto the
// destination first, and then pass the Graphics2D on to draw the pieces
// directly over it. Presently this doesn't work because some of the
// pieces screw up the Graphics2D when passed it... The advantage to doing
// it this way is that we create only one BufferedImage instead of two.
      final BufferedImage dst =
        ImageUtils.createCompatibleTranslucentImage(w, h);

      final Graphics2D g = dst.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);

      // paint the rectangle occupied by the piece at specified alpha
      g.setColor(new Color(0xff, 0xff, 0xff, CURSOR_ALPHA));
      g.fillRect(0, 0, w, h);

      // feather outwards
      for (int f = 0; f < b; ++f) {
        final int alpha = CURSOR_ALPHA * (f + 1) / b;
        g.setColor(new Color(0xff, 0xff, 0xff, alpha));
        g.drawRect(f, f, w-2*f, h-2*f);
      }

      // paint in the source image
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN));
      g.drawImage(src, 0, 0, null);
      g.dispose();

      return dst;
    }
 
Example 52
Project: TrabalhoFinalEDA2   File: mxConnectPreview.java   Source Code and License 5 votes vote down vote up
/**
 * 
 */
public void paint(Graphics g)
{
	if (previewState != null)
	{
		mxGraphics2DCanvas canvas = graphComponent.getCanvas();

		if (graphComponent.isAntiAlias())
		{
			mxUtils.setAntiAlias((Graphics2D) g, true, false);
		}

		float alpha = graphComponent.getPreviewAlpha();

		if (alpha < 1)
		{
			((Graphics2D) g).setComposite(AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, alpha));
		}

		Graphics2D previousGraphics = canvas.getGraphics();
		mxPoint previousTranslate = canvas.getTranslate();
		double previousScale = canvas.getScale();

		try
		{
			canvas.setScale(graphComponent.getGraph().getView().getScale());
			canvas.setTranslate(0, 0);
			canvas.setGraphics((Graphics2D) g);

			paintPreview(canvas);
		}
		finally
		{
			canvas.setScale(previousScale);
			canvas.setTranslate(previousTranslate.getX(), previousTranslate.getY());
			canvas.setGraphics(previousGraphics);
		}
	}
}
 
Example 53
Project: jdk8u-jdk   File: IncorrectUnmanagedImageSourceOffset.java   Source Code and License 5 votes vote down vote up
private static void fill(final Image image) {
    final Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setComposite(AlphaComposite.Src);
    for (int i = 0; i < image.getHeight(null); ++i) {
        graphics.setColor(new Color(i, 0, 0));
        graphics.fillRect(0, i, image.getWidth(null), 1);
    }
    graphics.dispose();
}
 
Example 54
Project: openjdk-jdk10   File: AlphaTest.java   Source Code and License 5 votes vote down vote up
private static BufferedImage createDst() {
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = img.createGraphics();
    g.setComposite(AlphaComposite.Clear);
    g.fillRect(0, 0, w, h);
    g.dispose();

    return img;
}
 
Example 55
Project: Progetto-B   File: FadeOutLabel.java   Source Code and License 5 votes vote down vote up
/**
 * Paints the label with the current alpha coefficient.
 *
 * @param g
 */
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setComposite(AlphaComposite.SrcOver.derive(1f - alpha));
    int x = (getWidth() - outImage.getWidth()) / 2;
    int y = (getHeight() - outImage.getHeight()) / 2;
    g2d.drawImage(outImage, x, y, this);
    g2d.dispose();
}
 
Example 56
Project: jdk8u-jdk   File: IncorrectAlphaSurface2SW.java   Source Code and License 5 votes vote down vote up
/**
 * Fills the whole image using different alpha for each row.
 *
 * @param image to fill
 */
private static void fill(final Image image, final int size) {
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setComposite(AlphaComposite.Src);
    graphics.setColor(Color.GREEN);
    graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    int row = image.getHeight(null) / size;
    for (int i = 0; i < size; ++i) {
        graphics.setColor(new Color(23, 127, 189, i));
        graphics.fillRect(0, i * row, image.getWidth(null), row);
    }
    graphics.dispose();
}
 
Example 57
Project: jdk8u-jdk   File: IncorrectUnmanagedImageRotatedClip.java   Source Code and License 5 votes vote down vote up
private static void draw(final BufferedImage from,final Image to) {
    final Graphics2D g2d = (Graphics2D) to.getGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.setColor(Color.ORANGE);
    g2d.fillRect(0, 0, to.getWidth(null), to.getHeight(null));
    g2d.rotate(Math.toRadians(45));
    g2d.clip(new Rectangle(41, 42, 43, 44));
    g2d.drawImage(from, 50, 50, Color.blue, null);
    g2d.dispose();
}
 
Example 58
Project: FreeCol   File: FreeColComboBoxRenderer.java   Source Code and License 5 votes vote down vote up
@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Composite oldComposite = g2d.getComposite();
    Color oldColor = g2d.getColor();
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f));
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    g2d.setComposite(oldComposite);
    g2d.setColor(oldColor);

    super.paintComponent(g);
}
 
Example 59
Project: parabuild-ci   File: Plot.java   Source Code and License 5 votes vote down vote up
/**
 * Fills the specified area with the background paint.
 * 
 * @param g2  the graphics device.
 * @param area  the area.
 * 
 * @see #getBackgroundPaint()
 * @see #getBackgroundAlpha()
 */
protected void fillBackground(Graphics2D g2, Rectangle2D area) {
    if (this.backgroundPaint != null) {
        Composite originalComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                this.backgroundAlpha));
        g2.setPaint(this.backgroundPaint);
        g2.fill(area);
        g2.setComposite(originalComposite);
    }
}
 
Example 60
Project: FreeCol   File: TileViewer.java   Source Code and License 5 votes vote down vote up
/**
 * Displays Fog of War of the tile onto the given Graphics2D object,
 * if necessary.
 *
 * @param g The {@code Graphics2D} object on which to draw
 *     the {@code Tile}.
 * @param tile The {@code Tile} to draw.
 */
void displayFogOfWar(Graphics2D g, Tile tile) {
    if (hasFogOfWar(tile)) {
        g.setColor(Color.BLACK);
        Composite oldComposite = g.getComposite();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                  0.2f));
        g.fill(fog);
        g.setComposite(oldComposite);
    }
}