Example usage for android.graphics Paint getAlpha

List of usage examples for android.graphics Paint getAlpha

Introduction

In this page you can find the example usage for android.graphics Paint getAlpha.

Prototype

public int getAlpha() 

Source Link

Document

Helper to getColor() that just returns the color's alpha value.

Usage

From source file:net.droidsolutions.droidcharts.core.renderer.xy.AbstractXYItemRenderer.java

/**
 * Draws a vertical line on the chart to represent a 'range marker'.
 *
 * @param g2  the graphics device.//from w ww .  j a  v a2  s. co  m
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param marker  the marker line.
 * @param dataArea  the axis data area.
 */
public void drawDomainMarker(Canvas g2, XYPlot plot, ValueAxis domainAxis, Marker marker,
        Rectangle2D dataArea) {

    if (marker instanceof ValueMarker) {
        ValueMarker vm = (ValueMarker) marker;
        double value = vm.getValue();
        Range range = domainAxis.getRange();
        if (!range.contains(value)) {
            return;
        }

        double v = domainAxis.valueToJava2D(value, dataArea, plot.getDomainAxisEdge());

        PlotOrientation orientation = plot.getOrientation();
        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 = marker.getPaint();
        int oldAlpha = paint.getAlpha();
        paint.setAlpha(marker.getAlpha());
        Float stroke = marker.getStroke();

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(stroke);

        g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(),
                paint);

        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();

            Paint lPaint = marker.getLabelPaint();
            lPaint.setTypeface(labelFont.getTypeFace());
            lPaint.setTextSize(labelFont.getSize());
            int lOldAlpha = lPaint.getAlpha();
            lPaint.setAlpha(marker.getAlpha());
            Point2D coordinates = calculateDomainMarkerTextAnchorPoint(g2, orientation, dataArea,
                    line.getBounds2D(), marker.getLabelOffset(), LengthAdjustmentType.EXPAND, anchor);
            TextUtilities.drawAlignedString(label, g2, (float) coordinates.getX(), (float) coordinates.getY(),
                    marker.getLabelTextAnchor(), lPaint);
            lPaint.setAlpha(lOldAlpha);
        }
        paint.setAlpha(oldAlpha);
    } else if (marker instanceof IntervalMarker) {
        IntervalMarker im = (IntervalMarker) marker;
        double start = im.getStartValue();
        double end = im.getEndValue();
        Range range = domainAxis.getRange();
        if (!(range.intersects(start, end))) {
            return;
        }

        double start2d = domainAxis.valueToJava2D(start, dataArea, plot.getDomainAxisEdge());
        double end2d = domainAxis.valueToJava2D(end, dataArea, plot.getDomainAxisEdge());
        double low = Math.min(start2d, end2d);
        double high = Math.max(start2d, end2d);

        PlotOrientation orientation = plot.getOrientation();
        Rectangle2D rect = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            // clip top and bottom bounds to data area
            low = Math.max(low, dataArea.getMinY());
            high = Math.min(high, dataArea.getMaxY());
            rect = new Rectangle2D.Double(dataArea.getMinX(), low, dataArea.getWidth(), high - low);
        } else if (orientation == PlotOrientation.VERTICAL) {
            // clip left and right bounds to data area
            low = Math.max(low, dataArea.getMinX());
            high = Math.min(high, dataArea.getMaxX());
            rect = new Rectangle2D.Double(low, dataArea.getMinY(), high - low, dataArea.getHeight());
        }

        Paint p = marker.getPaint();
        int oldAlpha = p.getAlpha();
        p.setAlpha(marker.getAlpha());
        p.setStyle(Paint.Style.FILL);

        g2.drawRect((float) rect.getMinX(), (float) rect.getMinY(), (float) rect.getMaxX(),
                (float) rect.getMaxY(), p);
        p.setAlpha(oldAlpha);
        // now draw the outlines, if visible...
        if (im.getOutlinePaint() != null && im.getOutlineStroke() != null) {
            if (orientation == PlotOrientation.VERTICAL) {
                Line2D line = new Line2D.Double();
                double y0 = dataArea.getMinY();
                double y1 = dataArea.getMaxY();
                Paint oPiant = im.getOutlinePaint();
                oPiant.setStyle(Paint.Style.STROKE);
                int oOldAlpha = oPiant.getAlpha();
                oPiant.setAlpha(marker.getAlpha());
                oPiant.setStrokeWidth(im.getOutlineStroke());

                if (range.contains(start)) {
                    line.setLine(start2d, y0, start2d, y1);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                if (range.contains(end)) {
                    line.setLine(end2d, y0, end2d, y1);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                oPiant.setAlpha(oOldAlpha);
            } else { // PlotOrientation.HORIZONTAL
                Line2D line = new Line2D.Double();
                double x0 = dataArea.getMinX();
                double x1 = dataArea.getMaxX();
                Paint oPiant = im.getOutlinePaint();
                oPiant.setStyle(Paint.Style.STROKE);
                int oOldAlpha = oPiant.getAlpha();
                oPiant.setAlpha(marker.getAlpha());
                oPiant.setStrokeWidth(im.getOutlineStroke());
                if (range.contains(start)) {
                    line.setLine(x0, start2d, x1, start2d);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                if (range.contains(end)) {
                    line.setLine(x0, end2d, x1, end2d);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                oPiant.setAlpha(oOldAlpha);
            }
        }

        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();
            Paint paint = marker.getLabelPaint();
            paint.setTypeface(labelFont.getTypeFace());
            paint.setTextSize(labelFont.getSize());
            Point2D coordinates = calculateDomainMarkerTextAnchorPoint(g2, orientation, dataArea, rect,
                    marker.getLabelOffset(), marker.getLabelOffsetType(), anchor);
            TextUtilities.drawAlignedString(label, g2, (float) coordinates.getX(), (float) coordinates.getY(),
                    marker.getLabelTextAnchor(), paint);
        }

    }

}

From source file:net.droidsolutions.droidcharts.core.renderer.xy.AbstractXYItemRenderer.java

/**
 * Draws a horizontal line across the chart to represent a 'range marker'.
 *
 * @param g2  the graphics device.//from w  ww.  ja  va2s . co  m
 * @param plot  the plot.
 * @param rangeAxis  the range axis.
 * @param marker  the marker line.
 * @param dataArea  the axis data area.
 */
public void drawRangeMarker(Canvas g2, XYPlot plot, ValueAxis rangeAxis, Marker marker, Rectangle2D dataArea) {

    if (marker instanceof ValueMarker) {
        ValueMarker vm = (ValueMarker) marker;
        double value = vm.getValue();
        Range range = rangeAxis.getRange();
        if (!range.contains(value)) {
            return;
        }

        double v = rangeAxis.valueToJava2D(value, dataArea, plot.getRangeAxisEdge());
        PlotOrientation orientation = plot.getOrientation();
        Line2D line = null;
        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);
        }

        Paint oPiant = marker.getPaint();
        oPiant.setStyle(Paint.Style.STROKE);
        int oOldAlpha = oPiant.getAlpha();
        oPiant.setAlpha(marker.getAlpha());
        oPiant.setStrokeWidth(marker.getStroke());
        g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(), (float) line.getY2(),
                oPiant);
        oPiant.setAlpha(oOldAlpha);
        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();
            Paint paint = marker.getLabelPaint();
            paint.setTypeface(labelFont.getTypeFace());
            paint.setTextSize(labelFont.getSize());
            Point2D coordinates = calculateRangeMarkerTextAnchorPoint(g2, orientation, dataArea,
                    line.getBounds2D(), marker.getLabelOffset(), LengthAdjustmentType.EXPAND, anchor);
            TextUtilities.drawAlignedString(label, g2, (float) coordinates.getX(), (float) coordinates.getY(),
                    marker.getLabelTextAnchor(), paint);
        }

    } else if (marker instanceof IntervalMarker) {
        IntervalMarker im = (IntervalMarker) marker;
        double start = im.getStartValue();
        double end = im.getEndValue();
        Range range = rangeAxis.getRange();
        if (!(range.intersects(start, end))) {
            return;
        }

        double start2d = rangeAxis.valueToJava2D(start, dataArea, plot.getRangeAxisEdge());
        double end2d = rangeAxis.valueToJava2D(end, dataArea, plot.getRangeAxisEdge());
        double low = Math.min(start2d, end2d);
        double high = Math.max(start2d, end2d);

        PlotOrientation orientation = plot.getOrientation();
        Rectangle2D rect = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            // clip left and right bounds to data area
            low = Math.max(low, dataArea.getMinX());
            high = Math.min(high, dataArea.getMaxX());
            rect = new Rectangle2D.Double(low, dataArea.getMinY(), high - low, dataArea.getHeight());
        } else if (orientation == PlotOrientation.VERTICAL) {
            // clip top and bottom bounds to data area
            low = Math.max(low, dataArea.getMinY());
            high = Math.min(high, dataArea.getMaxY());
            rect = new Rectangle2D.Double(dataArea.getMinX(), low, dataArea.getWidth(), high - low);
        }

        Paint p = marker.getPaint();
        int oldAlpha = p.getAlpha();
        p.setAlpha(marker.getAlpha());
        p.setStyle(Paint.Style.FILL);

        g2.drawRect((float) rect.getMinX(), (float) rect.getMinY(), (float) rect.getMaxX(),
                (float) rect.getMaxY(), p);
        p.setAlpha(oldAlpha);

        // now draw the outlines, if visible...
        if (im.getOutlinePaint() != null && im.getOutlineStroke() != null) {
            if (orientation == PlotOrientation.VERTICAL) {
                Line2D line = new Line2D.Double();
                double x0 = dataArea.getMinX();
                double x1 = dataArea.getMaxX();
                Paint oPiant = im.getOutlinePaint();
                oPiant.setStyle(Paint.Style.STROKE);
                int oOldAlpha = oPiant.getAlpha();
                oPiant.setAlpha(marker.getAlpha());
                oPiant.setStrokeWidth(im.getOutlineStroke());
                if (range.contains(start)) {
                    line.setLine(x0, start2d, x1, start2d);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                if (range.contains(end)) {
                    line.setLine(x0, end2d, x1, end2d);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                oPiant.setAlpha(oOldAlpha);
            } else { // PlotOrientation.HORIZONTAL
                Line2D line = new Line2D.Double();
                double y0 = dataArea.getMinY();
                double y1 = dataArea.getMaxY();
                Paint oPiant = im.getOutlinePaint();
                oPiant.setStyle(Paint.Style.STROKE);
                int oOldAlpha = oPiant.getAlpha();
                oPiant.setAlpha(marker.getAlpha());
                oPiant.setStrokeWidth(im.getOutlineStroke());
                if (range.contains(start)) {
                    line.setLine(start2d, y0, start2d, y1);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                if (range.contains(end)) {
                    line.setLine(end2d, y0, end2d, y1);
                    g2.drawLine((float) line.getX1(), (float) line.getY1(), (float) line.getX2(),
                            (float) line.getY2(), oPiant);
                }
                oPiant.setAlpha(oOldAlpha);
            }
        }

        String label = marker.getLabel();
        RectangleAnchor anchor = marker.getLabelAnchor();
        if (label != null) {
            Font labelFont = marker.getLabelFont();

            Paint paint = marker.getLabelPaint();
            paint.setTypeface(labelFont.getTypeFace());
            paint.setTextSize(labelFont.getSize());
            Point2D coordinates = calculateRangeMarkerTextAnchorPoint(g2, orientation, dataArea, rect,
                    marker.getLabelOffset(), marker.getLabelOffsetType(), anchor);
            TextUtilities.drawAlignedString(label, g2, (float) coordinates.getX(), (float) coordinates.getY(),
                    marker.getLabelTextAnchor(), paint);
        }
    }
}

From source file:com.google.appinventor.components.runtime.util.NativeOpenStreetMapController.java

private Drawable rasterizeSVG(MapMarker aiMarker, SVG markerSvg) {
    SVG.Svg svg = markerSvg.getRootElement();
    final float density = view.getContext().getResources().getDisplayMetrics().density;
    float height = aiMarker.Height() <= 0 ? getBestGuessHeight(svg) : aiMarker.Height();
    float width = aiMarker.Width() <= 0 ? getBestGuessWidth(svg) : aiMarker.Width();
    float scaleH = height / getBestGuessHeight(svg);
    float scaleW = width / getBestGuessWidth(svg);
    float scale = (float) Math.sqrt(scaleH * scaleH + scaleW * scaleW);

    // update fill color of SVG <path>
    Paint fillPaint = new Paint();
    Paint strokePaint = new Paint();
    PaintUtil.changePaint(fillPaint, aiMarker.FillColor());
    PaintUtil.changePaint(strokePaint, aiMarker.StrokeColor());
    SVG.Length strokeWidth = new SVG.Length(aiMarker.StrokeWidth() / scale);
    for (SVG.SvgObject element : svg.getChildren()) {
        if (element instanceof SVG.SvgConditionalElement) {
            SVG.SvgConditionalElement path = (SVG.SvgConditionalElement) element;
            path.baseStyle.fill = new SVG.Colour(fillPaint.getColor());
            path.baseStyle.fillOpacity = fillPaint.getAlpha() / 255.0f;
            path.baseStyle.stroke = new SVG.Colour(strokePaint.getColor());
            path.baseStyle.strokeOpacity = strokePaint.getAlpha() / 255.0f;
            path.baseStyle.strokeWidth = strokeWidth;
            if (path.style != null) {
                if ((path.style.specifiedFlags & SPECIFIED_FILL) == 0) {
                    path.style.fill = new SVG.Colour(fillPaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_FILL;
                }//w ww.j av a  2 s. c  om
                if ((path.style.specifiedFlags & SPECIFIED_FILL_OPACITY) == 0) {
                    path.style.fillOpacity = fillPaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_FILL_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE) == 0) {
                    path.style.stroke = new SVG.Colour(strokePaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_STROKE;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_OPACITY) == 0) {
                    path.style.strokeOpacity = strokePaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_WIDTH) == 0) {
                    path.style.strokeWidth = strokeWidth;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_WIDTH;
                }
            }
        }
    }

    // draw SVG to Picture and create a BitmapDrawable for rendering
    Picture picture = markerSvg.renderToPicture();
    Picture scaledPicture = new Picture();
    Canvas canvas = scaledPicture.beginRecording((int) ((width + 2.0f * aiMarker.StrokeWidth()) * density),
            (int) ((height + 2.0f * aiMarker.StrokeWidth()) * density));
    canvas.scale(density * scaleW, density * scaleH);
    canvas.translate(strokeWidth.floatValue(), strokeWidth.floatValue());
    picture.draw(canvas);
    scaledPicture.endRecording();
    return new PictureDrawable(scaledPicture);
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

private void doDraw(Canvas canvas) {
    Paint p = mPaint;
    Rect r = mRect;/*from  w ww  . ja v a2  s.  c  o m*/

    if (mFutureBgColor != 0) {
        drawBgColors(r, canvas, p);
    }
    drawGridBackground(r, canvas, p);
    drawHours(r, canvas, p);

    // Draw each day
    int cell = mFirstJulianDay;
    p.setAntiAlias(false);
    int alpha = p.getAlpha();
    p.setAlpha(mEventsAlpha);
    for (int day = 0; day < mNumDays; day++, cell++) {

        // events on every call.
        drawEvents(cell, day, HOUR_GAP, canvas, p);
        // If this is today
        if (cell == mTodayJulianDay) {
            int lineY = mCurrentTime.hour * (mCellHeight + HOUR_GAP)
                    + ((mCurrentTime.minute * mCellHeight) / 60) + 1;

            // And the current time shows up somewhere on the screen
            if (lineY >= mViewStartY && lineY < mViewStartY + mViewHeight - 2) {
                drawCurrentTimeLine(r, day, lineY, canvas, p);
            }
        }
    }
    p.setAntiAlias(true);
    p.setAlpha(alpha);

    // drawSelectedRect(r, canvas, p);
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

private void drawEvents(int date, int dayIndex, int top, Canvas canvas, Paint p) {
    Paint eventTextPaint = mEventTextPaint;
    int left = computeDayLeftPosition(dayIndex) + 1;
    int cellWidth = computeDayLeftPosition(dayIndex + 1) - left + 1;
    int cellHeight = mCellHeight;

    // Use the selected hour as the selection region
    Rect selectionArea = mSelectionRect;
    selectionArea.top = top + mSelectionHour * (cellHeight + HOUR_GAP);
    selectionArea.bottom = selectionArea.top + cellHeight;
    selectionArea.left = left;/*  ww w . j av  a  2s.  c  o  m*/
    selectionArea.right = selectionArea.left + cellWidth;

    final ArrayList<Event> events = mEvents;
    int numEvents = events.size();
    EventGeometry geometry = mEventGeometry;

    final int viewEndY = mViewStartY + mViewHeight - DAY_HEADER_HEIGHT;

    int alpha = eventTextPaint.getAlpha();
    eventTextPaint.setAlpha(mEventsAlpha);
    for (int i = 0; i < numEvents; i++) {
        Event event = events.get(i);

        if (!geometry.computeEventRect(date, left, top, cellWidth, event)) {
            continue;
        }

        // Don't draw it if it is not visible
        if (event.bottom < mViewStartY || event.top > viewEndY) {
            continue;
        }

        if (date == mSelectionDay && mComputeSelectedEvents
                && geometry.eventIntersectsSelection(event, selectionArea)) {
            mSelectedEvents.add(event);
        }

        Rect r = drawEventRect(event, canvas, p, eventTextPaint, mViewStartY, viewEndY);
        setupTextRect(r);

        // Don't draw text if it is not visible
        if (r.top > viewEndY || r.bottom < mViewStartY) {
            continue;
        }
        StaticLayout layout = getEventLayout(mLayouts, i, event, eventTextPaint, r);
        drawEventText(layout, r, canvas, mViewStartY + 4, mViewStartY + mViewHeight - DAY_HEADER_HEIGHT, false);
    }
    eventTextPaint.setAlpha(alpha);
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

private Rect drawEventRect(Event event, Canvas canvas, Paint p, Paint eventTextPaint, int visibleTop,
        int visibleBot) {
    // Draw the Event Rect
    Rect r = mRect;//from ww w .j  ava  2s  .c  o  m
    r.top = Math.max((int) event.top + EVENT_RECT_TOP_MARGIN, visibleTop);
    r.bottom = Math.min((int) event.bottom - EVENT_RECT_BOTTOM_MARGIN, visibleBot);
    r.left = (int) event.left + EVENT_RECT_LEFT_MARGIN;
    r.right = (int) event.right;

    int color;
    if (event == mClickedEvent) {
        color = mClickedColor;
    } else {
        color = event.color;
    }

    p.setStyle(Style.FILL_AND_STROKE);
    p.setAntiAlias(false);

    int floorHalfStroke = (int) Math.floor(EVENT_RECT_STROKE_WIDTH / 2.0f);
    int ceilHalfStroke = (int) Math.ceil(EVENT_RECT_STROKE_WIDTH / 2.0f);
    r.top = Math.max((int) event.top + EVENT_RECT_TOP_MARGIN + floorHalfStroke, visibleTop);
    r.bottom = Math.min((int) event.bottom - EVENT_RECT_BOTTOM_MARGIN - ceilHalfStroke, visibleBot);
    r.left += floorHalfStroke;
    r.right -= ceilHalfStroke;
    p.setStrokeWidth(EVENT_RECT_STROKE_WIDTH);
    p.setColor(color);
    int alpha = p.getAlpha();
    p.setAlpha(mEventsAlpha);
    canvas.drawRect(r, p);
    p.setAlpha(alpha);
    p.setStyle(Style.FILL);

    // If this event is selected, then use the selection color
    if (mSelectedEvent == event && mClickedEvent != null) {
        boolean paintIt = false;
        color = 0;

        if (paintIt) {
            p.setColor(color);
            canvas.drawRect(r, p);
        }
        p.setAntiAlias(true);
    }

    // Setup rect for drawEventText which follows
    r.top = (int) event.top + EVENT_RECT_TOP_MARGIN;
    r.bottom = (int) event.bottom - EVENT_RECT_BOTTOM_MARGIN;
    r.left = (int) event.left + EVENT_RECT_LEFT_MARGIN;
    r.right = (int) event.right - EVENT_RECT_RIGHT_MARGIN;
    return r;
}

From source file:net.droidsolutions.droidcharts.core.plot.XYPlot.java

/**
 * Draws the quadrants./*from  ww w.j a  va  2 s. c om*/
 * 
 * @param g2
 *            the graphics device.
 * @param area
 *            the area.
 * 
 * @see #setQuadrantOrigin(Point2D)
 * @see #setQuadrantPaint(int, Paint)
 */
protected void drawQuadrants(Canvas g2, Rectangle2D area) {
    // 0 | 1
    // --+--
    // 2 | 3
    boolean somethingToDraw = false;

    ValueAxis xAxis = getDomainAxis();
    if (xAxis == null) { // we can't draw quadrants without a valid x-axis
        return;
    }
    double x = xAxis.getRange().constrain(this.quadrantOrigin.getX());
    double xx = xAxis.valueToJava2D(x, area, getDomainAxisEdge());

    ValueAxis yAxis = getRangeAxis();
    if (yAxis == null) { // we can't draw quadrants without a valid y-axis
        return;
    }
    double y = yAxis.getRange().constrain(this.quadrantOrigin.getY());
    double yy = yAxis.valueToJava2D(y, area, getRangeAxisEdge());

    double xmin = xAxis.getLowerBound();
    double xxmin = xAxis.valueToJava2D(xmin, area, getDomainAxisEdge());

    double xmax = xAxis.getUpperBound();
    double xxmax = xAxis.valueToJava2D(xmax, area, getDomainAxisEdge());

    double ymin = yAxis.getLowerBound();
    double yymin = yAxis.valueToJava2D(ymin, area, getRangeAxisEdge());

    double ymax = yAxis.getUpperBound();
    double yymax = yAxis.valueToJava2D(ymax, area, getRangeAxisEdge());

    Rectangle2D[] r = new Rectangle2D[] { null, null, null, null };
    if (this.quadrantPaint[0] != null) {
        if (x > xmin && y < ymax) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[0] = new Rectangle2D.Double(Math.min(yymax, yy), Math.min(xxmin, xx), Math.abs(yy - yymax),
                        Math.abs(xx - xxmin));
            } else { // PlotOrientation.VERTICAL
                r[0] = new Rectangle2D.Double(Math.min(xxmin, xx), Math.min(yymax, yy), Math.abs(xx - xxmin),
                        Math.abs(yy - yymax));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[1] != null) {
        if (x < xmax && y < ymax) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[1] = new Rectangle2D.Double(Math.min(yymax, yy), Math.min(xxmax, xx), Math.abs(yy - yymax),
                        Math.abs(xx - xxmax));
            } else { // PlotOrientation.VERTICAL
                r[1] = new Rectangle2D.Double(Math.min(xx, xxmax), Math.min(yymax, yy), Math.abs(xx - xxmax),
                        Math.abs(yy - yymax));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[2] != null) {
        if (x > xmin && y > ymin) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[2] = new Rectangle2D.Double(Math.min(yymin, yy), Math.min(xxmin, xx), Math.abs(yy - yymin),
                        Math.abs(xx - xxmin));
            } else { // PlotOrientation.VERTICAL
                r[2] = new Rectangle2D.Double(Math.min(xxmin, xx), Math.min(yymin, yy), Math.abs(xx - xxmin),
                        Math.abs(yy - yymin));
            }
            somethingToDraw = true;
        }
    }
    if (this.quadrantPaint[3] != null) {
        if (x < xmax && y > ymin) {
            if (this.orientation == PlotOrientation.HORIZONTAL) {
                r[3] = new Rectangle2D.Double(Math.min(yymin, yy), Math.min(xxmax, xx), Math.abs(yy - yymin),
                        Math.abs(xx - xxmax));
            } else { // PlotOrientation.VERTICAL
                r[3] = new Rectangle2D.Double(Math.min(xx, xxmax), Math.min(yymin, yy), Math.abs(xx - xxmax),
                        Math.abs(yy - yymin));
            }
            somethingToDraw = true;
        }
    }
    if (somethingToDraw) {

        for (int i = 0; i < 4; i++) {
            if (this.quadrantPaint[i] != null && r[i] != null) {
                Paint paint = this.quadrantPaint[i];
                paint.setStyle(Paint.Style.FILL);
                int oldAlpha = paint.getAlpha();
                paint.setAlpha(getBackgroundAlpha());
                g2.drawRect((float) r[i].getMinX(), (float) r[i].getMinY(), (float) r[i].getMaxX(),
                        (float) r[i].getMaxY(), paint);
                paint.setAlpha(oldAlpha);
            }
        }

    }
}

From source file:net.droidsolutions.droidcharts.core.plot.XYPlot.java

/**
 * Draws the plot within the specified area on a graphics device.
 * //w w w  . ja  v a  2 s  .  c  o  m
 * @param g2
 *            the graphics device.
 * @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(Canvas 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);
    createAndAddEntity((Rectangle2D) dataArea.clone(), info, null, null);
    if (info != null) {
        info.setDataArea(dataArea);
    }

    // draw the plot background and axes...
    drawBackground(g2, dataArea);
    Map axisStateMap = drawAxes(g2, area, dataArea, info);

    PlotOrientation orient = getOrientation();

    // the anchor point is typically the point where the mouse last
    // clicked - the crosshairs will be driven off this point...
    if (anchor != null && !dataArea.contains(anchor)) {
        anchor = null;
    }
    CrosshairState crosshairState = new CrosshairState();
    crosshairState.setCrosshairDistance(Double.POSITIVE_INFINITY);
    crosshairState.setAnchor(anchor);

    crosshairState.setAnchorX(Double.NaN);
    crosshairState.setAnchorY(Double.NaN);
    if (anchor != null) {
        ValueAxis domainAxis = getDomainAxis();
        if (domainAxis != null) {
            double x;
            if (orient == PlotOrientation.VERTICAL) {
                x = domainAxis.java2DToValue(anchor.getX(), dataArea, getDomainAxisEdge());
            } else {
                x = domainAxis.java2DToValue(anchor.getY(), dataArea, getDomainAxisEdge());
            }
            crosshairState.setAnchorX(x);
        }
        ValueAxis rangeAxis = getRangeAxis();
        if (rangeAxis != null) {
            double y;
            if (orient == PlotOrientation.VERTICAL) {
                y = rangeAxis.java2DToValue(anchor.getY(), dataArea, getRangeAxisEdge());
            } else {
                y = rangeAxis.java2DToValue(anchor.getX(), dataArea, getRangeAxisEdge());
            }
            crosshairState.setAnchorY(y);
        }
    }
    crosshairState.setCrosshairX(getDomainCrosshairValue());
    crosshairState.setCrosshairY(getRangeCrosshairValue());

    g2.save();
    g2.clipRect((float) dataArea.getMinX(), (float) dataArea.getMinY(), (float) dataArea.getMaxX(),
            (float) dataArea.getMaxY());

    AxisState domainAxisState = (AxisState) axisStateMap.get(getDomainAxis());
    if (domainAxisState == null) {
        if (parentState != null) {
            domainAxisState = (AxisState) parentState.getSharedAxisStates().get(getDomainAxis());
        }
    }

    AxisState rangeAxisState = (AxisState) axisStateMap.get(getRangeAxis());
    if (rangeAxisState == null) {
        if (parentState != null) {
            rangeAxisState = (AxisState) parentState.getSharedAxisStates().get(getRangeAxis());
        }
    }
    if (domainAxisState != null) {
        drawDomainTickBands(g2, dataArea, domainAxisState.getTicks());
    }
    if (rangeAxisState != null) {
        drawRangeTickBands(g2, dataArea, rangeAxisState.getTicks());
    }
    if (domainAxisState != null) {
        drawDomainGridlines(g2, dataArea, domainAxisState.getTicks());
        drawZeroDomainBaseline(g2, dataArea);
    }
    if (rangeAxisState != null) {
        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 (i >= getDatasetCount()) { // we need the dataset to make
                continue; // a link to the axes
            }
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.BACKGROUND, info);
            }
        }

        for (int i = getDatasetCount() - 1; i >= 0; i--) {
            foundData = render(g2, dataArea, i, info, crosshairState) || foundData;
        }

        // draw foreground annotations
        for (int i = rendererCount - 1; i >= 0; i--) {
            XYItemRenderer r = getRenderer(i);
            if (i >= getDatasetCount()) { // we need the dataset to make
                continue; // a link to the axes
            }
            if (r != null) {
                ValueAxis domainAxis = getDomainAxisForDataset(i);
                ValueAxis rangeAxis = getRangeAxisForDataset(i);
                r.drawAnnotations(g2, dataArea, domainAxis, rangeAxis, Layer.FOREGROUND, info);
            }
        }

    }

    // draw domain crosshair if required...
    int xAxisIndex = crosshairState.getDomainAxisIndex();
    ValueAxis xAxis = getDomainAxis(xAxisIndex);
    RectangleEdge xAxisEdge = getDomainAxisEdge(xAxisIndex);
    if (!this.domainCrosshairLockedOnData && anchor != null) {
        double xx;
        if (orient == PlotOrientation.VERTICAL) {
            xx = xAxis.java2DToValue(anchor.getX(), dataArea, xAxisEdge);
        } else {
            xx = xAxis.java2DToValue(anchor.getY(), dataArea, xAxisEdge);
        }
        crosshairState.setCrosshairX(xx);
    }
    setDomainCrosshairValue(crosshairState.getCrosshairX(), false);
    if (isDomainCrosshairVisible()) {
        double x = getDomainCrosshairValue();
        Paint paint = getDomainCrosshairPaint();
        int oldAlpha = paint.getAlpha();
        paint.setAlpha(getForegroundAlpha());
        Float stroke = getDomainCrosshairStroke();
        drawDomainCrosshair(g2, dataArea, orient, x, xAxis, stroke, paint);
        paint.setAlpha(oldAlpha);
    }

    // draw range crosshair if required...
    int yAxisIndex = crosshairState.getRangeAxisIndex();
    ValueAxis yAxis = getRangeAxis(yAxisIndex);
    RectangleEdge yAxisEdge = getRangeAxisEdge(yAxisIndex);
    if (!this.rangeCrosshairLockedOnData && anchor != null) {
        double yy;
        if (orient == PlotOrientation.VERTICAL) {
            yy = yAxis.java2DToValue(anchor.getY(), dataArea, yAxisEdge);
        } else {
            yy = yAxis.java2DToValue(anchor.getX(), dataArea, yAxisEdge);
        }
        crosshairState.setCrosshairY(yy);
    }
    setRangeCrosshairValue(crosshairState.getCrosshairY(), false);
    if (isRangeCrosshairVisible()) {
        double y = getRangeCrosshairValue();
        Paint paint = getRangeCrosshairPaint();
        int oldAlpha = paint.getAlpha();
        paint.setAlpha(getForegroundAlpha());
        Float stroke = getRangeCrosshairStroke();
        drawRangeCrosshair(g2, dataArea, orient, y, yAxis, stroke, paint);
        paint.setAlpha(oldAlpha);
    }

    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.restore();
    drawOutline(g2, dataArea);

}