List of usage examples for java.awt Graphics2D drawImage
public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);
From source file:org.jax.maanova.plot.MaanovaChartPanel.java
/** * {@inheritDoc}/*from w ww. java 2 s . com*/ */ @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; if (this.chartImage != null) { if (this.chartImage.getWidth() == this.getWidth() && this.chartImage.getHeight() == this.getHeight()) { g2.drawImage(this.chartImage, 0, 0, this); } else { g2.drawImage(this.chartImage, 0, 0, this.getWidth(), this.getHeight(), this.getBackground(), this); } } if (this.dragRectangle != null) { this.renderDragRectangle(g2); } }
From source file:com.funambol.foundation.util.MediaUtils.java
/** * Rotates given buffered image by given amount of degree. * The valid degree values are 0, 90, 180, 270. * If the image is a jpg, the rotation is lossless, exif data are preserved * and image size is almost the same.//from w ww . ja va 2 s .com * * @param bufImage the buffered image * @param degree amount of degree to apply * @return a buffered image containing rotated image data * @throws PicturesException if amount of degree is invalid or if an * IOException occurs */ private static BufferedImage rotateImage(BufferedImage bufImage, int degree) throws FileDataObjecyUtilsException { degree = degree % 360; int h; int w; switch (degree) { case 0: case 180: h = bufImage.getHeight(); w = bufImage.getWidth(); break; case 90: case 270: h = bufImage.getWidth(); w = bufImage.getHeight(); break; default: throw new FileDataObjecyUtilsException( "Error rotating image since the '" + degree + "' degree value is unsupported"); } BufferedImage out = null; int bufImageType = bufImage.getType(); if (BufferedImage.TYPE_BYTE_INDEXED == bufImageType || BufferedImage.TYPE_BYTE_BINARY == bufImageType) { IndexColorModel model = (IndexColorModel) bufImage.getColorModel(); out = new BufferedImage(w, h, bufImage.getType(), model); } else if (BufferedImage.TYPE_CUSTOM == bufImageType) { // we don't know what type of image it can be // there's a bug in some VM that cause some PNG images to have // type custom: this should take care of this issue //check if we need to have alpha channel boolean alpha = bufImage.getTransparency() != BufferedImage.OPAQUE; if (alpha) { // TYPE_INT_ARGB_PRE gives you smaller output images // than TYPE_INT_ARGB out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE); } else { out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } } else { out = new BufferedImage(w, h, bufImage.getType()); } Graphics2D g2d = out.createGraphics(); Map renderingHints = new HashMap(); renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setRenderingHints(renderingHints); g2d.rotate(Math.toRadians(degree)); switch (degree) { case 90: g2d.translate(0, -w); break; case 180: g2d.translate(-w, -h); break; case 270: g2d.translate(-h, 0); break; } g2d.drawImage(bufImage, null, 0, 0); g2d.dispose(); return out; }
From source file:CompositeEffects.java
/** Draw the example */ public void paint(Graphics g1) { Graphics2D g = (Graphics2D) g1; // fill the background g.setPaint(new Color(175, 175, 175)); g.fillRect(0, 0, getWidth(), getHeight()); // Set text attributes g.setColor(Color.black);//w ww. j ava 2 s .c o m g.setFont(new Font("SansSerif", Font.BOLD, 12)); // Draw the unmodified image g.translate(10, 10); g.drawImage(cover, 0, 0, this); g.drawString("SRC_OVER", 0, COVERHEIGHT + 15); // Draw the cover again, using AlphaComposite to make the opaque // colors of the image 50% translucent g.translate(COVERWIDTH + 10, 0); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); g.drawImage(cover, 0, 0, this); // Restore the pre-defined default Composite for the screen, so // opaque colors stay opaque. g.setComposite(AlphaComposite.SrcOver); // Label the effect g.drawString("SRC_OVER, 50%", 0, COVERHEIGHT + 15); // Now get an offscreen image to work with. In order to achieve // certain compositing effects, the drawing surface must support // transparency. Onscreen drawing surfaces cannot, so we have to do the // compositing in an offscreen image that is specially created to have // an "alpha channel", then copy the final result to the screen. BufferedImage offscreen = new BufferedImage(COVERWIDTH, COVERHEIGHT, BufferedImage.TYPE_INT_ARGB); // First, fill the image with a color gradient background that varies // left-to-right from opaque to transparent yellow Graphics2D osg = offscreen.createGraphics(); osg.setPaint(new GradientPaint(0, 0, Color.yellow, COVERWIDTH, 0, new Color(255, 255, 0, 0))); osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT); // Now copy the cover image on top of this, but use the DstOver rule // which draws it "underneath" the existing pixels, and allows the // image to show depending on the transparency of those pixels. osg.setComposite(AlphaComposite.DstOver); osg.drawImage(cover, 0, 0, this); // And display this composited image on the screen. Note that the // image is opaque and that none of the screen background shows through g.translate(COVERWIDTH + 10, 0); g.drawImage(offscreen, 0, 0, this); g.drawString("DST_OVER", 0, COVERHEIGHT + 15); // Now start over and do a new effect with the off-screen image. // First, fill the offscreen image with a new color gradient. We // don't care about the colors themselves; we just want the // translucency of the background to vary. We use opaque black to // transparent black. Note that since we've already used this offscreen // image, we set the composite to Src, we can fill the image and // ignore anything that is already there. osg.setComposite(AlphaComposite.Src); osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0))); osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT); // Now set the compositing type to SrcIn, so colors come from the // source, but translucency comes from the destination osg.setComposite(AlphaComposite.SrcIn); // Draw our loaded image into the off-screen image, compositing it. osg.drawImage(cover, 0, 0, this); // And then copy our off-screen image to the screen. Note that the // image is translucent and some of the image shows through. g.translate(COVERWIDTH + 10, 0); g.drawImage(offscreen, 0, 0, this); g.drawString("SRC_IN", 0, COVERHEIGHT + 15); // If we do the same thing but use SrcOut, then the resulting image // will have the inverted translucency values of the destination osg.setComposite(AlphaComposite.Src); osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0))); osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT); osg.setComposite(AlphaComposite.SrcOut); osg.drawImage(cover, 0, 0, this); g.translate(COVERWIDTH + 10, 0); g.drawImage(offscreen, 0, 0, this); g.drawString("SRC_OUT", 0, COVERHEIGHT + 15); // Here's a cool effect; it has nothing to do with compositing, but // uses an arbitrary shape to clip the image. It uses Area to combine // shapes into more complicated ones. g.translate(COVERWIDTH + 10, 0); Shape savedClip = g.getClip(); // Save current clipping region // Create a shape to use as the new clipping region. // Begin with an ellipse Area clip = new Area(new Ellipse2D.Float(0, 0, COVERWIDTH, COVERHEIGHT)); // Intersect with a rectangle, truncating the ellipse. clip.intersect(new Area(new Rectangle(5, 5, COVERWIDTH - 10, COVERHEIGHT - 10))); // Then subtract an ellipse from the bottom of the truncated ellipse. clip.subtract(new Area(new Ellipse2D.Float(COVERWIDTH / 2 - 40, COVERHEIGHT - 20, 80, 40))); // Use the resulting shape as the new clipping region g.clip(clip); // Then draw the image through this clipping region g.drawImage(cover, 0, 0, this); // Restore the old clipping region so we can label the effect g.setClip(savedClip); g.drawString("Clipping", 0, COVERHEIGHT + 15); }
From source file:CompositeTest.java
public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D gImage = image.createGraphics(); gImage.setPaint(Color.red);/*from www . ja va 2 s. c om*/ gImage.fill(shape1); AlphaComposite composite = AlphaComposite.getInstance(rule, alpha); gImage.setComposite(composite); gImage.setPaint(Color.blue); gImage.fill(shape2); g2.drawImage(image, null, 0, 0); }
From source file:c.depthchart.ViewerPanel.java
public void paintComponent(Graphics g) // Draw the depth image and statistics info { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // convert image pixel array into an image DataBufferByte dataBuffer = new DataBufferByte(imgbytes, imWidth * imHeight); Raster raster = Raster.createPackedRaster(dataBuffer, imWidth, imHeight, 8, null); image.setData(raster);/*from ww w . j ava 2s. com*/ if (image != null) g2.drawImage(image, 0, 0, this); writeStats(g2); }
From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.MatrixChart.java
private void buildFinalChart(String title, String subTitle, String filename, String xAxisLabel, String yAxisLabel, int width, int height) { this.buildMainChart(title, subTitle, xAxisLabel, yAxisLabel, filename); this.buildLegendChart(5); BufferedImage mainChartImage = this.mainChart.createBufferedImage(width, height); BufferedImage legendChartImage = this.legendChart.createBufferedImage(width / 6, height / 3); BufferedImage info = null;/*from w ww .j a v a 2 s.co m*/ try { info = ImageIO.read(MatrixChart.logoFile); } catch (IOException ex) { } BufferedImage total = new BufferedImage(width + (width / 6), height, BufferedImage.TYPE_INT_RGB); Graphics2D g = total.createGraphics(); g.drawImage(mainChartImage, 0, 0, null); g.drawImage(legendChartImage, width, height / 4, null); g.setPaint(Color.WHITE); g.fillRect(width, 0, width, height / 4); g.fillRect(width, (height / 4) + (height / 3), width, height); if (info != null) { // g.drawImage(info, (width+(width/6))-info.getWidth(),10, null); // // up-right g.drawImage(info, (width + (width / 6)) - info.getWidth(), height - info.getHeight(), null); // down-right } g.dispose(); try { javax.imageio.ImageIO.write(total, "png", XMLHelper.createFileWithDirs(filename)); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:org.geomajas.plugin.rasterizing.layer.RasterDirectLayer.java
private BufferedImage makeOpaque(BufferedImage image) { if (image.getType() == BufferedImage.TYPE_CUSTOM) { log.warn("makeOpaque {} Unknown Image Type 0: ", getTitle()); return image; }// w ww .j a v a 2 s . co m BufferedImage opaqueCopy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); Graphics2D g1 = opaqueCopy.createGraphics(); g1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getOpacity())); g1.drawImage(image, null, 0, 0); g1.dispose(); return opaqueCopy; }
From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java
private static BufferedImage imageToBufferedImage(Image image) { BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, 0, 0, null); g2.dispose();//from w w w . j a v a 2 s .c om return bufferedImage; }
From source file:PSDReader.java
/** * Gets the image contents of frame n. Note that this expands the image to the * full frame size (if the layer was smaller) and any subsequent use of * getLayer() will return the full image. * //from w w w . j a v a2s .com * @return BufferedImage representation of frame, or null if n is invalid. */ public BufferedImage getFrame(int n) { BufferedImage im = null; if ((n >= 0) && (n < nLayers)) { im = frames[n]; LayerInfo info = layers[n]; if ((info.w != width) || (info.h != height)) { BufferedImage temp = new BufferedImage(width, height, ImageType); Graphics2D gc = temp.createGraphics(); gc.drawImage(im, info.x, info.y, null); gc.dispose(); im = temp; frames[n] = im; } } return im; }