List of usage examples for java.awt Graphics2D getClip
public abstract Shape getClip();
From source file:org.proteosuite.FastScatterPlot.java
/** * Draws the fast scatter plot on a Java 2D graphics device (such as the * screen or a printer)./*from w ww. jav a 2s . co m*/ * * @param g2 the graphics device. * @param area the area within which the plot (including axis labels) * should be drawn. * @param anchor the anchor point (<code>null</code> permitted). * @param parentState the state from the parent plot, if there is one. * @param info collects chart drawing information (<code>null</code> * permitted). */ public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) { // set up info collection... if (info != null) { info.setPlotArea(area); } // adjust the drawing area for plot insets (if any)... RectangleInsets insets = getInsets(); insets.trim(area); AxisSpace space = new AxisSpace(); space = this.domainAxis.reserveSpace(g2, this, area, RectangleEdge.BOTTOM, space); space = this.rangeAxis.reserveSpace(g2, this, area, RectangleEdge.LEFT, space); Rectangle2D dataArea = space.shrink(area, null); if (info != null) { info.setDataArea(dataArea); } // draw the plot background and axes... drawBackground(g2, dataArea); AxisState domainAxisState = null; AxisState rangeAxisState = null; if (this.domainAxis != null) { domainAxisState = this.domainAxis.draw(g2, dataArea.getMaxY(), area, dataArea, RectangleEdge.BOTTOM, info); } if (this.rangeAxis != null) { rangeAxisState = this.rangeAxis.draw(g2, dataArea.getMinX(), area, dataArea, RectangleEdge.LEFT, info); } drawDomainGridlines(g2, dataArea, domainAxisState.getTicks()); drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks()); Shape originalClip = g2.getClip(); Composite originalComposite = g2.getComposite(); g2.clip(dataArea); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha())); render(g2, dataArea, info, null); g2.setClip(originalClip); g2.setComposite(originalComposite); drawOutline(g2, dataArea); }
From source file:spinworld.gui.RadarPlot.java
/** * Draws the plot on a Java 2D graphics device (such as the screen or a * printer).// w w w. j a v a 2s . c o m * * @param g2 the graphics device. * @param area the area within which the plot should be drawn. * @param anchor the anchor point (<code>null</code> permitted). * @param parentState the state from the parent plot, if there is one. * @param info collects info about the drawing. */ public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) { // adjust for insets... RectangleInsets insets = getInsets(); insets.trim(area); if (info != null) { info.setPlotArea(area); info.setDataArea(area); } drawBackground(g2, area); drawOutline(g2, area); Shape savedClip = g2.getClip(); g2.clip(area); Composite originalComposite = g2.getComposite(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha())); if (!DatasetUtilities.isEmptyOrNull(this.dataset)) { int seriesCount = 0, catCount = 0; if (this.dataExtractOrder == TableOrder.BY_ROW) { seriesCount = this.dataset.getRowCount(); catCount = this.dataset.getColumnCount(); } else { seriesCount = this.dataset.getColumnCount(); catCount = this.dataset.getRowCount(); } // ensure we have origin and maximum value for each axis ensureBoundaryValues(seriesCount, catCount); // Next, setup the plot area // adjust the plot area by the interior spacing value double gapHorizontal = area.getWidth() * getInteriorGap(); double gapVertical = area.getHeight() * getInteriorGap(); double X = area.getX() + gapHorizontal / 2; double Y = area.getY() + gapVertical / 2; double W = area.getWidth() - gapHorizontal; double H = area.getHeight() - gapVertical; double headW = area.getWidth() * this.headPercent; double headH = area.getHeight() * this.headPercent; // make the chart area a square double min = Math.min(W, H) / 2; X = (X + X + W) / 2 - min; Y = (Y + Y + H) / 2 - min; W = 2 * min; H = 2 * min; Point2D centre = new Point2D.Double(X + W / 2, Y + H / 2); Rectangle2D radarArea = new Rectangle2D.Double(X, Y, W, H); // draw the axis and category label for (int cat = 0; cat < catCount; cat++) { double angle = getStartAngle() + (getDirection().getFactor() * cat * 360 / catCount); Point2D endPoint = getWebPoint(radarArea, angle, 1); // 1 = end of axis Line2D line = new Line2D.Double(centre, endPoint); g2.setPaint(this.axisLinePaint); g2.setStroke(this.axisLineStroke); g2.draw(line); if (isAxisTickVisible()) { drawTicks(g2, radarArea, angle, cat); } drawLabel(g2, area, radarArea, 0.0, cat, angle, 360.0 / catCount); } // Now actually plot each of the series polygons.. for (int series = 0; series < seriesCount; series++) { drawRadarPoly(g2, radarArea, centre, info, series, catCount, headH, headW); } } else { drawNoDataMessage(g2, area); } g2.setClip(savedClip); g2.setComposite(originalComposite); drawOutline(g2, area); }
From source file:net.sf.maltcms.chromaui.charts.FastHeatMapPlot.java
/** * Draws the fast scatter plot on a Java 2D graphics device (such as the * screen or a printer).// ww w . ja va 2 s.c om * * @param g2 the graphics device. * @param area the area within which the plot (including axis labels) should * be drawn. * @param anchor the anchor point (<code>null</code> permitted). * @param parentState the state from the parent plot (ignored). * @param info collects chart drawing information (<code>null</code> * permitted). */ @Override public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) { // set up info collection... if (info != null) { info.setPlotArea(area); } // adjust the drawing area for plot insets (if any)... RectangleInsets insets = getInsets(); insets.trim(area); AxisSpace space = new AxisSpace(); space = this.domainAxis.reserveSpace(g2, this, area, RectangleEdge.BOTTOM, space); space = this.rangeAxis.reserveSpace(g2, this, area, RectangleEdge.LEFT, space); Rectangle2D dataArea = space.shrink(area, null); if (info != null) { info.setDataArea(dataArea); } // draw the plot background and axes... drawBackground(g2, dataArea); AxisState domainAxisState = this.domainAxis.draw(g2, dataArea.getMaxY(), area, dataArea, RectangleEdge.BOTTOM, info); AxisState rangeAxisState = this.rangeAxis.draw(g2, dataArea.getMinX(), area, dataArea, RectangleEdge.LEFT, info); drawDomainGridlines(g2, dataArea, domainAxisState.getTicks()); drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks()); Shape originalClip = g2.getClip(); Composite originalComposite = g2.getComposite(); g2.clip(dataArea); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha())); render(g2, dataArea, info, null); g2.setClip(originalClip); g2.setComposite(originalComposite); drawOutline(g2, dataArea); }
From source file:org.tsho.dmc2.core.chart.AbstractDmcPlot.java
/** * Draws the fast scatter plot on a Java 2D graphics device (such as the screen or * a printer).// w w w. jav a 2 s . co m * * @param g2 the graphics device. * @param plotArea the area within which the plot (including axis labels) should be drawn. * @param info collects chart drawing information (<code>null</code> permitted). */ public void draw(Graphics2D g2, Rectangle2D plotArea, PlotState parentState, PlotRenderingInfo info) { // if (data == null) // return; // set up info collection... if (info != null) { info.setPlotArea(plotArea); } // adjust the drawing area for plot insets (if any)... Insets insets = getInsets(); if (insets != null) { plotArea.setRect(plotArea.getX() + insets.left, plotArea.getY() + insets.top, plotArea.getWidth() - insets.left - insets.right, plotArea.getHeight() - insets.top - insets.bottom); } AxisSpace space = new AxisSpace(); space = this.domainAxis.reserveSpace(g2, this, plotArea, RectangleEdge.BOTTOM, space); space = this.rangeAxis.reserveSpace(g2, this, plotArea, RectangleEdge.LEFT, space); Rectangle2D dataArea = space.shrink(plotArea, null); if (info != null) { info.setDataArea(dataArea); } // draw the plot background and axes... drawBackground(g2, dataArea); /* if automatic bounds... */ if (!isNoData()) { if (this instanceof DmcRenderablePlot) { DmcPlotRenderer renderer; renderer = ((DmcRenderablePlot) this).getPlotRenderer(); if (renderer != null) { renderer.initialize(); if (renderer.getState() == DmcPlotRenderer.STATE_STOPPED) { return; } } } } AxisState domainAxisState = null, rangeAxisState = null; if (this.domainAxis != null) { double cursor; cursor = dataArea.getMaxY(); domainAxisState = this.domainAxis.draw(g2, cursor, plotArea, dataArea, RectangleEdge.BOTTOM, info); // cursor = info.getCursor(); } if (this.rangeAxis != null) { double cursor; cursor = dataArea.getMinX(); rangeAxisState = this.rangeAxis.draw(g2, cursor, plotArea, dataArea, RectangleEdge.LEFT, info); } if (drawGridlines == true && domainAxisState != null && rangeAxisState != null) { drawGridlines(g2, dataArea, domainAxisState.getTicks(), rangeAxisState.getTicks()); } Shape originalClip = g2.getClip(); g2.clip(dataArea); // Composite originalComposite = g2.getComposite(); // g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, // getForegroundAlpha())); // g2.setStroke(new BasicStroke(12.0F)); if (isNoData()) { drawNoDataMessage(g2, plotArea); } else { drawPlot(g2, dataArea, info); } g2.setClip(originalClip); drawOutline(g2, dataArea); }
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);/*from w w w . j a v a 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:ucar.unidata.idv.control.chart.MyXYPlot.java
/** * Draws the plot within the specified area on a graphics device. * * @param g2 the graphics device.//from w w w . ja v a2s. co m * @param area the plot area (in Java2D space). * @param anchor an anchor point in Java2D space (<code>null</code> * permitted). * @param parentState the state from the parent plot, if there is one * (<code>null</code> permitted). * @param info collects chart drawing information (<code>null</code> * permitted). */ public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info) { // if the plot area is too small, just return... boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW); boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW); if (b1 || b2) { return; } // record the plot area... if (info != null) { info.setPlotArea(area); } // adjust the drawing area for the plot insets (if any)... RectangleInsets insets = getInsets(); insets.trim(area); AxisSpace space = calculateAxisSpace(g2, area); Rectangle2D dataArea = space.shrink(area, null); this.axisOffset.trim(dataArea); if (info != null) { info.setDataArea(dataArea); } // draw the plot background and axes... drawBackground(g2, dataArea); Map axisStateMap = drawAxes(g2, area, dataArea, info); if ((anchor != null) && !dataArea.contains(anchor)) { anchor = null; } CrosshairState crosshairState = new CrosshairState(); crosshairState.setCrosshairDistance(Double.POSITIVE_INFINITY); crosshairState.setAnchor(anchor); crosshairState.setCrosshairX(getDomainCrosshairValue()); crosshairState.setCrosshairY(getRangeCrosshairValue()); Shape originalClip = g2.getClip(); Composite originalComposite = g2.getComposite(); g2.clip(dataArea); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getForegroundAlpha())); AxisState domainAxisState = (AxisState) axisStateMap.get(getDomainAxis()); if (domainAxisState == null) { if (parentState != null) { domainAxisState = (AxisState) parentState.getSharedAxisStates().get(getDomainAxis()); } } if (domainAxisState != null) { drawDomainTickBands(g2, dataArea, domainAxisState.getTicks()); drawDomainGridlines(g2, dataArea, domainAxisState.getTicks()); } AxisState rangeAxisState = (AxisState) axisStateMap.get(getRangeAxis()); if (rangeAxisState == null) { if (parentState != null) { rangeAxisState = (AxisState) parentState.getSharedAxisStates().get(getRangeAxis()); } } if (rangeAxisState != null) { drawRangeTickBands(g2, dataArea, rangeAxisState.getTicks()); drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks()); drawZeroRangeBaseline(g2, dataArea); } // draw the markers that are associated with a specific renderer... for (int i = 0; i < this.renderers.size(); i++) { drawDomainMarkers(g2, dataArea, i, Layer.BACKGROUND); } for (int i = 0; i < this.renderers.size(); i++) { drawRangeMarkers(g2, dataArea, i, Layer.BACKGROUND); } // now draw annotations and render data items... boolean foundData = false; DatasetRenderingOrder order = getDatasetRenderingOrder(); if (order == DatasetRenderingOrder.FORWARD) { // draw background annotations int rendererCount = this.renderers.size(); for (int i = 0; i < rendererCount; i++) { XYItemRenderer r = getRenderer(i); if (r != null) { ValueAxis domainAxis = getDomainAxisForDataset(i); ValueAxis rangeAxis = getRangeAxisForDataset(i); r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info); } } // render data items... for (int i = 0; i < getDatasetCount(); i++) { foundData = render(g2, dataArea, i, info, crosshairState) || foundData; } // draw foreground annotations for (int i = 0; i < rendererCount; i++) { XYItemRenderer r = getRenderer(i); if (r != null) { ValueAxis domainAxis = getDomainAxisForDataset(i); ValueAxis rangeAxis = getRangeAxisForDataset(i); r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info); } } } else if (order == DatasetRenderingOrder.REVERSE) { // draw background annotations int rendererCount = this.renderers.size(); for (int i = rendererCount - 1; i >= 0; i--) { XYItemRenderer r = getRenderer(i); if (r != null) { ValueAxis domainAxis = getDomainAxisForDataset(i); ValueAxis rangeAxis = getRangeAxisForDataset(i); r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info); } } ucar.unidata.util.Trace.call1("renderData"); for (int i = getDatasetCount() - 1; i >= 0; i--) { foundData = render(g2, dataArea, i, info, crosshairState) || foundData; } ucar.unidata.util.Trace.call2("renderData"); // draw foreground annotations for (int i = rendererCount - 1; i >= 0; i--) { XYItemRenderer r = getRenderer(i); if (r != null) { ValueAxis domainAxis = getDomainAxisForDataset(i); ValueAxis rangeAxis = getRangeAxisForDataset(i); r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info); } } } PlotOrientation orient = getOrientation(); // draw domain crosshair if required... if (!this.domainCrosshairLockedOnData && (anchor != null)) { double xx = getDomainAxis().java2DToValue(anchor.getX(), dataArea, getDomainAxisEdge()); crosshairState.setCrosshairX(xx); } setDomainCrosshairValue(crosshairState.getCrosshairX(), false); if (isDomainCrosshairVisible()) { double x = getDomainCrosshairValue(); Paint paint = getDomainCrosshairPaint(); Stroke stroke = getDomainCrosshairStroke(); if (orient == PlotOrientation.HORIZONTAL) { drawHorizontalLine(g2, dataArea, x, stroke, paint); } else if (orient == PlotOrientation.VERTICAL) { drawVerticalLine(g2, dataArea, x, stroke, paint); } } // draw range crosshair if required... if (!this.rangeCrosshairLockedOnData && (anchor != null)) { double yy = getRangeAxis().java2DToValue(anchor.getY(), dataArea, getRangeAxisEdge()); crosshairState.setCrosshairX(yy); } setRangeCrosshairValue(crosshairState.getCrosshairY(), false); if (isRangeCrosshairVisible() && getRangeAxis().getRange().contains(getRangeCrosshairValue())) { double y = getRangeCrosshairValue(); Paint paint = getRangeCrosshairPaint(); Stroke stroke = getRangeCrosshairStroke(); if (orient == PlotOrientation.HORIZONTAL) { drawVerticalLine(g2, dataArea, y, stroke, paint); } else if (orient == PlotOrientation.VERTICAL) { drawHorizontalLine(g2, dataArea, y, stroke, paint); } } if (!foundData) { drawNoDataMessage(g2, dataArea); } for (int i = 0; i < this.renderers.size(); i++) { drawDomainMarkers(g2, dataArea, i, Layer.FOREGROUND); } for (int i = 0; i < this.renderers.size(); i++) { drawRangeMarkers(g2, dataArea, i, Layer.FOREGROUND); } drawAnnotations(g2, dataArea, info); g2.setClip(originalClip); g2.setComposite(originalComposite); drawOutline(g2, dataArea); }