List of usage examples for java.awt Graphics2D clearRect
public abstract void clearRect(int x, int y, int width, int height);
From source file:at.tuwien.ifs.somtoolbox.visualization.ClusterConnectionsVisualizer.java
@Override public BufferedImage createVisualization(int variantIndex, GrowingSOM gsom, int width, int height) throws SOMToolboxException { GrowingLayer layer = gsom.getLayer(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.WHITE); g.clearRect(0, 0, width, height); // need to calculate some params relative to the unit width/height, which is relative to the desired output size // a unitWidth/Height of 10, as used previously, works by default only fine in the SOMViewer int somWidth = layer.getXSize(); int somHeight = layer.getYSize(); double unitWidth = width / somWidth; double unitHeight = height / somHeight; DoubleMatrix2D unitDistanceMatrix = gsom.getLayer().getUnitDistanceMatrix(); for (int col = 0; col < somWidth; col++) { for (int row = 0; row < somHeight; row++) { if (col < somWidth - 1) { // draw horizontal connection double distanceRight = unitDistanceMatrix.get(layer.getUnitIndex(col, row), layer.getUnitIndex(col + 1, row)); g.setPaint(getColor(distanceRight)); int xPos = (int) (col * unitHeight + unitHeight * 0.7); int yPos = (int) (row * unitWidth + unitWidth * 0.4); g.fillRect(xPos, yPos, (int) (unitWidth * 0.6), (int) (unitHeight * 0.2)); }/*from w w w. j a v a 2s. c o m*/ if (row < somHeight - 1) { // draw vertical connection double distanceLower = unitDistanceMatrix.get(layer.getUnitIndex(col, row), layer.getUnitIndex(col, row + 1)); g.setPaint(getColor(distanceLower)); int xPos = (int) (col * unitHeight + unitHeight * 0.4); int yPos = (int) (row * unitWidth + unitWidth * 0.7); g.fillRect(xPos, yPos, (int) (unitWidth * 0.2), (int) (unitHeight * 0.6)); } } } return image; }
From source file:com.lingxiang2014.util.ImageUtils.java
public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile);/*from w w w.jav a 2 s . c o m*/ Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { } } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:de.uni_siegen.wineme.come_in.thumbnailer.thumbnailers.PDFBoxThumbnailer.java
private BufferedImage convertToImage(final PDPage page, final int imageType, final int thumbWidth, final int thumbHeight)/* */ throws IOException /* */ {//from w ww . j ava 2 s . com /* 707 */ final PDRectangle mBox = page.findMediaBox(); /* 708 */ final float widthPt = mBox.getWidth(); /* 709 */ final float heightPt = mBox.getHeight(); /* 711 */ final int widthPx = thumbWidth; // Math.round(widthPt * scaling); /* 712 */ final int heightPx = thumbHeight; // Math.round(heightPt * scaling); /* 710 */ final double scaling = Math.min((double) thumbWidth / widthPt, (double) thumbHeight / heightPt); // resolution / 72.0F; /* */ /* 714 */ final Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt); /* */ /* 716 */ final BufferedImage retval = new BufferedImage(widthPx, heightPx, imageType); /* 717 */ final Graphics2D graphics = (Graphics2D) retval.getGraphics(); /* 718 */ graphics.setBackground(PDFBoxThumbnailer.TRANSPARENT_WHITE); /* 719 */ graphics.clearRect(0, 0, retval.getWidth(), retval.getHeight()); /* 720 */ graphics.scale(scaling, scaling); /* 721 */ final PageDrawer drawer = new PageDrawer(); /* 722 */ drawer.drawPage(graphics, page, pageDimension); /* */ try /* */ { /* 728 */ final int rotation = page.findRotation(); /* 729 */ if (rotation == 90 || rotation == 270) /* */ { /* 731 */ final int w = retval.getWidth(); /* 732 */ final int h = retval.getHeight(); /* 733 */ final BufferedImage rotatedImg = new BufferedImage(w, h, retval.getType()); /* 734 */ final Graphics2D g = rotatedImg.createGraphics(); /* 735 */ g.rotate(Math.toRadians(rotation), w / 2, h / 2); /* 736 */ g.drawImage(retval, null, 0, 0); /* */ } /* */ } /* */ catch (final ImagingOpException e) /* */ { /* 741 */ //log.warn("Unable to rotate page image", e); /* */ } /* */ /* 744 */ return retval; /* */ }
From source file:com.frostwire.gui.library.LibraryCoverArt.java
private void setPrivateImage(Image image) { coverArtImage = image;//w w w .j a va 2s. co m if (coverArtImage == null) { coverArtImage = defaultCoverArt; } Graphics2D g2 = background.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setBackground(new Color(255, 255, 255, 0)); g2.clearRect(0, 0, getWidth(), getHeight()); g2.drawImage(coverArtImage, 0, 0, getWidth(), getHeight(), null); g2.dispose(); repaint(); getToolkit().sync(); }
From source file:net.groupbuy.util.ImageUtils.java
/** * // w ww . ja v a 2 s . c o m * * @param srcFile * ? * @param destFile * * @param destWidth * * @param destHeight * */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } if (imageOutputStream != null) { try { imageOutputStream.close(); } catch (IOException e) { } } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); operation.addImage(srcFile.getPath()); operation.addImage(destFile.getPath()); if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (IM4JavaException e) { e.printStackTrace(); } } } }
From source file:com.el.ecom.utils.ImageUtils.java
/** * /*from ww w . j a v a 2 s .co m*/ * * @param srcFile ? * @param destFile * @param destWidth * @param destHeight */ public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) { Assert.notNull(srcFile); Assert.state(srcFile.exists()); Assert.state(srcFile.isFile()); Assert.notNull(destFile); Assert.state(destWidth > 0); Assert.state(destHeight > 0); if (type == Type.jdk) { Graphics2D graphics2D = null; ImageOutputStream imageOutputStream = null; ImageWriter imageWriter = null; try { BufferedImage srcBufferedImage = ImageIO.read(srcFile); int srcWidth = srcBufferedImage.getWidth(); int srcHeight = srcBufferedImage.getHeight(); int width = destWidth; int height = destHeight; if (srcHeight >= srcWidth) { width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth)); } else { height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight)); } BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); graphics2D = destBufferedImage.createGraphics(); graphics2D.setBackground(BACKGROUND_COLOR); graphics2D.clearRect(0, 0, destWidth, destHeight); graphics2D.drawImage(srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), (destWidth / 2) - (width / 2), (destHeight / 2) - (height / 2), null); imageOutputStream = ImageIO.createImageOutputStream(destFile); imageWriter = ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName())) .next(); imageWriter.setOutput(imageOutputStream); ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam(); imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0)); imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam); imageOutputStream.flush(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { if (graphics2D != null) { graphics2D.dispose(); } if (imageWriter != null) { imageWriter.dispose(); } try { if (imageOutputStream != null) { imageOutputStream.close(); } } catch (IOException e) { } } } else { IMOperation operation = new IMOperation(); operation.thumbnail(destWidth, destHeight); operation.gravity("center"); operation.background(toHexEncoding(BACKGROUND_COLOR)); operation.extent(destWidth, destHeight); operation.quality((double) DEST_QUALITY); try { operation.addImage(srcFile.getCanonicalPath()); operation.addImage(destFile.getCanonicalPath()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } if (type == Type.graphicsMagick) { ConvertCmd convertCmd = new ConvertCmd(true); if (graphicsMagickPath != null) { convertCmd.setSearchPath(graphicsMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IM4JavaException e) { throw new RuntimeException(e.getMessage(), e); } } else { ConvertCmd convertCmd = new ConvertCmd(false); if (imageMagickPath != null) { convertCmd.setSearchPath(imageMagickPath); } try { convertCmd.run(operation); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage(), e); } catch (IM4JavaException e) { throw new RuntimeException(e.getMessage(), e); } } } }
From source file:edworld.pdfreader4humans.PDFReader.java
public BufferedImage createPageImage(int pageNumber, int scaling, Color inkColor, Color backgroundColor, boolean showStructure) throws IOException { Map<String, Font> fonts = new HashMap<String, Font>(); PDRectangle cropBox = getPageCropBox(pageNumber); BufferedImage image = new BufferedImage(Math.round(cropBox.getWidth() * scaling), Math.round(cropBox.getHeight() * scaling), BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = image.createGraphics(); graphics.setBackground(backgroundColor); graphics.clearRect(0, 0, image.getWidth(), image.getHeight()); graphics.setColor(backgroundColor);//from w ww .j a v a 2s. c om graphics.fillRect(0, 0, image.getWidth(), image.getHeight()); graphics.setColor(inkColor); graphics.scale(scaling, scaling); for (Component component : getFirstLevelComponents(pageNumber)) draw(component, graphics, inkColor, backgroundColor, showStructure, fonts); graphics.dispose(); return image; }
From source file:edu.umn.cs.spatialHadoop.operations.GeometricPlot.java
private static <S extends Shape> void plotLocal(Path inFile, Path outFile, OperationsParams params) throws IOException { int width = params.getInt("width", 1000); int height = params.getInt("height", 1000); Color strokeColor = params.getColor("color", Color.BLACK); int color = strokeColor.getRGB(); String hdfDataset = (String) params.get("dataset"); Shape shape = hdfDataset != null ? new NASARectangle() : (Shape) params.getShape("shape", null); Shape plotRange = params.getShape("rect", null); boolean keepAspectRatio = params.is("keep-ratio", true); String valueRangeStr = (String) params.get("valuerange"); MinMax valueRange;//w w w. j a v a2 s .c om if (valueRangeStr == null) { valueRange = null; } else { String[] parts = valueRangeStr.split(","); valueRange = new MinMax(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); } InputSplit[] splits; FileSystem inFs = inFile.getFileSystem(params); FileStatus inFStatus = inFs.getFileStatus(inFile); if (inFStatus != null && !inFStatus.isDir()) { // One file, retrieve it immediately. // This is useful if the input is a hidden file which is automatically // skipped by FileInputFormat. We need to plot a hidden file for the case // of plotting partition boundaries of a spatial index splits = new InputSplit[] { new FileSplit(inFile, 0, inFStatus.getLen(), new String[0]) }; } else { JobConf job = new JobConf(params); ShapeInputFormat<Shape> inputFormat = new ShapeInputFormat<Shape>(); ShapeInputFormat.addInputPath(job, inFile); splits = inputFormat.getSplits(job, 1); } boolean vflip = params.is("vflip"); Rectangle fileMbr; if (plotRange != null) { fileMbr = plotRange.getMBR(); } else if (hdfDataset != null) { // Plotting a NASA file fileMbr = new Rectangle(-180, -90, 180, 90); } else { fileMbr = FileMBR.fileMBR(inFile, params); } if (keepAspectRatio) { // Adjust width and height to maintain aspect ratio if (fileMbr.getWidth() / fileMbr.getHeight() > (double) width / height) { // Fix width and change height height = (int) (fileMbr.getHeight() * width / fileMbr.getWidth()); } else { width = (int) (fileMbr.getWidth() * height / fileMbr.getHeight()); } } boolean adaptiveSample = shape instanceof Point && params.getBoolean("sample", false); float adaptiveSampleRatio = 0.0f; if (adaptiveSample) { // Calculate the sample ratio long recordCount = FileMBR.fileMBR(inFile, params).recordCount; adaptiveSampleRatio = params.getFloat(AdaptiveSampleFactor, 1.0f) * width * height / recordCount; } boolean gradualFade = !(shape instanceof Point) && params.getBoolean("fade", false); if (hdfDataset != null) { // Collects some stats about the HDF file if (valueRange == null) valueRange = Aggregate.aggregate(new Path[] { inFile }, params); NASAPoint.minValue = valueRange.minValue; NASAPoint.maxValue = valueRange.maxValue; NASAPoint.setColor1(params.getColor("color1", Color.BLUE)); NASAPoint.setColor2(params.getColor("color2", Color.RED)); NASAPoint.gradientType = params.getGradientType("gradient", NASAPoint.GradientType.GT_HUE); } double scale2 = (double) width * height / (fileMbr.getWidth() * fileMbr.getHeight()); double scale = Math.sqrt(scale2); // Create an image BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = image.createGraphics(); Color bg_color = params.getColor("bgcolor", new Color(0, 0, 0, 0)); graphics.setBackground(bg_color); graphics.clearRect(0, 0, width, height); graphics.setColor(strokeColor); for (InputSplit split : splits) { if (hdfDataset != null) { // Read points from the HDF file RecordReader<NASADataset, NASAShape> reader = new HDFRecordReader(params, (FileSplit) split, hdfDataset, true); NASADataset dataset = reader.createKey(); while (reader.next(dataset, (NASAShape) shape)) { // Skip with a fixed ratio if adaptive sample is set if (adaptiveSample && Math.random() > adaptiveSampleRatio) continue; if (plotRange == null || shape.isIntersected(shape)) { shape.draw(graphics, fileMbr, width, height, 0.0); } } reader.close(); } else { RecordReader<Rectangle, Shape> reader = new ShapeRecordReader<Shape>(params, (FileSplit) split); Rectangle cell = reader.createKey(); while (reader.next(cell, shape)) { // Skip with a fixed ratio if adaptive sample is set if (adaptiveSample && Math.random() > adaptiveSampleRatio) continue; Rectangle shapeMBR = shape.getMBR(); if (shapeMBR != null) { if (plotRange == null || shapeMBR.isIntersected(plotRange)) { if (gradualFade) { double sizeInPixels = (shapeMBR.getWidth() + shapeMBR.getHeight()) * scale; if (sizeInPixels < 1.0 && Math.round(sizeInPixels * 255) < 1.0) { // This shape can be safely skipped as it is too small to be plotted continue; } else { int alpha = (int) Math.round(sizeInPixels * 255); graphics.setColor(new Color((alpha << 24) | color, true)); } } shape.draw(graphics, fileMbr, width, height, scale2); } } } reader.close(); } } // Write image to output graphics.dispose(); if (vflip) { AffineTransform tx = AffineTransform.getScaleInstance(1, -1); tx.translate(0, -image.getHeight()); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = op.filter(image, null); } FileSystem outFs = outFile.getFileSystem(params); OutputStream out = outFs.create(outFile, true); ImageIO.write(image, "png", out); out.close(); }
From source file:org.jrecruiter.service.impl.DataServiceImpl.java
/** {@inheritDoc} */ @Transactional(readOnly = true, propagation = Propagation.SUPPORTS) public Image getGoogleMapImage(final BigDecimal latitude, final BigDecimal longitude, final Integer zoomLevel) { if (longitude == null) { throw new IllegalArgumentException("Longitude cannot be null."); }/*from w w w. j a v a 2 s.co m*/ if (latitude == null) { throw new IllegalArgumentException("Latitude cannot be null."); } if (zoomLevel == null) { throw new IllegalArgumentException("ZoomLevel cannot be null."); } final URI url = GoogleMapsUtils.buildGoogleMapsStaticUrl(latitude, longitude, zoomLevel); BufferedImage img; try { URLConnection conn = url.toURL().openConnection(); img = ImageIO.read(conn.getInputStream()); } catch (UnknownHostException e) { LOGGER.error("Google static MAPS web service is not reachable (UnknownHostException).", e); img = new BufferedImage(GoogleMapsUtils.defaultWidth, 100, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics = img.createGraphics(); final Map<Object, Object> renderingHints = CollectionUtils.getHashMap(); renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); graphics.addRenderingHints(renderingHints); graphics.setBackground(Color.WHITE); graphics.setColor(Color.GRAY); graphics.clearRect(0, 0, GoogleMapsUtils.defaultWidth, 100); graphics.drawString("Not Available", 30, 30); } catch (IOException e) { throw new IllegalStateException(e); } return img; }
From source file:MyCanvas.java
public Graphics2D createDemoGraphics2D(Graphics g) { Graphics2D g2 = null; if (offImg == null || offImg.getWidth() != getSize().width || offImg.getHeight() != getSize().height) { offImg = (BufferedImage) createImage(getSize().width, getSize().height); }//from w w w . ja v a2s . c o m if (offImg != null) { g2 = offImg.createGraphics(); g2.setBackground(getBackground()); } // .. set attributes .. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // .. clear canvas .. g2.clearRect(0, 0, getSize().width, getSize().height); return g2; }