Java Code Examples for java.awt.Graphics2D#setPaint()

The following examples show how to use java.awt.Graphics2D#setPaint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Chart_1_AbstractCategoryItemRenderer_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Draws a line perpendicular to the range axis.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param axis  the value axis.
 * @param dataArea  the area for plotting data (not yet adjusted for any 3D
 *                  effect).
 * @param value  the value at which the grid line should be drawn.
 * @param paint  the paint (<code>null</code> not permitted).
 * @param stroke  the stroke (<code>null</code> not permitted).
 *
 * @see #drawRangeGridline
 *
 * @since 1.0.13
 */
public void drawRangeLine(Graphics2D g2, CategoryPlot plot, ValueAxis axis,
        Rectangle2D dataArea, double value, Paint paint, Stroke stroke) {

    Range range = axis.getRange();
    if (!range.contains(value)) {
        return;
    }

    PlotOrientation orientation = plot.getOrientation();
    Line2D line = null;
    double v = axis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge());
    if (orientation == PlotOrientation.HORIZONTAL) {
        line = new Line2D.Double(v, dataArea.getMinY(), v,
                dataArea.getMaxY());
    }
    else if (orientation == PlotOrientation.VERTICAL) {
        line = new Line2D.Double(dataArea.getMinX(), v,
                dataArea.getMaxX(), v);
    }

    g2.setPaint(paint);
    g2.setStroke(stroke);
    g2.draw(line);

}
 
Example 2
Source File: TextBlock.java    From pumpernickel with MIT License 6 votes vote down vote up
protected void paintBackground(Graphics2D g, GeneralPath bodyOutline) {
	Paint background = getBackground();
	if (background != null) {

		if (isShadowActive()) {
			Graphics2D g2 = (Graphics2D) g.create();
			double dy = 0;
			Stroke stroke = getStroke();
			if (stroke != null) {
				dy = (ShapeBounds.getBounds(
						stroke.createStrokedShape(bodyOutline)).getHeight() - ShapeBounds
						.getBounds(bodyOutline).getHeight()) / 2;
			}
			g2.translate(0, dy);
			g2.setColor(getBackgroundShadowColor());
			g2.translate(0, 1);
			g2.fill(bodyOutline);
			g2.translate(0, 1);
			g2.fill(bodyOutline);
			g2.dispose();
		}

		g.setPaint(background);
		g.fill(bodyOutline);
	}
}
 
Example 3
Source File: CategoryPlot.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method for drawing a line perpendicular to the range axis (used
 * for crosshairs).
 *
 * @param g2  the graphics device.
 * @param dataArea  the area defined by the axes.
 * @param value  the data value.
 * @param stroke  the line stroke.
 * @param paint  the line paint.
 */
protected void drawRangeLine(Graphics2D g2,
                             Rectangle2D dataArea,
                             double value, Stroke stroke, Paint paint) {

    double java2D = getRangeAxis().valueToJava2D(
        value, dataArea, getRangeAxisEdge()
    );
    Line2D line = null;
    if (this.orientation == PlotOrientation.HORIZONTAL) {
        line = new Line2D.Double(java2D, dataArea.getMinY(), java2D, 
                dataArea.getMaxY());
    }
    else if (this.orientation == PlotOrientation.VERTICAL) {
        line = new Line2D.Double(dataArea.getMinX(), java2D, 
                dataArea.getMaxX(), java2D);
    }
    g2.setStroke(stroke);
    g2.setPaint(paint);
    g2.draw(line);

}
 
Example 4
Source File: SchemeChooserPanel.java    From iBioSim with Apache License 2.0 6 votes vote down vote up
/**
 * draw the gradient for whichever item in the list
 */
@Override
public void paintComponent(Graphics g) {
	
	Color endColor;
	
	if (name.equals("none"))
		endColor = colorsArray[0];
	else 
		endColor = startColor;
	
	setOpaque(false);
       Graphics2D g2d = (Graphics2D) g;
       Paint oldPaint = g2d.getPaint();
       Paint newPaint = new GradientPaint(0, 0, gradientColor, getWidth(), 0, endColor, false);
       g2d.setPaint(newPaint);
       g2d.fillRect(0,0,getWidth(),getHeight());
       g2d.setPaint(oldPaint);
       super.paintComponent(g);
}
 
Example 5
Source File: ComboBoxArrowButtonPainter.java    From seaglass with Apache License 2.0 6 votes vote down vote up
/**
 * Paint the arrows (both up and down, or left and right).
 *
 * @param g      the Graphics2D context to paint with.
 * @param c      the component to paint.
 * @param width  the width.
 * @param height the height.
 */
private void paintArrows(Graphics2D g, JComponent c, int width, int height) {
    int xOffset = width / 2 - 5;
    int yOffset = height / 2 - 3;

    g.translate(xOffset, yOffset);

    Shape s = shapeGenerator.createArrowLeft(0.5, 0.5, 3, 4);

    g.setPaint(getCommonArrowPaint(s, type));
    g.fill(s);

    s = shapeGenerator.createArrowRight(6.5, 0.5, 3, 4);
    g.setPaint(getCommonArrowPaint(s, type));
    g.fill(s);

    g.translate(-xOffset, -yOffset);
}
 
Example 6
Source File: Plot.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the plot outline.  This method will be called during the chart
 * drawing process and is declared public so that it can be accessed by the
 * renderers used by certain subclasses. You shouldn't need to call this
 * method directly.
 *
 * @param g2  the graphics device.
 * @param area  the area within which the plot should be drawn.
 */
public void drawOutline(Graphics2D g2, Rectangle2D area) {
    if (!this.outlineVisible) {
        return;
    }
    if ((this.outlineStroke != null) && (this.outlinePaint != null)) {
        g2.setStroke(this.outlineStroke);
        g2.setPaint(this.outlinePaint);
        Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        g2.draw(area);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved);
    }
}
 
Example 7
Source File: AbstractXYItemRenderer.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Draws a grid line against the range axis.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param axis  the value axis.
 * @param dataArea  the area for plotting data (not yet adjusted for any
 *                  3D effect).
 * @param value  the value at which the grid line should be drawn.
 */
@Override
public void drawDomainGridLine(Graphics2D g2, XYPlot plot, ValueAxis axis,
        Rectangle2D dataArea, double value) {

    Range range = axis.getRange();
    if (!range.contains(value)) {
        return;
    }

    PlotOrientation orientation = plot.getOrientation();
    double v = axis.valueToJava2D(value, dataArea,
            plot.getDomainAxisEdge());
    Line2D line = null;
    if (orientation == PlotOrientation.HORIZONTAL) {
        line = new Line2D.Double(dataArea.getMinX(), v,
                dataArea.getMaxX(), v);
    }
    else if (orientation == PlotOrientation.VERTICAL) {
        line = new Line2D.Double(v, dataArea.getMinY(), v,
                dataArea.getMaxY());
    }

    Paint paint = plot.getDomainGridlinePaint();
    Stroke stroke = plot.getDomainGridlineStroke();
    g2.setPaint(paint != null ? paint : Plot.DEFAULT_OUTLINE_PAINT);
    g2.setStroke(stroke != null ? stroke : Plot.DEFAULT_OUTLINE_STROKE);
    Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
            RenderingHints.VALUE_STROKE_NORMALIZE);
    g2.draw(line);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved);
}
 
Example 8
Source File: SymbolAxis.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the grid bands for an axis that is aligned to the left or
 * right of the data area (that is, a vertical axis).
 *
 * @param g2  the graphics target (<code>null</code> not permitted).
 * @param plotArea  the area within which the plot is drawn (not used here).
 * @param dataArea  the area for the data (to which the axes are aligned,
 *     <code>null</code> not permitted).
 * @param firstGridBandIsDark  True: the first grid band takes the
 *                             color of <CODE>gridBandPaint</CODE>.
 *                             False: the second grid band takes the
 *                             color of <CODE>gridBandPaint</CODE>.
 * @param ticks  a list of ticks (<code>null</code> not permitted).
 */
protected void drawGridBandsVertical(Graphics2D g2, Rectangle2D plotArea,
        Rectangle2D dataArea, boolean firstGridBandIsDark, 
        List ticks) {

    boolean currentGridBandIsDark = firstGridBandIsDark;
    double xx = dataArea.getX();
    double yy1, yy2;

    //gets the outline stroke width of the plot
    double outlineStrokeWidth = 1.0;
    Stroke outlineStroke = getPlot().getOutlineStroke();
    if (outlineStroke != null && outlineStroke instanceof BasicStroke) {
        outlineStrokeWidth = ((BasicStroke) outlineStroke).getLineWidth();
    }

    Iterator iterator = ticks.iterator();
    ValueTick tick;
    Rectangle2D band;
    while (iterator.hasNext()) {
        tick = (ValueTick) iterator.next();
        yy1 = valueToJava2D(tick.getValue() + 0.5d, dataArea,
                RectangleEdge.LEFT);
        yy2 = valueToJava2D(tick.getValue() - 0.5d, dataArea,
                RectangleEdge.LEFT);
        if (currentGridBandIsDark) {
            g2.setPaint(this.gridBandPaint);
        }
        else {
            g2.setPaint(this.gridBandAlternatePaint);
        }
        band = new Rectangle2D.Double(xx + outlineStrokeWidth, 
                Math.min(yy1, yy2), dataArea.getMaxX() - xx 
                - outlineStrokeWidth, Math.abs(yy2 - yy1));
        g2.fill(band);
        currentGridBandIsDark = !currentGridBandIsDark;
    }
}
 
Example 9
Source File: ScrollPanePainter.java    From seaglass with Apache License 2.0 5 votes vote down vote up
private void paintBackground(Graphics2D g, JComponent c, int width, int height) {
    JViewport viewport = ((JScrollPane)c).getViewport();
    if (viewport.isOpaque()) {
        Shape s = shapeGenerator.createRoundRectangle(0, 0, width - 1, height - 1, CornerSize.BORDER);
        g.setPaint(viewport.getBackground());
        g.fill(s);
    }
}
 
Example 10
Source File: Arja_0062_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Draws a grid line against the domain axis.
 * <P>
 * Note that this default implementation assumes that the horizontal axis
 * is the domain axis. If this is not the case, you will need to override
 * this method.
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the area for plotting data (not yet adjusted for any
 *                  3D effect).
 * @param value  the Java2D value at which the grid line should be drawn.
 * @param paint  the paint (<code>null</code> not permitted).
 * @param stroke  the stroke (<code>null</code> not permitted).
 *
 * @see #drawRangeGridline(Graphics2D, CategoryPlot, ValueAxis,
 *     Rectangle2D, double)
 *
 * @since 1.2.0
 */
public void drawDomainLine(Graphics2D g2, CategoryPlot plot,
        Rectangle2D dataArea, double value, Paint paint, Stroke stroke) {

    if (paint == null) {
        throw new IllegalArgumentException("Null 'paint' argument.");
    }
    if (stroke == null) {
        throw new IllegalArgumentException("Null 'stroke' argument.");
    }
    Line2D line = null;
    PlotOrientation orientation = plot.getOrientation();

    if (orientation == PlotOrientation.HORIZONTAL) {
        line = new Line2D.Double(dataArea.getMinX(), value,
                dataArea.getMaxX(), value);
    }
    else if (orientation == PlotOrientation.VERTICAL) {
        line = new Line2D.Double(value, dataArea.getMinY(), value,
                dataArea.getMaxY());
    }

    g2.setPaint(paint);
    g2.setStroke(stroke);
    g2.draw(line);

}
 
Example 11
Source File: Arja_0062_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Draws an item label.
 *
 * @param g2  the graphics device.
 * @param orientation  the orientation.
 * @param dataset  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @param selected  is the item selected?
 * @param x  the x coordinate (in Java2D space).
 * @param y  the y coordinate (in Java2D space).
 * @param negative  indicates a negative value (which affects the item
 *                  label position).
 *
 * @since 1.2.0
 */
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation,
        CategoryDataset dataset, int row, int column, boolean selected,
        double x, double y, boolean negative) {

    CategoryItemLabelGenerator generator = getItemLabelGenerator(row,
            column, selected);
    if (generator != null) {
        Font labelFont = getItemLabelFont(row, column, selected);
        Paint paint = getItemLabelPaint(row, column, selected);
        g2.setFont(labelFont);
        g2.setPaint(paint);
        String label = generator.generateLabel(dataset, row, column);
        ItemLabelPosition position = null;
        if (!negative) {
            position = getPositiveItemLabelPosition(row, column, selected);
        }
        else {
            position = getNegativeItemLabelPosition(row, column, selected);
        }
        Point2D anchorPoint = calculateLabelAnchorPoint(
                position.getItemLabelAnchor(), x, y, orientation);
        TextUtilities.drawRotatedString(label, g2,
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(),
                position.getAngle(), position.getRotationAnchor());
    }

}
 
Example 12
Source File: PeriodAxis.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the major and minor tick marks for an axis that lies at the top or 
 * bottom of the plot.
 * 
 * @param g2  the graphics device.
 * @param state  the axis state.
 * @param dataArea  the data area.
 * @param edge  the edge.
 */
protected void drawTickMarksHorizontal(Graphics2D g2, AxisState state, 
                                       Rectangle2D dataArea, 
                                       RectangleEdge edge) {
    List ticks = new ArrayList();
    double x0 = dataArea.getX();
    double y0 = state.getCursor();
    double insideLength = getTickMarkInsideLength();
    double outsideLength = getTickMarkOutsideLength();
    RegularTimePeriod t = RegularTimePeriod.createInstance(
            this.majorTickTimePeriodClass, this.first.getStart(), 
            getTimeZone());
    long t0 = t.getFirstMillisecond(this.calendar);
    Line2D inside = null;
    Line2D outside = null;
    long firstOnAxis = getFirst().getFirstMillisecond(this.calendar);
    long lastOnAxis = getLast().getLastMillisecond(this.calendar);
    while (t0 <= lastOnAxis) {
        ticks.add(new NumberTick(new Double(t0), "", TextAnchor.CENTER, 
                TextAnchor.CENTER, 0.0));
        x0 = valueToJava2D(t0, dataArea, edge);
        if (edge == RectangleEdge.TOP) {
            inside = new Line2D.Double(x0, y0, x0, y0 + insideLength);  
            outside = new Line2D.Double(x0, y0, x0, y0 - outsideLength);
        }
        else if (edge == RectangleEdge.BOTTOM) {
            inside = new Line2D.Double(x0, y0, x0, y0 - insideLength);
            outside = new Line2D.Double(x0, y0, x0, y0 + outsideLength);
        }
        if (t0 > firstOnAxis) {
            g2.setPaint(getTickMarkPaint());
            g2.setStroke(getTickMarkStroke());
            g2.draw(inside);
            g2.draw(outside);
        }
        // draw minor tick marks
        if (this.minorTickMarksVisible) {
            RegularTimePeriod tminor = RegularTimePeriod.createInstance(
                    this.minorTickTimePeriodClass, new Date(t0), 
                    getTimeZone());
            long tt0 = tminor.getFirstMillisecond(this.calendar);
            while (tt0 < t.getLastMillisecond(this.calendar) 
                    && tt0 < lastOnAxis) {
                double xx0 = valueToJava2D(tt0, dataArea, edge);
                if (edge == RectangleEdge.TOP) {
                    inside = new Line2D.Double(xx0, y0, xx0, 
                            y0 + this.minorTickMarkInsideLength);
                    outside = new Line2D.Double(xx0, y0, xx0, 
                            y0 - this.minorTickMarkOutsideLength);
                }
                else if (edge == RectangleEdge.BOTTOM) {
                    inside = new Line2D.Double(xx0, y0, xx0, 
                            y0 - this.minorTickMarkInsideLength);
                    outside = new Line2D.Double(xx0, y0, xx0, 
                            y0 + this.minorTickMarkOutsideLength);
                }
                if (tt0 >= firstOnAxis) {
                    g2.setPaint(this.minorTickMarkPaint);
                    g2.setStroke(this.minorTickMarkStroke);
                    g2.draw(inside);
                    g2.draw(outside);
                }
                tminor = tminor.next();
                tt0 = tminor.getFirstMillisecond(this.calendar);
            }
        }            
        t = t.next();
        t0 = t.getFirstMillisecond(this.calendar);
    }
    if (edge == RectangleEdge.TOP) {
        state.cursorUp(Math.max(outsideLength, 
                this.minorTickMarkOutsideLength));
    }
    else if (edge == RectangleEdge.BOTTOM) {
        state.cursorDown(Math.max(outsideLength, 
                this.minorTickMarkOutsideLength));
    }
    state.setTicks(ticks);
}
 
Example 13
Source File: VectorRenderer.java    From openstock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the block representing the specified item.
 *
 * @param g2  the graphics device.
 * @param state  the state.
 * @param dataArea  the data area.
 * @param info  the plot rendering info.
 * @param plot  the plot.
 * @param domainAxis  the x-axis.
 * @param rangeAxis  the y-axis.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param crosshairState  the crosshair state.
 * @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) {

    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double dx = 0.0;
    double dy = 0.0;
    if (dataset instanceof VectorXYDataset) {
        dx = ((VectorXYDataset) dataset).getVectorXValue(series, item);
        dy = ((VectorXYDataset) dataset).getVectorYValue(series, item);
    }
    double xx0 = domainAxis.valueToJava2D(x, dataArea,
            plot.getDomainAxisEdge());
    double yy0 = rangeAxis.valueToJava2D(y, dataArea,
            plot.getRangeAxisEdge());
    double xx1 = domainAxis.valueToJava2D(x + dx, dataArea,
            plot.getDomainAxisEdge());
    double yy1 = rangeAxis.valueToJava2D(y + dy, dataArea,
            plot.getRangeAxisEdge());
    Line2D line;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation.equals(PlotOrientation.HORIZONTAL)) {
        line = new Line2D.Double(yy0, xx0, yy1, xx1);
    }
    else {
        line = new Line2D.Double(xx0, yy0, xx1, yy1);
    }
    g2.setPaint(getItemPaint(series, item));
    g2.setStroke(getItemStroke(series, item));
    g2.draw(line);

    // calculate the arrow head and draw it...
    double dxx = (xx1 - xx0);
    double dyy = (yy1 - yy0);
    double bx = xx0 + (1.0 - this.baseLength) * dxx;
    double by = yy0 + (1.0 - this.baseLength) * dyy;

    double cx = xx0 + (1.0 - this.headLength) * dxx;
    double cy = yy0 + (1.0 - this.headLength) * dyy;

    double angle = 0.0;
    if (dxx != 0.0) {
        angle = Math.PI / 2.0 - Math.atan(dyy / dxx);
    }
    double deltaX = 2.0 * Math.cos(angle);
    double deltaY = 2.0 * Math.sin(angle);

    double leftx = cx + deltaX;
    double lefty = cy - deltaY;
    double rightx = cx - deltaX;
    double righty = cy + deltaY;

    GeneralPath p = new GeneralPath();
    if (orientation == PlotOrientation.VERTICAL) {
        p.moveTo((float) xx1, (float) yy1);
        p.lineTo((float) rightx, (float) righty);
        p.lineTo((float) bx, (float) by);
        p.lineTo((float) leftx, (float) lefty);
    }
    else {  // orientation is HORIZONTAL
        p.moveTo((float) yy1, (float) xx1);
        p.lineTo((float) righty, (float) rightx);
        p.lineTo((float) by, (float) bx);
        p.lineTo((float) lefty, (float) leftx);
    }
    p.closePath();
    g2.draw(p);

    // setup for collecting optional entity info...
    EntityCollection entities;
    if (info != null) {
        entities = info.getOwner().getEntityCollection();
        if (entities != null) {
            addEntity(entities, line.getBounds(), dataset, series, item,
                    0.0, 0.0);
        }
    }

}
 
Example 14
Source File: BarRenderer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws an item label.  This method is used for bars instead of
 * {@link #drawItemLabel()} so that the bar can be used to calculate the
 * label anchor point.
 *
 * @param g2  the graphics device.
 * @param dataset  the dataset.
 * @param row  the row.
 * @param column  the column.
 * @param selected  is the item selected?
 * @param plot  the plot.
 * @param generator  the label generator.
 * @param bar  the bar.
 * @param negative  a flag indicating a negative value.
 *
 * @since 1.2.0
 */
protected void drawItemLabelForBar(Graphics2D g2, CategoryPlot plot,
        CategoryDataset dataset, int row, int column, boolean selected,
        CategoryItemLabelGenerator generator, Rectangle2D bar,
        boolean negative) {

    String label = generator.generateLabel(dataset, row, column);
    if (label == null) {
        return;  // nothing to do
    }

    Font labelFont = getItemLabelFont(row, column, selected);
    g2.setFont(labelFont);
    Paint paint = getItemLabelPaint(row, column, selected);
    g2.setPaint(paint);

    // find out where to place the label...
    ItemLabelPosition position = null;
    if (!negative) {
        position = getPositiveItemLabelPosition(row, column, selected);
    }
    else {
        position = getNegativeItemLabelPosition(row, column, selected);
    }

    // work out the label anchor point...
    Point2D anchorPoint = calculateLabelAnchorPoint(
            position.getItemLabelAnchor(), bar, plot.getOrientation());

    if (isInternalAnchor(position.getItemLabelAnchor())) {
        Shape bounds = TextUtilities.calculateRotatedStringBounds(label,
                g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());

        if (bounds != null) {
            if (!bar.contains(bounds.getBounds2D())) {
                if (!negative) {
                    position = getPositiveItemLabelPositionFallback();
                }
                else {
                    position = getNegativeItemLabelPositionFallback();
                }
                if (position != null) {
                    anchorPoint = calculateLabelAnchorPoint(
                            position.getItemLabelAnchor(), bar,
                            plot.getOrientation());
                }
            }
        }

    }

    if (position != null) {
        TextUtilities.drawRotatedString(label, g2,
                (float) anchorPoint.getX(), (float) anchorPoint.getY(),
                position.getTextAnchor(), position.getAngle(),
                position.getRotationAnchor());
    }
}
 
Example 15
Source File: XYBlockRenderer.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the block representing the specified item.
 *
 * @param g2  the graphics device.
 * @param state  the state.
 * @param dataArea  the data area.
 * @param info  the plot rendering info.
 * @param plot  the plot.
 * @param domainAxis  the x-axis.
 * @param rangeAxis  the y-axis.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param crosshairState  the crosshair state.
 * @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) {

    double x = dataset.getXValue(series, item);
    double y = dataset.getYValue(series, item);
    double z = 0.0;
    if (dataset instanceof XYZDataset) {
        z = ((XYZDataset) dataset).getZValue(series, item);
    }
    Paint p = this.paintScale.getPaint(z);
    double xx0 = domainAxis.valueToJava2D(x + this.xOffset, dataArea,
            plot.getDomainAxisEdge());
    double yy0 = rangeAxis.valueToJava2D(y + this.yOffset, dataArea,
            plot.getRangeAxisEdge());
    double xx1 = domainAxis.valueToJava2D(x + this.blockWidth
            + this.xOffset, dataArea, plot.getDomainAxisEdge());
    double yy1 = rangeAxis.valueToJava2D(y + this.blockHeight
            + this.yOffset, dataArea, plot.getRangeAxisEdge());
    Rectangle2D block;
    PlotOrientation orientation = plot.getOrientation();
    if (orientation.equals(PlotOrientation.HORIZONTAL)) {
        block = new Rectangle2D.Double(Math.min(yy0, yy1),
                Math.min(xx0, xx1), Math.abs(yy1 - yy0),
                Math.abs(xx0 - xx1));
    }
    else {
        block = new Rectangle2D.Double(Math.min(xx0, xx1),
                Math.min(yy0, yy1), Math.abs(xx1 - xx0),
                Math.abs(yy1 - yy0));
    }
    g2.setPaint(p);
    g2.fill(block);
    g2.setStroke(new BasicStroke(1.0f));
    g2.draw(block);

    EntityCollection entities = state.getEntityCollection();
    if (entities != null) {
        addEntity(entities, block, dataset, series, item, 0.0, 0.0);
    }

}
 
Example 16
Source File: patch1-Chart-1-jMutRepair_patch1-Chart-1-jMutRepair_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Draws a marker for the domain axis.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param plot  the plot (not <code>null</code>).
 * @param axis  the range axis (not <code>null</code>).
 * @param marker  the marker to be drawn (not <code>null</code>).
 * @param dataArea  the area inside the axes (not <code>null</code>).
 *
 * @see #drawRangeMarker(Graphics2D, CategoryPlot, ValueAxis, Marker,
 *     Rectangle2D)
 */
public void drawDomainMarker(Graphics2D g2,
                             CategoryPlot plot,
                             CategoryAxis axis,
                             CategoryMarker marker,
                             Rectangle2D dataArea) {

    Comparable category = marker.getKey();
    CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this));
    int columnIndex = dataset.getColumnIndex(category);
    if (columnIndex < 0) {
        return;
    }

    final Composite savedComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, marker.getAlpha()));

    PlotOrientation orientation = plot.getOrientation();
    Rectangle2D bounds = null;
    if (marker.getDrawAsLine()) {
        double v = axis.getCategoryMiddle(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Line2D line = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line = new Line2D.Double(dataArea.getMinX(), v,
                    dataArea.getMaxX(), v);
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            line = new Line2D.Double(v, dataArea.getMinY(), v,
                    dataArea.getMaxY());
        }
        g2.setPaint(marker.getPaint());
        g2.setStroke(marker.getStroke());
        g2.draw(line);
        bounds = line.getBounds2D();
    }
    else {
        double v0 = axis.getCategoryStart(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        double v1 = axis.getCategoryEnd(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Rectangle2D area = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            area = new Rectangle2D.Double(dataArea.getMinX(), v0,
                    dataArea.getWidth(), (v1 - v0));
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            area = new Rectangle2D.Double(v0, dataArea.getMinY(),
                    (v1 - v0), dataArea.getHeight());
        }
        g2.setPaint(marker.getPaint());
        g2.fill(area);
        bounds = area;
    }

    String label = marker.getLabel();
    RectangleAnchor anchor = marker.getLabelAnchor();
    if (label != null) {
        Font labelFont = marker.getLabelFont();
        g2.setFont(labelFont);
        g2.setPaint(marker.getLabelPaint());
        Point2D coordinates = calculateDomainMarkerTextAnchorPoint(
                g2, orientation, dataArea, bounds, marker.getLabelOffset(),
                marker.getLabelOffsetType(), anchor);
        TextUtilities.drawAlignedString(label, g2,
                (float) coordinates.getX(), (float) coordinates.getY(),
                marker.getLabelTextAnchor());
    }
    g2.setComposite(savedComposite);
}
 
Example 17
Source File: CategoryStepRenderer.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draw a single data item.
 *
 * @param g2  the graphics device.
 * @param state  the renderer state.
 * @param dataArea  the area in which the data is drawn.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param row  the row index (zero-based).
 * @param column  the column index (zero-based).
 * @param pass  the pass index.
 */
@Override
public void drawItem(Graphics2D g2, CategoryItemRendererState state,
        Rectangle2D dataArea, CategoryPlot plot, CategoryAxis domainAxis,
        ValueAxis rangeAxis, CategoryDataset dataset, int row,
        int column, int pass) {

    // do nothing if item is not visible
    if (!getItemVisible(row, column)) {
        return;
    }

    Number value = dataset.getValue(row, column);
    if (value == null) {
        return;
    }
    PlotOrientation orientation = plot.getOrientation();

    // current data point...
    double x1s = domainAxis.getCategoryStart(column, getColumnCount(),
            dataArea, plot.getDomainAxisEdge());
    double x1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
            dataArea, plot.getDomainAxisEdge());
    double x1e = 2 * x1 - x1s; // or: x1s + 2*(x1-x1s)
    double y1 = rangeAxis.valueToJava2D(value.doubleValue(), dataArea,
            plot.getRangeAxisEdge());
    g2.setPaint(getItemPaint(row, column));
    g2.setStroke(getItemStroke(row, column));

    if (column != 0) {
        Number previousValue = dataset.getValue(row, column - 1);
        if (previousValue != null) {
            // previous data point...
            double previous = previousValue.doubleValue();
            double x0s = domainAxis.getCategoryStart(column - 1,
                    getColumnCount(), dataArea, plot.getDomainAxisEdge());
            double x0 = domainAxis.getCategoryMiddle(column - 1,
                    getColumnCount(), dataArea, plot.getDomainAxisEdge());
            double x0e = 2 * x0 - x0s; // or: x0s + 2*(x0-x0s)
            double y0 = rangeAxis.valueToJava2D(previous, dataArea,
                    plot.getRangeAxisEdge());
            if (getStagger()) {
                int xStagger = row * STAGGER_WIDTH;
                if (xStagger > (x1s - x0e)) {
                    xStagger = (int) (x1s - x0e);
                }
                x1s = x0e + xStagger;
            }
            drawLine(g2, (State) state, orientation, x0e, y0, x1s, y0);
            // extend x0's flat bar

            drawLine(g2, (State) state, orientation, x1s, y0, x1s, y1);
            // upright bar
       }
   }
   drawLine(g2, (State) state, orientation, x1s, y1, x1e, y1);
   // x1's flat bar

   // draw the item labels if there are any...
   if (isItemLabelVisible(row, column)) {
        drawItemLabel(g2, orientation, dataset, row, column, x1, y1,
                (value.doubleValue() < 0.0));
   }

   // add an item entity, if this information is being collected
   EntityCollection entities = state.getEntityCollection();
   if (entities != null) {
       Rectangle2D hotspot = new Rectangle2D.Double();
       if (orientation == PlotOrientation.VERTICAL) {
           hotspot.setRect(x1s, y1, x1e - x1s, 4.0);
       }
       else {
           hotspot.setRect(y1 - 2.0, x1s, 4.0, x1e - x1s);
       }
       addItemEntity(entities, dataset, row, column, hotspot);
   }

}
 
Example 18
Source File: LineBorder.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the border by filling in the reserved space (in black).
 *
 * @param g2  the graphics device.
 * @param area  the area.
 */
@Override
public void draw(Graphics2D g2, Rectangle2D area) {
    double w = area.getWidth();
    double h = area.getHeight();
    // if the area has zero height or width, we shouldn't draw anything
    if (w <= 0.0 || h <= 0.0) {
        return;
    }
    double t = this.insets.calculateTopInset(h);
    double b = this.insets.calculateBottomInset(h);
    double l = this.insets.calculateLeftInset(w);
    double r = this.insets.calculateRightInset(w);
    double x = area.getX();
    double y = area.getY();
    double x0 = x + l / 2.0;
    double x1 = x + w - r / 2.0;
    double y0 = y + h - b / 2.0;
    double y1 = y + t / 2.0;
    g2.setPaint(getPaint());
    g2.setStroke(getStroke());
    Object saved = g2.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, 
            RenderingHints.VALUE_STROKE_NORMALIZE);
    Line2D line = new Line2D.Double();
    if (t > 0.0) {
        line.setLine(x0, y1, x1, y1);
        g2.draw(line);
    }
    if (b > 0.0) {
        line.setLine(x0, y0, x1, y0);
        g2.draw(line);
    }
    if (l > 0.0) {
        line.setLine(x0, y0, x0, y1);
        g2.draw(line);
    }
    if (r > 0.0) {
        line.setLine(x1, y0, x1, y1);
        g2.draw(line);
    }
    g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, saved);
}
 
Example 19
Source File: PiePlot.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws a section label on the right side of the pie chart.
 *
 * @param g2  the graphics device.
 * @param state  the state.
 * @param record  the label record.
 */
protected void drawRightLabel(Graphics2D g2, PiePlotState state,
                              PieLabelRecord record) {

    double anchorX = state.getLinkArea().getMaxX();
    double targetX = anchorX + record.getGap();
    double targetY = record.getAllocatedY();

    if (this.labelLinksVisible) {
        double theta = record.getAngle();
        double linkX = state.getPieCenterX() + Math.cos(theta)
                * state.getPieWRadius() * record.getLinkPercent();
        double linkY = state.getPieCenterY() - Math.sin(theta)
                * state.getPieHRadius() * record.getLinkPercent();
        double elbowX = state.getPieCenterX() + Math.cos(theta)
                * state.getLinkArea().getWidth() / 2.0;
        double elbowY = state.getPieCenterY() - Math.sin(theta)
                * state.getLinkArea().getHeight() / 2.0;
        double anchorY = elbowY;
        g2.setPaint(this.labelLinkPaint);
        g2.setStroke(this.labelLinkStroke);
        PieLabelLinkStyle style = getLabelLinkStyle();
        if (style.equals(PieLabelLinkStyle.STANDARD)) {
            g2.draw(new Line2D.Double(linkX, linkY, elbowX, elbowY));
            g2.draw(new Line2D.Double(anchorX, anchorY, elbowX, elbowY));
            g2.draw(new Line2D.Double(anchorX, anchorY, targetX, targetY));
        }
        else if (style.equals(PieLabelLinkStyle.QUAD_CURVE)) {
            QuadCurve2D q = new QuadCurve2D.Float();
            q.setCurve(targetX, targetY, anchorX, anchorY, elbowX, elbowY);
            g2.draw(q);
            g2.draw(new Line2D.Double(elbowX, elbowY, linkX, linkY));
        }
        else if (style.equals(PieLabelLinkStyle.CUBIC_CURVE)) {
            CubicCurve2D c = new CubicCurve2D .Float();
            c.setCurve(targetX, targetY, anchorX, anchorY, elbowX, elbowY,
                    linkX, linkY);
            g2.draw(c);
        }
    }

    TextBox tb = record.getLabel();
    tb.draw(g2, (float) targetX, (float) targetY, RectangleAnchor.LEFT);

}
 
Example 20
Source File: ImageLibrary.java    From freecol with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Fills a certain rectangle with the image texture.
 * 
 * @param g2 The {@code Graphics} used for painting the border.
 * @param img The {@code BufferedImage} to fill the texture.
 * @param x The x-component of the offset.
 * @param y The y-component of the offset.
 * @param width The width of the rectangle.
 * @param height The height of the rectangle.
 */
public static void fillTexture(Graphics2D g2, BufferedImage img,
                               int x, int y, int width, int height) {
    Rectangle anchor = new Rectangle(
        x, y, img.getWidth(), img.getHeight());
    TexturePaint paint = new TexturePaint(img, anchor);
    g2.setPaint(paint);
    g2.fillRect(x, y, width, height);
}