List of usage examples for java.awt Graphics2D clearRect
public abstract void clearRect(int x, int y, int width, int height);
From source file:ClipImage.java
public Graphics2D createDemoGraphics2D(Graphics g) { Graphics2D g2 = null; if (offImg == null || offImg.getWidth() != w || offImg.getHeight() != h) { offImg = (BufferedImage) createImage(w, h); newBufferedImage = true;/*w w w .j a v a 2 s . c o m*/ } if (offImg != null) { g2 = offImg.createGraphics(); g2.setBackground(getBackground()); } // .. set attributes .. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); // .. clear canvas .. g2.clearRect(0, 0, w, h); return g2; }
From source file:ClipImage.java
public Graphics2D createDemoGraphics2D(Graphics g) { Graphics2D g2 = null; if (offImg == null || offImg.getWidth() != w || offImg.getHeight() != h) { offImg = (BufferedImage) createImage(w, h); newBufferedImage = true;/*from w w w . jav a 2s . c om*/ } if (offImg != null) { g2 = offImg.createGraphics(); g2.setBackground(getBackground()); } // .. set attributes .. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); // .. clear canvas .. g2.clearRect(0, 0, w, h); return g2; }
From source file:gdt.jgui.entity.webset.JWeblinkEditor.java
private void showIconMenu(MouseEvent e) { try {//from ww w. ja v a2 s .c om iconMenu = new JPopupMenu(); JMenuItem loadItem = new JMenuItem("Load"); iconMenu.add(loadItem); loadItem.setHorizontalTextPosition(JMenuItem.RIGHT); loadItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String favicon$ = "http://www.google.com/s2/favicons?domain=" + addressField.getText(); URL url = new URL(favicon$); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); ImageIcon icon = new ImageIcon(ImageIO.read(input)); int type = BufferedImage.TYPE_INT_RGB; BufferedImage out = new BufferedImage(24, 24, type); Color background = JWeblinkEditor.this.getBackground(); Graphics2D g2 = out.createGraphics(); g2.setBackground(background); g2.clearRect(0, 0, 24, 24); Image image = icon.getImage(); g2.drawImage(image, 4, 4, null); g2.dispose(); icon = new ImageIcon(out); iconIcon.setIcon(icon); input.close(); } catch (Exception ee) { Logger.getLogger(getClass().getName()).info(ee.toString()); } } }); JMenuItem setItem = new JMenuItem("Set"); iconMenu.add(setItem); setItem.setHorizontalTextPosition(JMenuItem.RIGHT); setItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("WeblinkEditor:set icon"); JIconSelector is = new JIconSelector(); String isLocator$ = is.getLocator(); if (entihome$ != null) isLocator$ = Locator.append(isLocator$, Entigrator.ENTIHOME, entihome$); if (entityKey$ != null) isLocator$ = Locator.append(isLocator$, EntityHandler.ENTITY_KEY, entityKey$); String responseLocator$ = getLocator(); responseLocator$ = Locator.append(responseLocator$, JRequester.REQUESTER_ACTION, ACTION_SET_ICON); responseLocator$ = Locator.append(responseLocator$, BaseHandler.HANDLER_METHOD, "response"); isLocator$ = Locator.append(isLocator$, JRequester.REQUESTER_RESPONSE_LOCATOR, Locator.compressText(responseLocator$)); JConsoleHandler.execute(console, isLocator$); } }); iconMenu.show(e.getComponent(), e.getX(), e.getY()); } catch (Exception ee) { Logger.getLogger(getClass().getName()).severe(ee.toString()); } }
From source file:au.com.gaiaresources.bdrs.controller.test.TestDataCreator.java
private byte[] getAndScaleImageData(int targetWidth, int targetHeight, File file) throws IOException { if (file == null) { return null; }/*from w w w. ja v a2 s. c om*/ BufferedImage sourceImage = ImageIO.read(file); BufferedImage targetImage; if (targetWidth > -1 && targetHeight > -1) { // Resize the image as required to fit the space targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2_scaled = targetImage.createGraphics(); // Better scaling g2_scaled.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2_scaled.setBackground(Color.WHITE); g2_scaled.clearRect(0, 0, targetWidth, targetHeight); int sourceWidth = sourceImage.getWidth(); int sourceHeight = sourceImage.getHeight(); double widthRatio = (double) targetWidth / (double) sourceWidth; double heightRatio = (double) targetHeight / (double) targetHeight; if (heightRatio > widthRatio) { int scaledHeight = (int) Math.round(widthRatio * sourceHeight); g2_scaled.drawImage(sourceImage, 0, (targetImage.getHeight() - scaledHeight) / 2, targetImage.getWidth(), scaledHeight, g2_scaled.getBackground(), null); } else { int scaledWidth = (int) Math.round(heightRatio * sourceWidth); g2_scaled.drawImage(sourceImage, (targetImage.getWidth() - scaledWidth) / 2, 0, scaledWidth, targetImage.getHeight(), new Color(0, 0, 0, 255), null); } } else { targetImage = sourceImage; } ByteArrayOutputStream baos = new ByteArrayOutputStream(targetWidth * targetHeight); ImageIO.write(targetImage, "png", baos); baos.flush(); byte[] data = baos.toByteArray(); baos.close(); return data; }
From source file:edu.umn.cs.spatialHadoop.operations.PyramidPlot.java
private static void plotLocal(Path inFile, Path outFile, OperationsParams params) throws IOException { int tileWidth = params.getInt("tilewidth", 256); int tileHeight = params.getInt("tileheight", 256); Color strokeColor = params.getColor("color", Color.BLACK); String hdfDataset = (String) params.get("dataset"); Shape shape = hdfDataset != null ? new NASARectangle() : (Shape) params.getShape("shape", null); Shape plotRange = params.getShape("rect", null); String valueRangeStr = (String) params.get("valuerange"); MinMax valueRange;/* w w w . ja v a 2 s . c o m*/ 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); } boolean keepAspectRatio = params.is("keep-ratio", true); if (keepAspectRatio) { // Adjust width and height to maintain aspect ratio if (fileMBR.getWidth() > fileMBR.getHeight()) { fileMBR.y1 -= (fileMBR.getWidth() - fileMBR.getHeight()) / 2; fileMBR.y2 = fileMBR.y1 + fileMBR.getWidth(); } else { fileMBR.x1 -= (fileMBR.getHeight() - fileMBR.getWidth() / 2); fileMBR.x2 = fileMBR.x1 + fileMBR.getHeight(); } } 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); } boolean adaptiveSampling = params.getBoolean("sample", false); int numLevels = params.getInt("numlevels", 7); float[] levelProb = new float[numLevels]; double[] scale2 = new double[numLevels]; double[] scale = new double[numLevels]; levelProb[0] = params.getFloat(GeometricPlot.AdaptiveSampleRatio, 0.1f); // Size of the whole file in pixels at the f scale2[0] = (double) tileWidth * tileHeight / (fileMBR.getWidth() * fileMBR.getHeight()); scale[0] = Math.sqrt(scale2[0]); for (int level = 1; level < numLevels; level++) { levelProb[level] = levelProb[level - 1] * 4; scale2[level] = scale2[level - 1] * (1 << level) * (1 << level); scale[level] = scale[level - 1] * (1 << level); } Map<TileIndex, BufferedImage> tileImages = new HashMap<PyramidPlot.TileIndex, BufferedImage>(); Map<TileIndex, Graphics2D> tileGraphics = new HashMap<PyramidPlot.TileIndex, Graphics2D>(); GridInfo bottomGrid = new GridInfo(fileMBR.x1, fileMBR.y1, fileMBR.x2, fileMBR.y2); bottomGrid.rows = bottomGrid.columns = (int) Math.round(Math.pow(2, numLevels - 1)); TileIndex tileIndex = new TileIndex(); boolean gradualFade = !(shape instanceof Point) && params.getBoolean("fade", false); for (InputSplit split : splits) { ShapeRecordReader<Shape> reader = new ShapeRecordReader<Shape>(params, (FileSplit) split); Rectangle cell = reader.createKey(); while (reader.next(cell, shape)) { Rectangle shapeMBR = shape.getMBR(); if (shapeMBR != null) { int min_level = 0; if (adaptiveSampling) { // Special handling for NASA data double p = Math.random(); // Skip levels that do not satisfy the probability while (min_level < numLevels && p > levelProb[min_level]) min_level++; } java.awt.Rectangle overlappingCells = bottomGrid.getOverlappingCells(shapeMBR); for (tileIndex.level = numLevels - 1; tileIndex.level >= min_level; tileIndex.level--) { if (gradualFade && !(shape instanceof Point)) { double areaInPixels = (shapeMBR.getWidth() + shapeMBR.getHeight()) * scale[tileIndex.level]; if (areaInPixels < 1.0 && Math.round(areaInPixels * 255) < 1.0) { // This shape can be safely skipped as it is too small to be plotted return; } } for (int i = 0; i < overlappingCells.width; i++) { tileIndex.x = i + overlappingCells.x; for (int j = 0; j < overlappingCells.height; j++) { tileIndex.y = j + overlappingCells.y; // Draw in image associated with this tile Graphics2D g; { g = tileGraphics.get(tileIndex); if (g == null) { TileIndex key = tileIndex.clone(); BufferedImage image = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_INT_ARGB); if (tileImages.put(key, image) != null) throw new RuntimeException( "Error! Image is already there but graphics is not " + tileIndex); Color bg_color = new Color(0, 0, 0, 0); try { g = image.createGraphics(); } catch (Throwable e) { g = new SimpleGraphics(image); } g.setBackground(bg_color); g.clearRect(0, 0, tileWidth, tileHeight); g.setColor(strokeColor); // Coordinates of this tile in image coordinates g.translate(-(tileWidth * tileIndex.x), -(tileHeight * tileIndex.y)); tileGraphics.put(key, g); } } shape.draw(g, fileMBR, tileWidth * (1 << tileIndex.level), tileHeight * (1 << tileIndex.level), scale2[tileIndex.level]); } } // Shrink overlapping cells to match the upper level int updatedX1 = overlappingCells.x / 2; int updatedY1 = overlappingCells.y / 2; int updatedX2 = (overlappingCells.x + overlappingCells.width - 1) / 2; int updatedY2 = (overlappingCells.y + overlappingCells.height - 1) / 2; overlappingCells.x = updatedX1; overlappingCells.y = updatedY1; overlappingCells.width = updatedX2 - updatedX1 + 1; overlappingCells.height = updatedY2 - updatedY1 + 1; } } } reader.close(); } // Write image to output for (Map.Entry<TileIndex, Graphics2D> tileGraph : tileGraphics.entrySet()) { tileGraph.getValue().dispose(); } FileSystem outFS = outFile.getFileSystem(params); for (Map.Entry<TileIndex, BufferedImage> tileImage : tileImages.entrySet()) { tileIndex = tileImage.getKey(); BufferedImage image = tileImage.getValue(); 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); tileIndex.y = ((1 << tileIndex.level) - 1) - tileIndex.y; } Path imagePath = new Path(outFile, tileIndex.getImageFileName()); FSDataOutputStream outStream = outFS.create(imagePath); ImageIO.write(image, "png", outStream); outStream.close(); } }
From source file:SimpleBufferedImageDemo.java
public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; if (display) { if (buffered) { BufferedImage bi = (BufferedImage) createImage(getWidth(), getHeight()); // Draw into the memory buffer. for (int i = 0; i < getWidth(); i = i + displayImage.getWidth(this)) { for (int j = 0; j < getHeight(); j = j + displayImage.getHeight(this)) { bi.createGraphics().drawImage(displayImage, i, j, this); }/* w w w . j av a2 s . c om*/ } // Draw the buffered Image on to the screen g2D.drawImage(bi, 0, 0, this); } // This block of code draws the texture directly onto the screen. else if (!buffered) { for (int i = 0; i < getWidth(); i = i + displayImage.getWidth(this)) { for (int j = 0; j < getHeight(); j = j + displayImage.getHeight(this)) { g2D.drawImage(displayImage, i, j, this); } } } display = false; } else if (clear) { g2D.setColor(Color.white); g2D.clearRect(0, 0, getWidth(), getHeight()); clear = false; } }
From source file:de.tor.tribes.ui.panels.MinimapPanel.java
@Override public void paintComponent(Graphics g) { super.paintComponent(g); try {/*from w ww. j a va 2 s .c o m*/ Graphics2D g2d = (Graphics2D) g; g2d.clearRect(0, 0, getWidth(), getHeight()); g2d.drawImage(mBuffer, 0, 0, null); if (iCurrentView == ID_MINIMAP) { g2d.setColor(Color.YELLOW); int mapWidth = rVisiblePart.width; int mapHeight = rVisiblePart.height; int w = (int) Math.rint(((double) getWidth() / mapWidth) * (double) iWidth); int h = (int) Math.rint(((double) getHeight() / mapHeight) * (double) iHeight); double posX = ((double) getWidth() / mapWidth * (double) (iX - rVisiblePart.x)) - w / 2; double posY = ((double) getHeight() / mapHeight * (double) (iY - rVisiblePart.y)) - h / 2; g2d.drawRect((int) Math.rint(posX), (int) Math.rint(posY), w, h); if (iCurrentCursor == ImageManager.CURSOR_SHOT) { if (rDrag != null) { g2d.setColor(Color.ORANGE); g2d.drawRect((int) rDrag.getMinX(), (int) rDrag.getMinY(), (int) (rDrag.getWidth() - rDrag.getX()), (int) (rDrag.getHeight() - rDrag.getY())); } } else if (iCurrentCursor == ImageManager.CURSOR_ZOOM) { if (rDrag != null) { g2d.setColor(Color.CYAN); g2d.drawRect((int) rDrag.getX(), (int) rDrag.getY(), (int) (rDrag.getWidth() - rDrag.getX()), (int) ((rDrag.getWidth() - rDrag.getX()) * ((double) getHeight()) / getWidth())); } } } if (showControls) { //g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f)); Rectangle r = minimapButtons.get(ID_MINIMAP); g2d.setColor(Color.WHITE); Point menuPos = r.getLocation(); menuPos.translate(-2, -2); //draw border g2d.fillRect(menuPos.x, menuPos.y, 88, 30); g2d.setColor(Color.BLACK); //check if mouse is inside minimap button if (getMousePosition() != null && r.contains(getMousePosition())) { g2d.setColor(Color.YELLOW); g2d.fillRect(r.x, r.y, r.width, r.height); g2d.setColor(Color.BLACK); } g2d.drawImage(minimapIcons.get(ID_MINIMAP), r.x, r.y, null); g2d.drawRect(r.x, r.y, r.width, r.height); r = minimapButtons.get(ID_ALLY_CHART); //check if mouse is inside ally chart button if (getMousePosition() != null && r.contains(getMousePosition())) { g2d.setColor(Color.YELLOW); g2d.fillRect(r.x, r.y, r.width, r.height); g2d.setColor(Color.BLACK); } g2d.drawImage(minimapIcons.get(ID_ALLY_CHART), r.x, r.y, null); g2d.drawRect(r.x, r.y, r.width, r.height); r = minimapButtons.get(ID_TRIBE_CHART); //check if mouse is inside tribe chart button if (getMousePosition() != null && r.contains(getMousePosition())) { g2d.setColor(Color.YELLOW); g2d.fillRect(r.x, r.y, r.width, r.height); g2d.setColor(Color.BLACK); } g2d.drawImage(minimapIcons.get(ID_TRIBE_CHART), r.x, r.y, null); g2d.drawRect(r.x, r.y, r.width, r.height); } g2d.dispose(); } catch (Exception e) { logger.error("Failed painting Minimap", e); } }
From source file:biogenesis.Organism.java
public BufferedImage getImage() { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setBackground(Color.BLACK); g.clearRect(0, 0, width, height); for (int i = _segments - 1; i >= 0; i--) { g.setColor(_segColor[i]);/*from w w w.j ava2 s . c o m*/ g.drawLine(x1[i] - x + _centerX, y1[i] - y + _centerY, x2[i] - x + _centerX, y2[i] - y + _centerY); } return image; }
From source file:org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram.java
/** * Paints the component by drawing the chart to fill the entire component, * but allowing for the insets (which will be non-zero if a border has been * set for this component). To increase performance (at the expense of * memory), an off-screen buffer image can be used. * * @param g the graphics device for drawing on. *//*from w ww . j a va 2s .c om*/ public void paintComponent(Graphics g) { super.paintComponent(g); if (this.chart == null) { return; } Graphics2D g2 = (Graphics2D) g.create(); // first determine the size of the chart rendering area... Dimension size = getSize(); Insets insets = getInsets(); Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top, size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom); // work out if scaling is required... boolean scale = false; double drawWidth = available.getWidth(); double drawHeight = available.getHeight(); this.scaleX = 1.0; this.scaleY = 1.0; if (drawWidth < this.minimumDrawWidth) { this.scaleX = drawWidth / this.minimumDrawWidth; drawWidth = this.minimumDrawWidth; scale = true; } else if (drawWidth > this.maximumDrawWidth) { this.scaleX = drawWidth / this.maximumDrawWidth; drawWidth = this.maximumDrawWidth; scale = true; } if (drawHeight < this.minimumDrawHeight) { this.scaleY = drawHeight / this.minimumDrawHeight; drawHeight = this.minimumDrawHeight; scale = true; } else if (drawHeight > this.maximumDrawHeight) { this.scaleY = drawHeight / this.maximumDrawHeight; drawHeight = this.maximumDrawHeight; scale = true; } Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight); // are we using the chart buffer? if (this.useBuffer) { // if buffer is being refreshed, it needs clearing unless it is // new - use the following flag to track this... boolean clearBuffer = true; // do we need to resize the buffer? if ((this.chartBuffer == null) || (this.chartBufferWidth != available.getWidth()) || (this.chartBufferHeight != available.getHeight())) { this.chartBufferWidth = (int) available.getWidth(); this.chartBufferHeight = (int) available.getHeight(); this.chartBuffer = createImage(this.chartBufferWidth, this.chartBufferHeight); // GraphicsConfiguration gc = g2.getDeviceConfiguration(); // this.chartBuffer = gc.createCompatibleImage( // this.chartBufferWidth, this.chartBufferHeight, // Transparency.TRANSLUCENT); this.refreshBuffer = true; clearBuffer = false; // buffer is new, no clearing required } // do we need to redraw the buffer? if (this.refreshBuffer) { this.refreshBuffer = false; // clear the flag Rectangle2D bufferArea = new Rectangle2D.Double(0, 0, this.chartBufferWidth, this.chartBufferHeight); Graphics2D bufferG2 = (Graphics2D) this.chartBuffer.getGraphics(); if (clearBuffer) { bufferG2.clearRect(0, 0, this.chartBufferWidth, this.chartBufferHeight); } if (scale) { AffineTransform saved = bufferG2.getTransform(); AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY); bufferG2.transform(st); this.chart.draw(bufferG2, chartArea, this.anchor, this.info); bufferG2.setTransform(saved); } else { this.chart.draw(bufferG2, bufferArea, this.anchor, this.info); } } g2.drawImage(this.chartBuffer, insets.left, insets.top, this); g2.draw((Shape) activeROI.getShapesList().get(1)); // Dibujar las rois que se encuentren activas } // or redrawing the chart every time... else { AffineTransform saved = g2.getTransform(); g2.translate(insets.left, insets.top); if (scale) { AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY); g2.transform(st); } this.chart.draw(g2, chartArea, this.anchor, this.info); g2.drawImage(this.chartBuffer, insets.left, insets.top, this); // Se pintan las Roi activa drawsROIs(g2); g2.setTransform(saved); } g2.dispose(); this.anchor = null; }