Java Code Examples for java.awt.Image#getGraphics()

The following examples show how to use java.awt.Image#getGraphics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IncorrectClipXorModeSW2Surface.java    From openjdk-jdk9 with GNU General Public License v2.0 7 votes vote down vote up
private static void draw(Shape clip, Shape shape, Image from, Image to) {
    Graphics2D g2d = (Graphics2D) to.getGraphics();
    g2d.setXORMode(Color.BLACK);
    g2d.setClip(clip);
    Rectangle toBounds = shape.getBounds();
    g2d.drawImage(from, toBounds.x, toBounds.y, toBounds.width,
                  toBounds.height, null);
    g2d.dispose();
}
 
Example 2
Source File: UnmanagedDrawImagePerformance.java    From jdk8u-jdk with GNU General Public License v2.0 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 3
Source File: SourceClippingBlitTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void initImage(GraphicsConfiguration gc, Image image) {
    Graphics g = image.getGraphics();
    g.setColor(Color.RED);
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    g.fillRect(0, 0, w, h);
    g.dispose();

    // need to 'accelerate' the image
    if (dstImage == null) {
        dstImage =
            gc.createCompatibleVolatileImage(TESTW, TESTH,
                                             Transparency.OPAQUE);
    }
    dstImage.validate(gc);
    g = dstImage.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.drawImage(image, 0, 0, null);
    g.drawImage(image, 0, 0, null);
}
 
Example 4
Source File: UnmanagedDrawImagePerformance.java    From jdk8u-dev-jdk with GNU General Public License v2.0 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 5
Source File: TranslucentWindowPainter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Updates the window associated with the painter.
 *
 * @param repaint indicates if the window should be completely repainted
 * to the back buffer using {@link java.awt.Window#paintAll} before update.
 */
public void updateWindow(boolean repaint) {
    boolean done = false;
    Image bb = getBackBuffer(repaint);
    while (!done) {
        if (repaint) {
            Graphics2D g = (Graphics2D)bb.getGraphics();
            try {
                window.paintAll(g);
            } finally {
                g.dispose();
            }
        }

        done = update(bb);
        if (!done) {
            repaint = true;
            bb = getBackBuffer(true);
        }
    }
}
 
Example 6
Source File: MechSlotLabel.java    From megamek with GNU General Public License v2.0 6 votes vote down vote up
private void drawBGImage() {
    Dimension d = getSize();
    int w = d.width;
    int h = d.height;
    Image BGImage = createImage(w, h);
    if (BGImage == null)
        return;
    Graphics g = BGImage.getGraphics();
    g.setColor(Color.green.darker().darker());
    g.fillRect(0, 0, w, h);
    g.setColor(Color.green.darker());
    g.fillRect(w - 2, 0, 2, h);
    g.fillRect(0, h - 2, w, 2);
    g.setColor(Color.green.darker().darker().darker());
    g.fillRect(0, 0, w, 2);
    g.fillRect(0, 0, 2, h);
    g.dispose();
    bgd.setImage(BGImage);
}
 
Example 7
Source File: TranslucentWindowPainter.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Updates the window associated with the painter.
 *
 * @param repaint indicates if the window should be completely repainted
 * to the back buffer using {@link java.awt.Window#paintAll} before update.
 */
public void updateWindow(boolean repaint) {
    boolean done = false;
    Image bb = getBackBuffer(repaint);
    while (!done) {
        if (repaint) {
            Graphics2D g = (Graphics2D)bb.getGraphics();
            try {
                window.paintAll(g);
            } finally {
                g.dispose();
            }
        }

        done = update(bb);
        if (!done) {
            repaint = true;
            bb = getBackBuffer(true);
        }
    }
}
 
Example 8
Source File: SourceClippingBlitTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static void initImage(GraphicsConfiguration gc, Image image) {
    Graphics g = image.getGraphics();
    g.setColor(Color.RED);
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    g.fillRect(0, 0, w, h);
    g.dispose();

    // need to 'accelerate' the image
    if (dstImage == null) {
        dstImage =
            gc.createCompatibleVolatileImage(TESTW, TESTH,
                                             Transparency.OPAQUE);
    }
    dstImage.validate(gc);
    g = dstImage.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.drawImage(image, 0, 0, null);
    g.drawImage(image, 0, 0, null);
}
 
Example 9
Source File: JRViewer.java    From nordpos with GNU General Public License v3.0 6 votes vote down vote up
protected Image getPageErrorImage()
{
	PrintPageFormat pageFormat = getPageFormat();
	Image image = new BufferedImage(
			(int) (pageFormat.getPageWidth() * realZoom) + 1,
			(int) (pageFormat.getPageHeight() * realZoom) + 1,
			BufferedImage.TYPE_INT_RGB
			);
	
	Graphics2D grx = (Graphics2D) image.getGraphics();
	AffineTransform transform = new AffineTransform();
	transform.scale(realZoom, realZoom);
	grx.transform(transform);

	drawPageError(grx);
	
	return image;
}
 
Example 10
Source File: TranslucentWindowPainter.java    From openjdk-jdk8u with GNU General Public License v2.0 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 11
Source File: SimpleUnmanagedImage.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void init(final Image image) {
    final Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setComposite(AlphaComposite.Src);
    graphics.setColor(new Color(0, 0, 0, 0));
    graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
    graphics.dispose();
}
 
Example 12
Source File: TranslucentWindowPainter.java    From jdk8u60 with GNU General Public License v2.0 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 13
Source File: IncorrectAlphaSurface2SW.java    From dragonwell8_jdk with GNU General Public License v2.0 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 14
Source File: IncorrectUnmanagedImageRotatedClip.java    From jdk8u-jdk with GNU General Public License v2.0 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 15
Source File: IncorrectUnmanagedImageSourceOffset.java    From openjdk-jdk8u with GNU General Public License v2.0 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 16
Source File: BattleArmorMapSet.java    From megamek with GNU General Public License v2.0 5 votes vote down vote up
private void drawArmorImage(Image im, int a) {
    int x = 0;
    int w = im.getWidth(null);
    int h = im.getHeight(null);
    Graphics g = im.getGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, w, h);
    for (int i = 0; i < a; i++) {
        x = i * 7;
        g.setColor(Color.green.darker());
        g.fillRect(x, 0, 5, 12);
    }
}
 
Example 17
Source File: DashZeroWidth.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void draw(final Image img) {
    float[] dashes = {10.0f, 10.0f};
    BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,
                                     BasicStroke.JOIN_BEVEL, 10.0f, dashes,
                                     0.0f);
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 200, 40);
    Line2D line = new Line2D.Double(20, 20, 180, 20);
    g.setColor(Color.BLACK);
    g.setStroke(bs);
    g.draw(line);
    g.dispose();
}
 
Example 18
Source File: MainDesktopPane.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create background tile when MainDesktopPane is first displayed. Recenter
 * logoLabel on MainWindow and set backgroundLabel to the size of
 * MainDesktopPane.
 * 
 * @param e the component event
 */
@Override
public void componentResized(ComponentEvent e) {
	// If displayed for the first time, create background image tile.
	// The size of the background tile cannot be determined during construction
	// since it requires the MainDesktopPane be displayed first.
	if (firstDisplay) {
		ImageIcon baseImageIcon = ImageLoader.getIcon(Msg.getString("img.background")); //$NON-NLS-1$
		Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
		Image backgroundImage = createImage((int) screen_size.getWidth(), (int) screen_size.getHeight());
		Graphics backgroundGraphics = backgroundImage.getGraphics();

		for (int x = 0; x < backgroundImage.getWidth(this); x += baseImageIcon.getIconWidth()) {
			for (int y = 0; y < backgroundImage.getHeight(this); y += baseImageIcon.getIconHeight()) {
				backgroundGraphics.drawImage(baseImageIcon.getImage(), x, y, this);
			}
		}

		backgroundImageIcon.setImage(backgroundImage);

		backgroundLabel.setSize(getSize());

		firstDisplay = false;
	}

	// Set the backgroundLabel size to the size of the desktop
	backgroundLabel.setSize(getSize());

}
 
Example 19
Source File: IncorrectManagedImageSourceOffset.java    From dragonwell8_jdk with GNU General Public License v2.0 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 20
Source File: NonOpaqueDestLCDAATest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void render(Image im, int type, String s) {
    Graphics2D g2d = (Graphics2D) im.getGraphics();
    clear(g2d, type, im.getWidth(null), im.getHeight(null));
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    Font f = new Font("Dialog", Font.BOLD, 40);// g2d.getFont().deriveFont(32.0f);
    g2d.setColor(Color.white);
    g2d.setFont(g2d.getFont().deriveFont(36.0f));
    g2d.drawString(s, 10, im.getHeight(null) / 2);
}