List of usage examples for java.awt Graphics2D drawImage
public abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y);
From source file:DefaultGraphics2D.java
/** * Draws as much of the specified area of the specified image as is currently * available, scaling it on the fly to fit inside the specified area of the * destination drawable surface. Transparent pixels do not affect whatever * pixels are already there./*from w w w . ja v a 2s . co m*/ * <p> * This method returns immediately in all cases, even if the image area to be * drawn has not yet been scaled, dithered, and converted for the current * output device. If the current output representation is not yet complete * then <code>drawImage</code> returns <code>false</code>. As more of the * image becomes available, the process that draws the image notifies the * specified image observer. * <p> * This method always uses the unscaled version of the image to render the * scaled rectangle and performs the required scaling on the fly. It does not * use a cached, scaled version of the image for this operation. Scaling of * the image from source to destination is performed such that the first * coordinate of the source rectangle is mapped to the first coordinate of the * destination rectangle, and the second source coordinate is mapped to the * second destination coordinate. The subimage is scaled and flipped as needed * to preserve those mappings. * * @param img * the specified image to be drawn * @param dx1 * the <i>x</i> coordinate of the first corner of the destination * rectangle. * @param dy1 * the <i>y</i> coordinate of the first corner of the destination * rectangle. * @param dx2 * the <i>x</i> coordinate of the second corner of the destination * rectangle. * @param dy2 * the <i>y</i> coordinate of the second corner of the destination * rectangle. * @param sx1 * the <i>x</i> coordinate of the first corner of the source * rectangle. * @param sy1 * the <i>y</i> coordinate of the first corner of the source * rectangle. * @param sx2 * the <i>x</i> coordinate of the second corner of the source * rectangle. * @param sy2 * the <i>y</i> coordinate of the second corner of the source * rectangle. * @param observer * object to be notified as more of the image is scaled and * converted. * @see java.awt.Image * @see java.awt.image.ImageObserver * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, * int, int, int) * @since JDK1.1 */ public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) { BufferedImage src = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g = src.createGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); src = src.getSubimage(sx1, sy1, sx2 - sx1, sy2 - sy1); return drawImage(src, dx1, dy1, dx2 - dx1, dy2 - dy1, observer); }
From source file:org.tsho.dmc2.core.chart.AbsorbingAreaRenderer.java
private void iterateCurve(int k, Graphics2D g, BufferedImage image, int imageX, int imageY) { double delta = 0.00001;//should be taken from GUI double deltaUpperBound = 0.001; double deltaLowerBound = 0.000001; int index = 0; double t = 0; double t1 = 0; double tnext; Point prevPoint = parameterizedCurvePoint(t); Point nextPoint;/*from www . j av a 2 s . com*/ iterate(prevPoint, k); if (prevPoint.isInsideBounds()) setGridState(prevPoint, -20000 * k); while (t < 1) { double delta1 = delta; boolean isDeltaFound = false; boolean isPreviousDistanceTooSmall = false; //boolean ignoreDistanceTooSmall=false; if (prevPoint.isInsideBounds()) { while (!isDeltaFound && delta1 < deltaUpperBound && delta1 > deltaLowerBound) { t1 = t + delta1; nextPoint = parameterizedCurvePoint(t1); iterate(nextPoint, k); if (pixelDistance(prevPoint, nextPoint) == 0) { delta1 = delta1 * 2; isPreviousDistanceTooSmall = true; } else { if (pixelDistance(prevPoint, nextPoint) > 1 && isPreviousDistanceTooSmall) { delta1 = delta1 / 2; isDeltaFound = true; } else { if (pixelDistance(prevPoint, nextPoint) > 1 && !isPreviousDistanceTooSmall) { delta1 = delta1 / 2; } else { if (pixelDistance(prevPoint, nextPoint) <= 1) { isDeltaFound = true; } } } } if (stopped) break; } } if (stopped) break; t = t + delta1; nextPoint = parameterizedCurvePoint(t); iterate(nextPoint, k); prevPoint.set(nextPoint); if (nextPoint.isInsideBounds()) setGridState(nextPoint, -20000 * k); g.drawImage(image, null, imageX, imageY); } }
From source file:edu.harvard.iq.dvn.core.web.ExploreDataPage.java
private BufferedImage getCompositeImage(BufferedImage image) throws FontFormatException, IOException { Integer heightAdjustment = new Integer(0); if (this.heightInt == 1) { heightAdjustment = 40;//from www. j a v a 2s . c om } if (this.heightInt == 3) { heightAdjustment = -100; } BufferedImage yAxisImage = new BufferedImage(100, 500, BufferedImage.TYPE_INT_ARGB); BufferedImage combinedImage = new BufferedImage(776, 575 + heightAdjustment, BufferedImage.TYPE_INT_ARGB); if (graphTitle.trim().isEmpty()) { graphTitle = " "; } File retFile = generateImageString("16", "620x", "South", "0", graphTitle); BufferedImage titleImage = ImageIO.read(retFile); if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) { if (retFile != null && retFile.exists()) { retFile.delete(); } } String source = ""; if (!sourceString.trim().isEmpty()) { source = sourceString; } if (source.trim().isEmpty()) { source = " "; } retFile = generateImageString("14", "676x", "NorthWest", "0", source); BufferedImage sourceImage = ImageIO.read(retFile); if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) { if (retFile != null && retFile.exists()) { retFile.delete(); } } if (yAxisLabel.trim().isEmpty()) { yAxisLabel = " "; } retFile = generateImageString("14", new Integer(350 + heightAdjustment) + "x", "South", "-90", yAxisLabel); BufferedImage yAxisVertImage = ImageIO.read(retFile); if (!FileUtil.keepTempFiles("core.web.ExploreDataPage")) { if (retFile != null && retFile.exists()) { retFile.delete(); } } Graphics2D yag2 = yAxisImage.createGraphics(); Graphics2D cig2 = combinedImage.createGraphics(); Graphics2D sig2 = sourceImage.createGraphics(); cig2.setColor(Color.WHITE); yag2.setColor(Color.WHITE); yag2.fillRect(0, 0, 676, 500); cig2.fillRect(0, 0, 776, 550); cig2.drawImage(yAxisImage, 0, 0, null); cig2.drawImage(yAxisVertImage, 15, 50, null); cig2.drawImage(image, 50, 50, null); cig2.drawImage(titleImage, 85, 15, null); cig2.drawImage(sourceImage, 45, 475 + heightAdjustment, null); yag2.dispose(); sig2.dispose(); cig2.dispose(); return combinedImage; }
From source file:net.geoprism.dashboard.DashboardMap.java
/** * Builds a combined image layer of all the layers in a saved map. * /*from w w w . j a va2 s. c om*/ * @mapWidth * @mapHeight * @orderedLayers * @mapBounds - expects json object {"bottom":"VALUE", "top":"VALUE", "left":"VALUE", "right":"VALUE"} */ public BufferedImage getLayersExportCanvas(int mapWidth, int mapHeight, DashboardLayer[] orderedLayers, String mapBounds) { String bottom; String top; String right; String left; String processingFormat = "png"; // needed to allow transparency on each overlay before combining to a single // map/format Graphics mapBaseGraphic = null; BufferedImage base = null; try { base = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_ARGB); mapBaseGraphic = base.getGraphics(); mapBaseGraphic.drawImage(base, 0, 0, null); // Get bounds of the map try { JSONObject mapBoundsObj = new JSONObject(mapBounds); bottom = mapBoundsObj.getString("bottom"); top = mapBoundsObj.getString("top"); right = mapBoundsObj.getString("right"); left = mapBoundsObj.getString("left"); } catch (JSONException e) { String error = "Could not parse map bounds."; throw new ProgrammingErrorException(error, e); } // Generates map overlays and combines them into a single map image for (DashboardLayer layer : orderedLayers) { // if (layer instanceof DashboardThematicLayer) // { Graphics2D newOverlayBaseGraphic = null; Graphics2D mapLayerGraphic2d = null; String layersString = GeoserverProperties.getWorkspace() + ":" + layer.getViewName(); StringBuffer requestURL = new StringBuffer(); requestURL.append(GeoserverProperties.getLocalPath() + "/wms?"); requestURL.append("LAYERS=" + layersString); requestURL.append("&"); requestURL.append("STYLES="); // there are no geoserver styles being added. sld's are used instead requestURL.append("&"); requestURL.append("SRS=EPSG%3A4326"); requestURL.append("&"); requestURL.append("TRANSPARENT=true"); requestURL.append("&"); requestURL.append("ISBASELAYER=false"); // in the browser the baselayer prop is set for the 1st layer in the // map. requestURL.append("&"); requestURL.append( "SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage"); requestURL.append("&"); requestURL.append("FORMAT=image%2F" + processingFormat); requestURL.append("&"); requestURL.append("BBOX=" + left + "," + bottom + "," + right + "," + top); requestURL.append("&"); requestURL.append("WIDTH=" + Integer.toString(mapWidth)); requestURL.append("&"); requestURL.append("HEIGHT=" + Integer.toString(mapHeight)); try { BufferedImage layerImg = this.getImageFromGeoserver(requestURL.toString()); BufferedImage newOverlayBase = new BufferedImage(mapWidth, mapHeight, BufferedImage.TYPE_INT_ARGB); newOverlayBaseGraphic = newOverlayBase.createGraphics(); // Add transparency to the layerGraphic // This is set in JavaScript in the app so we are replicating browser side transparency settings that are // applied to the whole layer AlphaComposite thisLayerComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, this.getLayerOpacity(layer)); mapLayerGraphic2d = layerImg.createGraphics(); newOverlayBaseGraphic.setComposite(thisLayerComposite); // Add the current layerGraphic to the base image newOverlayBaseGraphic.drawImage(layerImg, 0, 0, null); mapBaseGraphic.drawImage(newOverlayBase, 0, 0, null); } finally { if (newOverlayBaseGraphic != null) { newOverlayBaseGraphic.dispose(); } if (mapLayerGraphic2d != null) { mapLayerGraphic2d.dispose(); } } // } } } finally { mapBaseGraphic.dispose(); } return base; }
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 ww w. j av a2 s .c o m 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; }
From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java
private BufferedImage createFloorImage(HmFolder floor, double scale, int floorWidth, int floorHeight, Map<Long, Integer> channelMap, Map<Long, Integer> colorMap, int borderX, int borderY, double gridSize) throws Exception { BufferedImage image = new BufferedImage(floorWidth + borderX + 1, floorHeight + borderY + 1, BufferedImage.TYPE_INT_ARGB); if (floor == null) { return image; }//from www . j a v a2 s . c o m double metricWidth = 0.0, metricHeight = 0.0, offsetX = 0.0, offsetY = 0.0; int imageWidth = 0; LengthUnit lengthUnit = LengthUnit.METERS; if (null != floor.getMetricWidth()) { metricWidth = floor.getMetricWidth().doubleValue(); } if (null != floor.getMetricHeight()) { metricHeight = floor.getMetricHeight().doubleValue(); } if (null != floor.getImageWidth()) { imageWidth = floor.getImageWidth().intValue(); } if (null != floor.getOffsetX()) { offsetX = floor.getOffsetX().doubleValue(); } if (null != floor.getOffsetY()) { offsetY = floor.getOffsetY().doubleValue(); } if (null != floor.getLengthUnit()) { lengthUnit = floor.getLengthUnit(); } Graphics2D g2 = image.createGraphics(); g2.setStroke(new BasicStroke(1)); if (getDistanceMetric(metricWidth, lengthUnit) == 0) { g2.setColor(new Color(255, 255, 255)); g2.fillRect(borderX, 0, floorWidth + 1, borderY); g2.fillRect(0, 0, borderX, borderY + floorHeight + 1); g2.setColor(new Color(120, 120, 120)); g2.drawLine(0, borderY, floorWidth + borderX, borderY); g2.drawLine(borderX, 0, borderX, floorHeight + borderY); g2.setColor(new Color(255, 255, 204)); g2.fillRect(borderX + 2, borderY + 2, 162, 25); g2.setColor(new Color(0, 51, 102)); g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12)); g2.drawString("Please size this floor plan.", borderX + 8, borderY + 19); return image; } double screenWidth = scale * getDistanceMetric(metricWidth, lengthUnit); double imageScale = screenWidth / imageWidth; int originX = (int) (getDistanceMetric(offsetX, lengthUnit) * scale); int originY = (int) (getDistanceMetric(offsetY, lengthUnit) * scale); g2.setColor(new Color(255, 255, 255)); if (floor.getBackground() != null && floor.getBackground().length() > 0) { LinkedMultiValueMap<String, String> metadata = new LinkedMultiValueMap<>(); String url = getImageBaseUrl(floor.getOwnerId()) + floor.getBackground(); try { BufferedImage map = ImageIO.read(clientFileService.getFile(url, "AFS_TOKEN", metadata)); AffineTransform transform = new AffineTransform(); transform.scale(imageScale, imageScale); g2.drawImage(map, new AffineTransformOp(transform, null), getFloorX(0, borderX, originX), getFloorY(0, borderY, originY)); } catch (Exception e) { logger.error(String.format("image file not found with url: %s", url)); double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit); g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth, (int) screenHeight); } } else { double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit); g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth, (int) screenHeight); } g2.setColor(new Color(204, 204, 204)); // Right edge border g2.drawLine(borderX + floorWidth, borderY + 1, borderX + floorWidth, borderY + floorHeight); // Left edge border (right of tick marks) g2.drawLine(borderX + 1, borderY + floorHeight, borderX + floorWidth, borderY + floorHeight); g2.setColor(new Color(255, 255, 255)); g2.fillRect(borderX, 0, floorWidth + 1, borderY); g2.fillRect(0, 0, borderX, borderY + floorHeight + 1); g2.setColor(new Color(120, 120, 120)); g2.drawLine(0, borderY, floorWidth + borderX, borderY); g2.drawLine(borderX, 0, borderX, floorHeight + borderY); Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12); double actualWidth = floorWidth / scale; double actualHeight = floorHeight / scale; String firstLabel; double unitScale = scale; if (LengthUnit.FEET == lengthUnit) { firstLabel = "0 feet"; actualWidth /= HmFolder.FEET_TO_METERS; actualHeight /= HmFolder.FEET_TO_METERS; unitScale *= HmFolder.FEET_TO_METERS; } else { firstLabel = "0 meters"; } g2.drawString(firstLabel, borderX + 4, 12); double gridX = gridSize; while (gridX < actualWidth) { int x = (int) (gridX * unitScale) + borderX; g2.drawLine(x, 0, x, borderY); boolean label = true; if (gridX + gridSize >= actualWidth) { // Last mark if (x + getNumberPixelWidth(gridX) + 2 > floorWidth) { label = false; } } if (label) { g2.drawString("" + (int) gridX, x + 4, 12); } gridX += gridSize; } double gridY = 0; while (gridY < actualHeight) { int y = (int) (gridY * unitScale) + borderY; g2.drawLine(0, y, borderX, y); double lx = gridY; int dx = 1; for (int bx = borderX; bx >= 16; bx -= 7) { if (lx < 10) { dx += 7; } else { lx /= 10; } } boolean label = true; if (gridY + gridSize >= actualHeight) { // Last mark if (y - borderY + 13 > floorHeight) { label = false; } } if (label) { g2.drawString("" + (int) gridY, dx, y + 13); } gridY += gridSize; } double mapToImage = getMapToMetric(metricWidth, imageWidth, lengthUnit) * scale; if (floor.getPerimeter().size() > 0) { g2.setStroke(new BasicStroke(2)); g2.setColor(new Color(2, 159, 245)); int[] xPoints = new int[floor.getPerimeter().size()]; int[] yPoints = new int[floor.getPerimeter().size()]; int nPoints = 0; int perimId = floor.getPerimeter().get(0).getId(); for (int i = 0; i < floor.getPerimeter().size(); i++) { HmVertex vertex = floor.getPerimeter().get(i); if (vertex.getId() != perimId) { g2.drawPolygon(xPoints, yPoints, nPoints); nPoints = 0; perimId = vertex.getId(); } xPoints[nPoints] = getFloorX((int) (vertex.getX() * mapToImage), borderX, originX); yPoints[nPoints++] = getFloorY((int) (vertex.getY() * mapToImage), borderY, originY); } g2.drawPolygon(xPoints, yPoints, nPoints); } g2.setStroke(new BasicStroke(1)); g2.setColor(new Color(0, 170, 0)); g2.setFont(font.deriveFont(Font.BOLD, 11)); List<HmDeviceLocationEx> devices = deviceLocationExRep.findAllHmDevices(floor.getId()); if (null != devices && !devices.isEmpty()) { for (HmDeviceLocationEx device : devices) { double x = device.getX() * mapToImage; double y = device.getY() * mapToImage; createNodeImage(device.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX), getFloorY((int) y, borderY, originY), g2); } } else { List<HmDevicePlanningEx> plannedDevices = devicePlanningExRep.findAllPlannedDevices(floor.getId()); for (HmDevicePlanningEx plannedDevice : plannedDevices) { double x = plannedDevice.getX() * mapToImage; double y = plannedDevice.getY() * mapToImage; createNodeImage(plannedDevice.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX), getFloorY((int) y, borderY, originY), g2); } } return image; }
From source file:gda.plots.SimpleXYItemRenderer.java
/** * Draws the visual representation of a single data item. This mostly reproduces the code of StandardXYItemRenderer * but using the line by line information stored in the SimpleXYSeries instead of the series indexed information * stored in the Renderer itself./*from w w w. ja va2 s.co m*/ * * @param g2 * the graphics device. * @param state * the renderer state. * @param dataArea * the area within which the data is being drawn. * @param info * collects information about the drawing. * @param plot * the plot (can be used to obtain standard color information etc). * @param domainAxis * the domain axis. * @param rangeAxis * the range axis. * @param dataset * the dataset. * @param series * the series index (zero-based). * @param item * the item index (zero-based). * @param crosshairState * crosshair information for the plot ( <code>null</code> permitted). * @param pass * the pass index. */ @Override public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { SimpleXYSeries sxys = (SimpleXYSeries) ((SimpleXYSeriesCollection) dataset).getSeries(series); if (!sxys.isVisible()) { return; } // setup for collecting optional entity info... Shape entityArea = null; EntityCollection entities = null; if (info != null) { entities = info.getOwner().getEntityCollection(); } PlotOrientation orientation = plot.getOrientation(); g2.setPaint(sxys.getPaint()); g2.setStroke(sxys.getStroke()); // get the data point double x1 = dataset.getXValue(series, item); double y1 = dataset.getYValue(series, item); // Test x1 = xValueTransformer.transformValue(x1); if (Double.isNaN(x1) || Double.isNaN(y1)) { return; } RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); if (sxys.isDrawLines()) { if (item > 0) { // get the previous data point... double x0 = dataset.getXValue(series, item - 1); double y0 = dataset.getYValue(series, item - 1); // Test // System.out.print("tranformed " + x0); x0 = xValueTransformer.transformValue(x0); // Message.debug(" to " + x0); if (!Double.isNaN(x0) && !Double.isNaN(y0)) { boolean drawLine = true; if (getPlotDiscontinuous()) { // only draw a line if the gap between the current and // previous data // point is within the threshold int numX = dataset.getItemCount(series); double minX = dataset.getXValue(series, 0); double maxX = dataset.getXValue(series, numX - 1); drawLine = (x1 - x0) <= ((maxX - minX) / numX * getGapThreshold()); } if (drawLine) { double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation); double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation); // only draw if we have good values if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1) || Double.isNaN(transY1)) { return; } if (orientation == PlotOrientation.HORIZONTAL) { state.workingLine.setLine(transY0, transX0, transY1, transX1); } else if (orientation == PlotOrientation.VERTICAL) { state.workingLine.setLine(transX0, transY0, transX1, transY1); } if (state.workingLine.intersects(dataArea)) { g2.draw(state.workingLine); } } } } } if (sxys.isDrawMarkers()) { Shape shape = sxys.getSymbol(); if (orientation == PlotOrientation.HORIZONTAL) { shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1); } else if (orientation == PlotOrientation.VERTICAL) { shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1); } if (shape.intersects(dataArea)) { g2.setPaint(sxys.getSymbolPaint()); // Always use full stroke for drawing marker g2.setStroke(new BasicStroke()); if (sxys.getFilled()) { g2.fill(shape); } else { g2.draw(shape); } g2.setPaint(sxys.getPaint()); g2.setStroke(sxys.getStroke()); } entityArea = shape; } if (getPlotImages()) { // use shape scale with transform?? // double scale = getShapeScale(plot, series, item, transX1, // transY1); Image image = getImage(plot, series, item, transX1, transY1); if (image != null) { Point hotspot = getImageHotspot(plot, series, item, transX1, transY1, image); g2.drawImage(image, (int) (transX1 - hotspot.getX()), (int) (transY1 - hotspot.getY()), null); entityArea = new Rectangle2D.Double(transX1 - hotspot.getX(), transY1 - hotspot.getY(), image.getWidth(null), image.getHeight(null)); } } // draw the item label if there is one... if (isItemLabelVisible(series, item)) { drawItemLabel(g2, orientation, dataset, series, item, transX1, transY1, (y1 < 0.0)); } updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, orientation); // add an entity for the item... if (entities != null) { addEntity(entities, entityArea, dataset, series, item, transX1, transY1); } }
From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.link_and_brush.LinkAndBrushChartPanel.java
@Override public void paintComponent(Graphics g) { super.paintComponent(g); if (getChart() == null) { return;/* ww w . java 2 s . com*/ } 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(); setChartFieldValue(getChartFieldByName("scaleX"), 1.0); // this.scaleX = 1.0; setChartFieldValue(getChartFieldByName("scaleY"), 1.0); // this.scaleY = 1.0; if (drawWidth < getMinimumDrawWidth()) { setChartFieldValue(getChartFieldByName("scaleX"), drawWidth / getMinimumDrawWidth()); // this.scaleX = drawWidth / getMinimumDrawWidth(); drawWidth = getMinimumDrawWidth(); scale = true; } else if (drawWidth > getMaximumDrawWidth()) { setChartFieldValue(getChartFieldByName("scaleX"), drawWidth / getMaximumDrawWidth()); // this.scaleX = drawWidth / getMaximumDrawWidth(); drawWidth = getMaximumDrawWidth(); scale = true; } if (drawHeight < getMinimumDrawHeight()) { setChartFieldValue(getChartFieldByName("scaleY"), drawHeight / getMinimumDrawHeight()); // this.scaleY = drawHeight / getMinimumDrawHeight(); drawHeight = getMinimumDrawHeight(); scale = true; } else if (drawHeight > getMaximumDrawHeight()) { setChartFieldValue(getChartFieldByName("scaleY"), drawHeight / getMaximumDrawHeight()); // this.scaleY = drawHeight / getMaximumDrawHeight(); drawHeight = getMaximumDrawHeight(); scale = true; } Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight); // are we using the chart buffer? if ((Boolean) getChartFieldValueByName("useBuffer")) { // do we need to resize the buffer? if ((getChartFieldValueByName("chartBuffer") == null) || ((Integer) getChartFieldValueByName("chartBufferWidth") != available.getWidth()) || ((Integer) getChartFieldValueByName("chartBufferHeight") != available.getHeight())) { setChartFieldValue(getChartFieldByName("chartBufferWidth"), (int) available.getWidth()); // this.chartBufferWidth = (int) available.getWidth(); setChartFieldValue(getChartFieldByName("chartBufferHeight"), (int) available.getHeight()); // this.chartBufferHeight = (int) available.getHeight(); GraphicsConfiguration gc = g2.getDeviceConfiguration(); setChartFieldValue(getChartFieldByName("chartBuffer"), gc.createCompatibleImage((Integer) getChartFieldValueByName("chartBufferWidth"), (Integer) getChartFieldValueByName("chartBufferHeight"), Transparency.TRANSLUCENT)); // this.chartBuffer = gc.createCompatibleImage(this.chartBufferWidth, // this.chartBufferHeight, Transparency.TRANSLUCENT); setRefreshBuffer(true); } // do we need to redraw the buffer? if (getRefreshBuffer()) { setRefreshBuffer(false); // clear the flag Rectangle2D bufferArea = new Rectangle2D.Double(0, 0, (Integer) getChartFieldValueByName("chartBufferWidth"), (Integer) getChartFieldValueByName("chartBufferHeight")); Graphics2D bufferG2 = (Graphics2D) ((Image) getChartFieldValueByName("chartBuffer")).getGraphics(); Rectangle r = new Rectangle(0, 0, (Integer) getChartFieldValueByName("chartBufferWidth"), (Integer) getChartFieldValueByName("chartBufferHeight")); bufferG2.setPaint(getBackground()); bufferG2.fill(r); if (scale) { AffineTransform saved = bufferG2.getTransform(); AffineTransform st = AffineTransform.getScaleInstance( (Double) getChartFieldValueByName("scaleX"), (Double) getChartFieldValueByName("scaleY")); bufferG2.transform(st); getChart().draw(bufferG2, chartArea, getAnchor(), getChartRenderingInfo()); bufferG2.setTransform(saved); } else { getChart().draw(bufferG2, bufferArea, getAnchor(), getChartRenderingInfo()); } } // zap the buffer onto the panel... g2.drawImage((Image) getChartFieldValueByName("chartBuffer"), insets.left, insets.top, this); } // or redrawing the chart every time... else { AffineTransform saved = g2.getTransform(); g2.translate(insets.left, insets.top); if (scale) { AffineTransform st = AffineTransform.getScaleInstance((Double) getChartFieldValueByName("scaleX"), (Double) getChartFieldValueByName("scaleY")); g2.transform(st); } getChart().draw(g2, chartArea, getAnchor(), getChartRenderingInfo()); g2.setTransform(saved); } Iterator iterator = ((List) getChartFieldValueByName("overlays")).iterator(); while (iterator.hasNext()) { Overlay overlay = (Overlay) iterator.next(); overlay.paintOverlay(g2, this); } // redraw the zoom rectangle (if present) - if useBuffer is false, // we use XOR so we can XOR the rectangle away again without redrawing // the chart drawZoomRectangle(g2, !(Boolean) getChartFieldValueByName("useBuffer")); g2.dispose(); setAnchor(null); setVerticalTraceLine(null); setHorizontalTraceLine(null); }
From source file:com.isti.traceview.common.TraceViewChartPanel.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. * //from w w w .j a v a 2 s. c o m * @param g * the graphics device for drawing on. */ 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) { // 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 = new BufferedImage(this.chartBufferWidth, this.chartBufferHeight, BufferedImage.TYPE_INT_RGB); //this.chartBuffer = createImage(this.chartBufferWidth, this.chartBufferHeight); -by Max // GraphicsConfiguration gc = g2.getDeviceConfiguration(); // this.chartBuffer = gc.createCompatibleImage( // this.chartBufferWidth, this.chartBufferHeight, // Transparency.TRANSLUCENT); this.refreshBuffer = true; } // do we need to redraw the buffer? if (this.refreshBuffer) { Rectangle2D bufferArea = new Rectangle2D.Double(0, 0, this.chartBufferWidth, this.chartBufferHeight); Graphics2D bufferG2 = (Graphics2D) this.chartBuffer.getGraphics(); 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); } this.refreshBuffer = false; } // zap the buffer onto the panel... g2.drawImage(this.chartBuffer, insets.left, insets.top, this); } // 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.setTransform(saved); } // Redraw the zoom rectangle (if present) drawZoomRectangle(g2); g2.dispose(); this.anchor = null; this.verticalTraceLine = null; this.horizontalTraceLine = null; }
From source file:org.rdv.viz.chart.ChartPanel.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 w w . jav a 2 s. c o m*/ 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); } } // zap the buffer onto the panel... g2.drawImage(this.chartBuffer, insets.left, insets.top, this); } // 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.setTransform(saved); } // Redraw the zoom rectangle (if present) drawZoomRectangle(g2); g2.dispose(); this.anchor = null; this.verticalTraceLine = null; this.horizontalTraceLine = null; }