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

The following are Jave code examples for showing how to use getGraphics() of the java.awt.Image class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: ScaleFx.java   View Source Code Vote up 7 votes
private Image createCompImage(Component comp, Dimension targetSize) {
    // component won't paint if not showing anyway, so don't create
    // empty image but honestly return null 
    if (!comp.isShowing()) {
        return null;
    }
    
    Image image = comp.createImage(comp.getWidth(), comp.getHeight());
    
    /*BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().
            getDefaultScreenDevice().getDefaultConfiguration().
            createCompatibleImage(comp.getWidth(), comp.getHeight());*/
    //BufferedImage image = new BufferedImage (targetSize.width, targetSize.height, BufferedImage.TYPE_INT_RGB);
    
    Graphics2D gr2d = (Graphics2D)image.getGraphics();
    
    comp.paint(gr2d);
    
    gr2d.dispose();
    
    return image;
}
 
Example 2
Project: incubator-netbeans   File: ViewTooltips.java   View Source Code Vote up 6 votes
/**
 * Set the cell renderer we will proxy.
 */
public void setComponent (Component jc, JComponent owner) {
    Dimension dd = jc.getPreferredSize();
    Rectangle currentScreenBounds = Utilities.getUsableScreenBounds();
    // get some reasonable limit for the width
    int width = Math.min(dd.width, 2 * currentScreenBounds.width);
    int height = Math.min(dd.height + 2, 2 * currentScreenBounds.height);
    Image nue = !Utilities.isMac() ? owner.createVolatileImage(width, height) :
                new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics g = nue.getGraphics();
    g.setColor (bg);
    g.fillRect (0, 0, width, dd.height + 2);
    if( jc instanceof Container && !jc.isValid() ) {
        //#214739
        jc.setSize( width, dd.height );
        jc.doLayout();
    }
    SwingUtilities.paintComponent(g, jc, this, 0, 0, width, dd.height + 2);
    g.dispose();
    setImage (nue);
}
 
Example 3
Project: OpenJSharp   File: ImageTests.java   View Source Code Vote up 6 votes
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 4
Project: OpenJSharp   File: CGLGraphicsConfig.java   View Source Code Vote up 6 votes
@Override
public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,
                 final int x1, final int y1, final int x2, final int y2,
                 final BufferCapabilities.FlipContents flipAction) {
    final Graphics g = peer.getGraphics();
    try {
        g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
    } finally {
        g.dispose();
    }
    if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
        final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
        try {
            bg.setBackground(peer.getBackground());
            bg.clearRect(0, 0, backBuffer.getWidth(null),
                         backBuffer.getHeight(null));
        } finally {
            bg.dispose();
        }
    }
}
 
Example 5
Project: OpenJSharp   File: TranslucentWindowPainter.java   View Source Code Vote up 6 votes
/**
 * 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
Project: jdk8u-jdk   File: NonOpaqueDestLCDAATest.java   View Source Code Vote up 6 votes
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);
}
 
Example 7
Project: rapidminer   File: AbstractCachedPainter.java   View Source Code Vote up 5 votes
public void paint(Component c, Graphics g, int x, int y, int w, int h, Object[] args) {
	if (w <= 0 || h <= 0) {
		return;
	}
	Object key = getClass();
	GraphicsConfiguration config = c.getGraphicsConfiguration();
	Cache cache = getCache(key);
	Image image = cache.getImage(key, config, w, h, args);
	int attempts = 0;
	do {
		boolean draw = false;
		if (image instanceof VolatileImage) {
			switch (((VolatileImage) image).validate(config)) {
				case VolatileImage.IMAGE_INCOMPATIBLE:
					((VolatileImage) image).flush();
					image = null;
					break;
				case VolatileImage.IMAGE_RESTORED:
					draw = true;
					break;
			}
		}
		if (image == null) {
			image = createImage(c, w, h, config);
			cache.setImage(key, config, w, h, args, image);
			draw = true;
		}
		if (draw) {
			Graphics g2 = image.getGraphics();
			paintToImage(c, g2, w, h, args);
			g2.dispose();
		}

		paintImage(c, g, x, y, w, h, image, args);
	} while (image instanceof VolatileImage && ((VolatileImage) image).contentsLost() && ++attempts < 3);
}
 
Example 8
Project: openjdk-jdk10   File: BitmaskVolatileImage.java   View Source Code Vote up 5 votes
private static void fill(Image image, Color color) {
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setColor(color);
    g2d.setComposite(AlphaComposite.Src);
    g2d.fillRect(0, 0, S, S);
    g2d.dispose();
}
 
Example 9
Project: jdk8u-jdk   File: IncorrectUnmanagedImageRotatedClip.java   View Source Code Vote up 5 votes
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 10
Project: openjdk-jdk10   File: IncorrectClipXorModeSW2Surface.java   View Source Code Vote up 5 votes
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 11
Project: BrainControl   File: Art.java   View Source Code Vote up 5 votes
private static SpriteSheet cutImage(GraphicsConfiguration gc, String imageName, int xSize, int ySize) throws IOException 
{
	xSize*=GlobalOptions.resolution_factor;
	ySize*=GlobalOptions.resolution_factor;
			
	Image source = getImage(gc, imageName);		
			
	Image[][] cutsource = new Image[source.getWidth(null) / xSize][source.getHeight(null) / ySize];		
	
	for (int x = 0; x < source.getWidth(null) / xSize; x++) 
	{
		for (int y = 0; y < source.getHeight(null) / ySize; y++) 
		{
			Image image = gc.createCompatibleImage(xSize, ySize, Transparency.TRANSLUCENT);
			Graphics2D g = (Graphics2D) image.getGraphics();
							
			//g.setComposite(AlphaComposite.Src);
			
			//ENABLE PARTIAL TRANSPARENCY
			AlphaComposite ac = java.awt.AlphaComposite.getInstance(AlphaComposite.SRC);
	        g.setComposite(ac);						
			
			g.drawImage(source, -x * xSize, -y * ySize, null);
			g.dispose();
			
			cutsource[x][y] = image;
		}
	}		
	
	System.out.println(imageName);
	SpriteSheet ret = new SpriteSheet(xSize, ySize, source.getWidth(null) / xSize, source.getHeight(null) / ySize, cutsource);
	
	return ret;
}
 
Example 12
Project: jdk8u-jdk   File: IncorrectClipXorModeSurface2Surface.java   View Source Code Vote up 5 votes
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 13
Project: openjdk-jdk10   File: SourceClippingBlitTest.java   View Source Code Vote up 5 votes
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 14
Project: jdk8u-jdk   File: IncorrectUnmanagedImageSourceOffset.java   View Source Code Vote up 5 votes
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
Project: openjdk-jdk10   File: NonOpaqueDestLCDAATest.java   View Source Code Vote up 5 votes
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);
}
 
Example 16
Project: openjdk-jdk10   File: IncorrectSourceOffset.java   View Source Code Vote up 5 votes
private static void fill(Image image) {
    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 17
Project: jdk8u-jdk   File: TranslucentWindowPainter.java   View Source Code Vote up 5 votes
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 18
Project: jdk8u-jdk   File: IncorrectUnmanagedImageRotatedClip.java   View Source Code Vote up 5 votes
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 19
Project: jdk8u-jdk   File: IncorrectAlphaSurface2SW.java   View Source Code Vote up 5 votes
/**
 * 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 20
Project: VASSAL-src   File: Transparent.java   View Source Code Vote up 4 votes
public void draw(Graphics g, int x, int y, Component obs, double zoom) {
    if (alpha == 1.0) {
      piece.draw(g, x, y, obs, zoom);
      return;
    }
    if (opaque.isChanged()) {

      int trans = VASSAL.tools.TransparentFilter.getOffscreenEquivalent(obs.getBackground().getRGB(), obs);
      VASSAL.tools.TransparentFilter filter = new VASSAL.tools.TransparentFilter();
      filter.setAlpha(alpha);
      filter.setAlpha(0.0, trans);
      im = opaque.getImage(obs);
      Image im2 = obs.createImage(im.getWidth(obs), im.getHeight(obs));
      Graphics2D gg = (Graphics2D) im2.getGraphics();
      gg.drawImage(im, 0, 0, obs);
      gg.dispose();
      im = obs.createImage(new FilteredImageSource
        (im2.getSource(), filter));
      offset = new Point(piece.boundingBox().x,
                         piece.boundingBox().y);

/*
      im = opaque.getImage(obs);
      final Image im2 = obs.createImage(im.getWidth(obs), im.getHeight(obs));
      final Graphics2D gg = (Graphics2D) im2.getGraphics();
      gg.setComposite(
        AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) alpha));
      gg.drawImage(im, 0, 0, obs);
      gg.dispose();

      offset = new Point(piece.boundingBox().x,
                         piece.boundingBox().y);
*/
    }

    Image scaled = im;
    if (zoom != 1.0) {
      scaled = GameModule.getGameModule().getDataArchive().getScaledImage(im,zoom);
    }
    g.drawImage(scaled,
                x + (int) (zoom * offset.x),
                y + (int) (zoom * offset.y),
                obs);
  }