Java Code Examples for java.awt.image.BufferedImage.getGraphics()

The following are Jave code examples for showing how to use getGraphics() of the java.awt.image.BufferedImage 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: openjdk-jdk10   File: IconTest.java   View Source Code Vote up 7 votes
private static BufferedImage generateImage(int scale, Color c) {
    int x = SZ * scale;
    BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();
    try {
        if (g != null) {
            g.setColor(c);
            g.fillRect(0, 0, x, x);
            g.setColor(Color.GREEN);
            g.drawRect(0, 0, x-1, x-1);
        }
    } finally {
        g.dispose();
    }
    return img;
}
 
Example 2
Project: openjdk-jdk10   File: ImageGenerator.java   View Source Code Vote up 6 votes
public ImageGenerator(int _width, int _height, Color bgColor)
{
      width = _width;
      height = _height;
      bi = new BufferedImage(
          width,
          height,
          BufferedImage.TYPE_INT_ARGB);
      Graphics gr = bi.getGraphics();
      if(null==bgColor){
          bgColor = Color.WHITE;
      }
      gr.setColor(bgColor);
      gr.fillRect(0, 0, width, height);
      paint(gr);
      gr.dispose();
}
 
Example 3
Project: openjdk-jdk10   File: ImageTests.java   View Source Code Vote up 6 votes
public void runTest(Object ctx, int numReps) {
    ImageOpTests.Context ictx = (ImageOpTests.Context)ctx;
    BufferedImageOp op = ictx.bufImgOp;
    BufferedImage src = ictx.bufSrc;
    BufferedImage dst = ictx.bufDst;
    if (ictx.touchSrc) {
        Graphics gSrc = src.getGraphics();
        do {
            gSrc.fillRect(0, 0, 1, 1);
            op.filter(src, dst);
        } while (--numReps > 0);
    } else {
        do {
            op.filter(src, dst);
        } while (--numReps > 0);
    }
}
 
Example 4
Project: jdk8u-jdk   File: DisplayChangeVITest.java   View Source Code Vote up 6 votes
DisplayChangeVITest() {
    selectDisplayModes();
    addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                synchronized (lock) {
                    done = true;
                }
            }
        }
    });
    sprite = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    sprite.getRaster().getDataBuffer();
    Graphics g = sprite.getGraphics();
    g.setColor(Color.yellow);
    g.fillRect(0, 0, sprite.getWidth(), sprite.getHeight());
}
 
Example 5
Project: Yass   File: YassSongInfo.java   View Source Code Vote up 6 votes
/**
 * Sets the background attribute of the YassSongInfo object
 *
 * @param im The new background value
 */
public void setBackground(Image im) {
    bgString = null;

    origbg = new BufferedImage(im.getWidth(null), im.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics g = origbg.getGraphics();
    g.drawImage(im, 0, 0, null);
    g.dispose();

    imgbg = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = imgbg.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);
    g2d.drawImage(origbg, 0, 0, 800, 600, null);
    g2d.dispose();

    setProperty("bg-filename", "");
    setProperty("bg-filesize", "");
    setProperty("bg-width", origbg == null ? "" : origbg.getWidth() + "");
    setProperty("bg-height", origbg == null ? "" : origbg.getHeight() + "");

    repaint();
}
 
Example 6
Project: ALTTPMenuPractice   File: MenuGameConstants.java   View Source Code Vote up 6 votes
/**
 *
 * @param s
 * @param flag 0 : white | 1 : yellow | 2 : gray
 * @return
 */
public static BufferedImage makeWordImage(String s, int flag) {
	BufferedImage ret = new BufferedImage(s.length() * 8, 8, BufferedImage.TYPE_INT_ARGB);
	char[] temp = s.toUpperCase().toCharArray();
	Graphics g = ret.getGraphics();
	int y = 8 * flag;
	for (int i = 0; i < temp.length; i++) {
		int loc = CHARS.indexOf(temp[i]);
		BufferedImage t = FONT_SPRITES.getSubimage(loc * 8, y, 8, 8);
		g.drawImage(t, i * 8, 0, null);
	}
	return ret;
}
 
Example 7
Project: jdk8u-jdk   File: Test7019861.java   View Source Code Vote up 6 votes
public static void main(String[] argv) throws Exception {
    BufferedImage im = getWhiteImage(30, 30);
    Graphics2D g2 = (Graphics2D)im.getGraphics();
    g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(KEY_STROKE_CONTROL, VALUE_STROKE_PURE);
    g2.setStroke(new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
    g2.setBackground(Color.white);
    g2.setColor(Color.black);

    Path2D p = getPath(0, 0, 20);
    g2.draw(p);

    if (!(new Color(im.getRGB(20, 19))).equals(Color.black)) {
        throw new Exception("This pixel should be black");
    }
}
 
Example 8
Project: openjdk-jdk10   File: AquaImageFactory.java   View Source Code Vote up 5 votes
static BufferedImage getAppIconImageCompositedOn(final Image background, int scaleFactor) {

        final int scaledAlertIconSize = kAlertIconSize * scaleFactor;
        final int kAlertSubIconSize = (int) (scaledAlertIconSize * 0.5);
        final int kAlertSubIconInset = scaledAlertIconSize - kAlertSubIconSize;
        final Icon smallAppIconScaled = new AquaIcon.CachingScalingIcon(
                kAlertSubIconSize, kAlertSubIconSize) {
                    Image createImage() {
                        return getGenericJavaIcon();
                    }
                };

        final BufferedImage image = new BufferedImage(scaledAlertIconSize,
                scaledAlertIconSize, BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics g = image.getGraphics();
        g.drawImage(background, 0, 0,
                scaledAlertIconSize, scaledAlertIconSize, null);
        if (g instanceof Graphics2D) {
            // improves icon rendering quality in Quartz
            ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
        }

        smallAppIconScaled.paintIcon(null, g,
                kAlertSubIconInset, kAlertSubIconInset);
        g.dispose();

        return image;
    }
 
Example 9
Project: jmt   File: SampleQNAnimation.java   View Source Code Vote up 5 votes
@Override
public Image getBGTileIcon(Rectangle bounds) {
	BufferedImage bi = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_4BYTE_ABGR);
	Graphics g = bi.getGraphics();
	g.setColor(new Color(150, 150, 150, 200));
	g.fillRect(0, 0, bounds.width, bounds.height);
	return bi;
}
 
Example 10
Project: bitcoin_wallet_recovery_tool   File: Main.java   View Source Code Vote up 5 votes
private static void createQRImage(File qrFile, String qrCodeText, int size, String fileType)
		throws WriterException, IOException {
	// Create the ByteMatrix for the QR-Code that encodes the given String
	Hashtable hintMap = new Hashtable();
	hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
	QRCodeWriter qrCodeWriter = new QRCodeWriter();
	BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
	// Make the BufferedImage that are to hold the QRCode
	int matrixWidth = byteMatrix.getWidth();
	BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
	image.createGraphics();

	Graphics2D graphics = (Graphics2D) image.getGraphics();
	graphics.setColor(Color.WHITE);
	graphics.fillRect(0, 0, matrixWidth, matrixWidth);
	// Paint and save the image using the ByteMatrix
	graphics.setColor(Color.BLACK);

	for (int i = 0; i < matrixWidth; i++) {
		for (int j = 0; j < matrixWidth; j++) {
			if (byteMatrix.get(i, j)) {
				graphics.fillRect(i, j, 1, 1);
			}
		}
	}
	ImageIO.write(image, fileType, qrFile);
}
 
Example 11
Project: SE2017-Team2   File: PetriDrawer.java   View Source Code Vote up 5 votes
public BufferedImage draw(Petrinet petrinet){
	petriConverter.clearPetriConverter();
	petriConverter.convertPetri(petrinet);
	petriWindow.updateGraph(petriConverter.nodes, petriConverter.edges);
	
	BufferedImage bi = (BufferedImage) petriWindow.createImage(petriWindow.getWidth(), petriWindow.getHeight());
	bi.createGraphics();
	Graphics g = bi.getGraphics();
	petriWindow.paint(g);
	return bi;					
}
 
Example 12
Project: openjdk-jdk10   File: MultiResolutionDragImageTest.java   View Source Code Vote up 5 votes
private static Image createImage(final int length, final Color color) {

        final BufferedImage image = new BufferedImage(length, length,
                BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics graphics = image.getGraphics();
        graphics.setColor(color);
        graphics.fillRect(0, 0, length, length);
        graphics.dispose();
        return image;
    }
 
Example 13
Project: sumo   File: FontFactory.java   View Source Code Vote up 5 votes
private void createFontMetrics() 
{
	BufferedImage baseImage = new BufferedImage(1, 1,
			BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = (Graphics2D) baseImage.getGraphics();
	fontMetrics = g.getFontMetrics(font);
}
 
Example 14
Project: jdk8u-jdk   File: MultiResolutionDragImageTest.java   View Source Code Vote up 5 votes
private static Image createImage(final int length, final Color color) {

        final BufferedImage image = new BufferedImage(length, length,
                BufferedImage.TYPE_INT_ARGB_PRE);
        final Graphics graphics = image.getGraphics();
        graphics.setColor(color);
        graphics.fillRect(0, 0, length, length);
        graphics.dispose();
        return image;
    }
 
Example 15
Project: BasicsProject   File: ImageCognition.java   View Source Code Vote up 5 votes
/**ζˆͺ取图像*/  
public static BufferedImage cutImage(BufferedImage bufferedImage, int w, int h,int x,int y){
	BufferedImage distin = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
	Graphics g = distin.getGraphics();
	g.drawImage(bufferedImage, 0, 0, w, h, x, y, x+w, y+h, null);
	/*try {
		ImageIO.write(distin, "jpg", new File("E:/IMG/test/"+x+".jpg"));
	} catch (IOException e) {
		e.printStackTrace();
	}*/
	return distin;
}
 
Example 16
Project: incubator-netbeans   File: InnerPanelSupport.java   View Source Code Vote up 4 votes
private boolean updateTooltipImage(RenderedImage contents, int row, int col) {
            TableCellRenderer renderer = listClasses.getCellRenderer(row, col); 
            Component component = listClasses.prepareRenderer(renderer, row, col);
            
            if (!(component instanceof JComponent)) {
                // sorry.
                return false;
            }
            Rectangle cellRect = listClasses.getCellRect(row, col, false);
            Dimension size = component.getPreferredSize();
            Rectangle visibleRect = listClasses.getVisibleRect();
            
            // The visible region is wide enough, hide the tooltip
            if (cellRect.width >= size.width) {
                hidePopup();
                return false;
            }           
            // Hide if the cell does not vertically fit
            if (cellRect.y + size.height > visibleRect.y + visibleRect.height + 1) {
                hidePopup();
                return false;
            }
            BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
            Graphics g = img.getGraphics();
            g.setClip(null);
            g.setColor(listClasses.getBackground());
            g.fillRect(0, 0, size.width, size.height);
            g.translate(-cellRect.x, -cellRect.y);
//            g.translate(-visibleRect.x, -visibleRect.y);
            
            cellRect.width = size.width;
            // prevent some repaing issues, see javadoc for CellRendererPane.
            rendererPane.paintComponent(g, component, listClasses, cellRect);
            
            // if table displays lines, frame the cell's display using lines.
            if (listClasses.getShowHorizontalLines()) {
                int rightX = size.width - 1;
                g.translate(cellRect.x, cellRect.y);
                g.setColor(listClasses.getForeground());
                g.drawLine(0, 0, rightX, 0);
                g.drawLine(rightX, 0, rightX, size.height);
                g.drawLine(rightX, size.height - 1, 0, size.height - 1);
            }
            g.dispose();
            rendererPane.remove(component);
            contents.setImage(img);
            return true;
        }
 
Example 17
Project: ConvNetCL4J   File: Scalr.java   View Source Code Vote up 4 votes
/**
 * Used to copy a {@link BufferedImage} from a non-optimal type into a new
 * {@link BufferedImage} instance of an optimal type (RGB or ARGB). If
 * <code>src</code> is already of an optimal type, then it is returned
 * unmodified.
 * <p/>
 * This method is meant to be used by any calling code (imgscalr's or
 * otherwise) to convert any inbound image from a poorly supported image
 * type into the 2 most well-supported image types in Java2D (
 * {@link BufferedImage#TYPE_INT_RGB} or {@link BufferedImage#TYPE_INT_ARGB}
 * ) in order to ensure all subsequent graphics operations are performed as
 * efficiently and correctly as possible.
 * <p/>
 * When using Java2D to work with image types that are not well supported,
 * the results can be anything from exceptions bubbling up from the depths
 * of Java2D to images being completely corrupted and just returned as solid
 * black.
 * 
 * @param src
 *            The image to copy (if necessary) into an optimally typed
 *            {@link BufferedImage}.
 * 
 * @return a representation of the <code>src</code> image in an optimally
 *         typed {@link BufferedImage}, otherwise <code>src</code> if it was
 *         already of an optimal type.
 * 
 * @throws IllegalArgumentException
 *             if <code>src</code> is <code>null</code>.
 */
protected static BufferedImage copyToOptimalImage(BufferedImage src)
		throws IllegalArgumentException {
	if (src == null)
		throw new IllegalArgumentException("src cannot be null");

	// Calculate the type depending on the presence of alpha.
	int type = (src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
			: BufferedImage.TYPE_INT_ARGB);
	BufferedImage result = new BufferedImage(src.getWidth(),
			src.getHeight(), type);

	// Render the src image into our new optimal source.
	Graphics g = result.getGraphics();
	g.drawImage(src, 0, 0, null);
	g.dispose();

	return result;
}
 
Example 18
Project: myfaces-trinidad   File: BlafImageUtils.java   View Source Code Vote up 4 votes
static ImmInsets __getDialogPadding()
{
  if (_sDialogPadding != null)
    return _sDialogPadding;

  // Create a BufferedImage that we can use to rasterize some glyphs.
  int width = 40;
  int height = 40;
  BufferedImage image = new BufferedImage(40,
                                          40,
                                          BufferedImage.TYPE_INT_ARGB);

  // Get the Graphics object to use to draw into the image
  Graphics g = image.getGraphics();

  // Clear out the image
  g.setColor(Color.white);
  g.fillRect(0, 0, width, height);

  // Render our glyphs
  g.setColor(Color.black);
  g.setFont(new Font("Dialog", Font.PLAIN, 12));

  FontMetrics metrics = g.getFontMetrics();
  int baseline = metrics.getAscent();

  g.drawString("X", 0, baseline);

  // Now that we have rendered the glyphs, we examine the
  // image to see how many lines of padding we've got.
  int top = 0;
  for (int y = 0; y < height; y++)
  {
    if (!_isWhiteScanline(image, y, width))
    {
      top = y;
      break;
    }
  }

  // Just use the descent as the bottom padding
  int bottom = metrics.getDescent();

  _sDialogPadding = new ImmInsets(top, 0, bottom, 0);


  // Clean up
  g.dispose();
  image.flush();

  return _sDialogPadding;
}
 
Example 19
Project: PokeData   File: BlockRenderer.java   View Source Code Vote up 4 votes
private Graphics2D getGraphics(BufferedImage image) {
    Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

    return graphics;
}
 
Example 20
Project: trashjam2017   File: BufferedImageUtil.java   View Source Code Vote up 2 votes
/**
 * Implement of transform copy area for 1.4
 * 
 * @param image The image to copy
	 * @param x The x position to copy to
 * @param y The y position to copy to
 * @param width The width of the image
 * @param height The height of the image
 * @param dx The transform on the x axis
 * @param dy The transform on the y axis
 */
private static void copyArea(BufferedImage image, int x, int y, int width, int height, int dx, int dy) {
	Graphics2D g = (Graphics2D) image.getGraphics();
	
	g.drawImage(image.getSubimage(x, y, width, height),x+dx,y+dy,null);
}