Example usage for java.awt Graphics2D getColor

List of usage examples for java.awt Graphics2D getColor

Introduction

In this page you can find the example usage for java.awt Graphics2D getColor.

Prototype

public abstract Color getColor();

Source Link

Document

Gets this graphics context's current color.

Usage

From source file:de.codesourcery.planning.swing.DateAxis.java

public BoundingBox render(ITimelineCallback callback, boolean layoutOnly) {
    final Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);/* ww  w. j  a  v a 2s  .  co  m*/
    cal.set(Calendar.MILLISECOND, 0);
    Date currentDate = cal.getTime();

    final Date endDate = duration.addTo(startDate);

    BoundingBox lastLabel = null;

    final int labelSpacing = 10;

    final Graphics2D graphics = callback.getGraphics();
    final int fontHeight = graphics.getFontMetrics().getHeight();

    final double scalingFactor = getXScalingFactor(callback.getBoundingBox());
    final BoundingBox box = callback.getBoundingBox();
    double x = callback.getBoundingBox().getX();
    final int tickToLabelSpacing = 2;
    final int tickLength = fontHeight;
    final int axisHeight = fontHeight + tickLength + tickToLabelSpacing;
    final double xIncrement = Math.floor(tickDuration.toSeconds() * scalingFactor);

    final Color oldColor = graphics.getColor();
    //      
    while (currentDate.compareTo(endDate) <= 0) {
        final int currentX = (int) Math.floor(x);
        if (lastLabel == null || lastLabel.getMaxX() < x) {
            final String labelText = callback.getLabelProvider().getTimelineLabel(currentDate);
            if (!StringUtils.isBlank(labelText)) {
                final Rectangle2D stringBounds = callback.getStringBounds(labelText);

                if (!layoutOnly) {
                    graphics.setColor(Color.BLACK);
                    // draw tick
                    final Stroke oldStroke = graphics.getStroke();
                    graphics.setStroke(new BasicStroke(2.0f));
                    graphics.drawLine(currentX, box.getY() + axisHeight, currentX,
                            box.getY() + fontHeight + tickToLabelSpacing);
                    graphics.setStroke(oldStroke);

                    // draw label
                    callback.drawString(Color.BLACK, currentX, box.getY(), labelText);
                }

                final BoundingBox labelBox = new BoundingBox(currentX, box.getY(),
                        currentX + (int) stringBounds.getWidth() + labelSpacing,
                        box.getY() + (int) stringBounds.getHeight());

                if (lastLabel == null) {
                    lastLabel = labelBox;
                } else {
                    lastLabel.add(labelBox);
                }

            }
        } else {
            // draw short tick
            if (!layoutOnly) {

                final int halfTickHeight = (int) Math.floor(tickLength / 2.0d);

                graphics.drawLine(currentX, box.getY() + axisHeight, currentX,
                        box.getY() + axisHeight - halfTickHeight);
            }
        }

        // draw part of axis
        if (!layoutOnly) {
            graphics.drawLine((int) x, box.getY() + axisHeight, (int) (x + xIncrement),
                    box.getY() + axisHeight);
        }
        x += xIncrement;
        currentDate = tickDuration.addTo(currentDate);
    }

    callback.getGraphics().setColor(oldColor);
    final BoundingBox result = lastLabel != null ? lastLabel : new BoundingBox(0, 0, 0, 0);
    result.incHeight(axisHeight);
    return result;
}

From source file:VASSAL.build.module.map.MapShader.java

public void draw(Graphics g, Map map) {
    if (shadingVisible) {

        double zoom = map.getZoom();
        buildStroke(zoom);/*from www.j  a  v a  2s. c  om*/

        final Graphics2D g2 = (Graphics2D) g;

        final Composite oldComposite = g2.getComposite();
        final Color oldColor = g2.getColor();
        final Paint oldPaint = g2.getPaint();
        final Stroke oldStroke = g2.getStroke();

        g2.setComposite(getComposite());
        g2.setColor(getColor());
        g2.setPaint(scaleImage && pattern.equals(TYPE_IMAGE) && imageName != null ? getTexture(zoom)
                : getTexture());
        Area area = getShadeShape(map);
        if (zoom != 1.0) {
            area = new Area(AffineTransform.getScaleInstance(zoom, zoom).createTransformedShape(area));
        }
        g2.fill(area);
        if (border) {
            g2.setComposite(getBorderComposite());
            g2.setStroke(getStroke(map.getZoom()));
            g2.setColor(getBorderColor());
            g2.draw(area);
        }

        g2.setComposite(oldComposite);
        g2.setColor(oldColor);
        g2.setPaint(oldPaint);
        g2.setStroke(oldStroke);
    }
}

From source file:com.projity.pm.graphic.gantt.GanttRenderer.java

public void paintNonWorkingDays(Graphics2D g2, Rectangle bounds) {
    BarFormat calFormat = getCalendarFormat();
    if (calFormat == null)
        return;//from  w ww.j a va 2s  .co  m
    //non working days
    Color oldColor = g2.getColor();
    Paint oldPaint = g2.getPaint();
    CoordinatesConverter coord = ((GanttParams) graphInfo).getCoord();
    Project project = coord.getProject();
    WorkingCalendar wc = (WorkingCalendar) project.getWorkCalendar();

    if (coord.getTimescaleManager().isShowWholeDays()) {
        boolean useScale2 = coord.getTimescaleManager().getCurrentScaleIndex() == 0; //valid only for current time scales
        TimeIterator i = coord.getTimeIterator(bounds.getX(), bounds.getMaxX(), useScale2);
        long startNonworking = -1L, endNonWorking = -1L;
        Calendar cal = DateTime.calendarInstance();

        PredefinedPaint paint = (PredefinedPaint) calFormat.getMiddle().getPaint();//new PredefinedPaint(PredefinedPaint.DOT_LINE,Colors.VERY_LIGHT_GRAY,Color.WHITE);
        paint.applyPaint(g2, useTextures());
        while (i.hasNext()) {
            TimeInterval interval = i.next();
            long s = interval.getStart();
            if (CalendarService.getInstance().getDay(wc, s).isWorking()) {
                if (startNonworking != -1L) {
                    drawNonWorking(g2, startNonworking, endNonWorking, cal, coord, bounds, useScale2);
                    startNonworking = endNonWorking = -1L;
                }
            } else {
                if (startNonworking == -1L)
                    startNonworking = s;
                endNonWorking = s;

            }
        }
        if (startNonworking != -1L) {
            drawNonWorking(g2, startNonworking, endNonWorking, cal, coord, bounds, useScale2);
            startNonworking = endNonWorking = -1L;
        }
    }

    if (container != null) {
        //scale2 separation lines
        TimeIterator i = coord.getTimeIterator(bounds.getX(), bounds.getMaxX(), true);
        g2.setPaint(new PredefinedPaint(PredefinedPaint.DOT_LINE2, Color.GRAY, g2.getBackground()));
        while (i.hasNext()) {
            TimeInterval interval = i.next();
            int startX = (int) Math.round(coord.toX(interval.getStart()));
            g2.drawLine(startX, bounds.y, startX, bounds.y + bounds.height);
        }

        //project start
        int projectStartX = (int) Math.round(coord.toX(project.getStart()));
        if (projectStartX >= bounds.getX() && projectStartX <= bounds.getMaxX()) {
            g2.setPaint(new PredefinedPaint(PredefinedPaint.DASH_LINE, Color.GRAY, g2.getBackground()));
            g2.drawLine(projectStartX, bounds.y, projectStartX, bounds.y + bounds.height);
        }

        //project start
        long statusDate = project.getStatusDate();
        if (statusDate != 0) {
            int statusDateX = (int) Math.round(coord.toX(statusDate));
            if (statusDateX >= bounds.getX() && statusDateX <= bounds.getMaxX()) {
                g2.setPaint(new PredefinedPaint(PredefinedPaint.DOT_LINE2, Color.GREEN, g2.getBackground()));
                g2.drawLine(statusDateX, bounds.y, statusDateX, bounds.y + bounds.height);
            }
        }

        if (oldColor != null)
            g2.setColor(oldColor);
        if (oldPaint != null)
            g2.setPaint(oldPaint);

    }
}

From source file:HelloUniverse.java

private void drawZPip(Graphics2D g2, float zAngle) {
    AffineTransform trans = new AffineTransform();
    Color origColor = g2.getColor();

    trans.translate(margin, margin);/*from  ww w  .  j  a va2 s.c  om*/
    trans.rotate(zAngle, diameter / 2, diameter / 2);

    g2.setXORMode(getBackground());
    g2.setTransform(trans);
    g2.setColor(Color.red);
    g2.fillPolygon(zPip);

    // Reset graphics context
    trans.setToIdentity();
    g2.setTransform(trans);
    g2.setColor(origColor);
    g2.setPaintMode();
}

From source file:HelloUniverse.java

private void drawXPip(Graphics2D g2, float angle) {
    AffineTransform trans = new AffineTransform();
    int y;/*from  w ww. j av a2 s. c o  m*/
    int xOrig = margin + diameter + space;
    int yOrig = margin;
    Color origColor = g2.getColor();

    if (angle <= Math.PI) {
        y = yOrig + diameter - (int) ((Math.abs(angle - Math.PI / 2) / (Math.PI / 2)) * diameter / 2);
    } else
        y = yOrig + (int) ((Math.abs((angle - Math.PI * 1.5)) / (Math.PI / 2)) * diameter / 2);

    if (angle < Math.PI / 2 || angle > Math.PI * 1.5)
        g2.setColor(Color.red); // Infront of wheel
    else {
        g2.setColor(Color.black); // Behind Wheel
        g2.setClip(xBackClip);
    }

    g2.setXORMode(getBackground());
    trans.setToTranslation(xOrig + pipOffset, y);
    g2.setTransform(trans);
    g2.fillPolygon(xPip);

    // Reset graphics context
    trans.setToIdentity();
    g2.setTransform(trans);
    g2.setColor(origColor);
    g2.setPaintMode();
}

From source file:HelloUniverse.java

private void drawYPip(Graphics2D g2, float angle) {
    AffineTransform trans = new AffineTransform();
    int x;/*from ww w .j  a  va 2s. c o  m*/
    int xOrig = margin;
    int yOrig = margin + diameter + space;
    Color origColor = g2.getColor();

    if (angle <= Math.PI) {
        x = xOrig + diameter - (int) ((Math.abs(angle - Math.PI / 2) / (Math.PI / 2)) * diameter / 2);
    } else
        x = xOrig + (int) ((Math.abs((angle - Math.PI * 1.5)) / (Math.PI / 2)) * diameter / 2);

    if (angle < Math.PI / 2 || angle > Math.PI * 1.5)
        g2.setColor(Color.red); // Infront on wheel
    else {
        g2.setColor(Color.black); // Behind Wheel
        g2.setClip(yBackClip);
    }

    g2.setXORMode(getBackground());
    trans.setToTranslation(x, yOrig + pipOffset);
    g2.setTransform(trans);
    g2.fillPolygon(yPip);

    // Reset graphics context
    trans.setToIdentity();
    g2.setTransform(trans);
    g2.setColor(origColor);
    g2.setPaintMode();
}

From source file:net.sf.maltcms.chromaui.annotations.PeakAnnotationRenderer.java

public void draw(Graphics2D g2, ChartPanel chartPanel, XYPlot plot, Color fillColor,
        Collection<? extends VisualPeakAnnotation> shapes,
        Collection<? extends VisualPeakAnnotation> selectedShapes) {
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Shape savedClip = g2.getClip();
    Rectangle2D dataArea = chartPanel.getScreenDataArea();
    g2.clip(dataArea);//from   ww w .  j  a  v  a 2 s  .  co m

    ValueAxis xAxis = plot.getDomainAxis();
    RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
    ValueAxis yAxis = plot.getRangeAxis();
    RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
    Color c = g2.getColor();
    //         Color fillColor = peakAnnotations.getColor();
    //         if (fillColor == null || fillColor.equals(Color.WHITE) || fillColor.equals(new Color(255, 255, 255, 0))) {
    ////            System.out.println("Peak annotation color was null or white, using color from treatment group!");
    //            fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
    //         }
    for (VisualPeakAnnotation x : shapes) {
        Shape s = toViewXY(x, chartPanel, x.getCenter());
        switch (x.getPeakAnnotationType()) {
        case LINE:
            drawEntity(s, g2, fillColor, null, chartPanel, false, 0.1f);
            break;
        case OUTLINE:
            //                  plot.addAnnotation(new XYShapeAnnotation(x, new BasicStroke(1.0f), new Color(fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue(), 64), new Color(254, 254, 254, 254)), false);
            drawOutline(s, g2, fillColor, Color.DARK_GRAY, chartPanel, false, 0.25f);
            break;
        case POINTER:
            drawEntity(s, g2, fillColor, Color.DARK_GRAY, chartPanel, false, 0.25f);
            break;
        default:
            drawEntity(s, g2, fillColor, null, chartPanel, false, 0.1f);
        }
    }
    for (VisualPeakAnnotation x : selectedShapes) {
        Shape s = toViewXY(x, chartPanel, x.getCenter());
        switch (x.getPeakAnnotationType()) {
        case LINE:
            drawEntity(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
            break;
        case OUTLINE:
            //                  plot.addAnnotation(new XYShapeAnnotation(x, new BasicStroke(1.0f), new Color(fillColor.getRed(),fillColor.getGreen(),fillColor.getBlue(),64), new Color(254,254,254,254)), false);
            drawOutline(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
            break;
        case POINTER:
            drawEntity(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
            break;
        default:
            drawEntity(s, g2, fillColor, Color.BLACK, chartPanel, false, 1f);
        }
    }
    g2.setColor(c);
    g2.setClip(savedClip);
    //         chartPanel.repaint();
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

@Override
protected void paintLayer(Graphics2D g2, JXLayer<? extends V> layer) {
    super.paintLayer(g2, layer);

    if (this.mainTraceInfo.getPoint() == null) {
        return;//ww w  .  jav  a2s  .c o m
    }

    final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Color oldColor = g2.getColor();

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(COLOR_BLUE);
    final int BALL_RADIUS = 8;
    g2.fillOval((int) (this.mainTraceInfo.getPoint().getX() - (BALL_RADIUS >> 1) + 0.5),
            (int) (this.mainTraceInfo.getPoint().getY() - (BALL_RADIUS >> 1) + 0.5), BALL_RADIUS, BALL_RADIUS);

    for (TraceInfo indicatorTraceInfo : this.indicatorTraceInfos) {
        final Point2D point = indicatorTraceInfo.getPoint();
        if (null == point) {
            continue;
        }
        g2.fillOval((int) (point.getX() - (BALL_RADIUS >> 1) + 0.5),
                (int) (point.getY() - (BALL_RADIUS >> 1) + 0.5), BALL_RADIUS, BALL_RADIUS);
    }

    g2.setColor(oldColor);

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias);
    this.drawInformationBox(g2, layer);
}

From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java

private void drawTitle(Graphics2D g2) {
    final Object oldValueAntiAlias = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    final Color oldColor = g2.getColor();
    final Font oldFont = g2.getFont();

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    final Font titleFont = oldFont.deriveFont(oldFont.getStyle() | Font.BOLD, (float) oldFont.getSize() * 1.5f);

    final int margin = 5;

    final FontMetrics titleFontMetrics = g2.getFontMetrics(titleFont);
    final FontMetrics oldFontMetrics = g2.getFontMetrics(oldFont);
    final java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
    numberFormat.setMaximumFractionDigits(2);
    numberFormat.setMinimumFractionDigits(2);

    final double totalInvestValue = this.investmentFlowChartJDialog.getTotalInvestValue();
    final double totalROIValue = this.investmentFlowChartJDialog.getTotalROIValue();

    final DecimalPlace decimalPlace = JStock.instance().getJStockOptions().getDecimalPlace();

    final String invest = org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace,
            totalInvestValue);//from  ww w  . j a v  a2  s  .co m
    final String roi = org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, totalROIValue);
    final double gain = totalROIValue - totalInvestValue;
    final double percentage = totalInvestValue > 0.0 ? gain / totalInvestValue * 100.0 : 0.0;
    final String gain_str = org.yccheok.jstock.portfolio.Utils.toCurrencyWithSymbol(decimalPlace, gain);
    final String percentage_str = numberFormat.format(percentage);

    final String SELECTED = this.investmentFlowChartJDialog.getCurrentSelectedString();
    final String INVEST = GUIBundle.getString("InvestmentFlowLayerUI_Invest");
    final String RETURN = GUIBundle.getString("InvestmentFlowLayerUI_Return");
    final String GAIN = (SELECTED.length() > 0 ? SELECTED + " " : "")
            + GUIBundle.getString("InvestmentFlowLayerUI_Gain");
    final String LOSS = (SELECTED.length() > 0 ? SELECTED + " " : "")
            + GUIBundle.getString("InvestmentFlowLayerUI_Loss");

    final int string_width = oldFontMetrics.stringWidth(INVEST + ": ")
            + titleFontMetrics.stringWidth(invest + " ") + oldFontMetrics.stringWidth(RETURN + ": ")
            + titleFontMetrics.stringWidth(roi + " ")
            + oldFontMetrics.stringWidth((gain >= 0 ? GAIN : LOSS) + ": ")
            + titleFontMetrics.stringWidth(gain_str + " (" + percentage_str + "%)");

    int x = (int) (this.investmentFlowChartJDialog.getChartPanel().getWidth() - string_width) >> 1;
    final int y = margin + titleFontMetrics.getAscent();

    g2.setFont(oldFont);
    g2.drawString(INVEST + ": ", x, y);
    x += oldFontMetrics.stringWidth(INVEST + ": ");
    g2.setFont(titleFont);
    g2.drawString(invest + " ", x, y);
    x += titleFontMetrics.stringWidth(invest + " ");
    g2.setFont(oldFont);
    g2.drawString(RETURN + ": ", x, y);
    x += oldFontMetrics.stringWidth(RETURN + ": ");
    g2.setFont(titleFont);
    g2.drawString(roi + " ", x, y);
    x += titleFontMetrics.stringWidth(roi + " ");
    g2.setFont(oldFont);
    if (gain >= 0) {
        if (gain > 0) {
            if (org.yccheok.jstock.engine.Utils.isFallBelowAndRiseAboveColorReverse()) {
                g2.setColor(JStock.instance().getJStockOptions().getLowerNumericalValueForegroundColor());
            } else {
                g2.setColor(JStock.instance().getJStockOptions().getHigherNumericalValueForegroundColor());
            }
        }
        g2.drawString(GAIN + ": ", x, y);
        x += oldFontMetrics.stringWidth(GAIN + ": ");
    } else {
        if (org.yccheok.jstock.engine.Utils.isFallBelowAndRiseAboveColorReverse()) {
            g2.setColor(JStock.instance().getJStockOptions().getHigherNumericalValueForegroundColor());
        } else {
            g2.setColor(JStock.instance().getJStockOptions().getLowerNumericalValueForegroundColor());
        }
        g2.drawString(LOSS + ": ", x, y);
        x += oldFontMetrics.stringWidth(LOSS + ": ");
    }
    g2.setFont(titleFont);
    g2.drawString(gain_str + " (" + percentage_str + "%)", x, y);

    g2.setColor(oldColor);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldValueAntiAlias);
    g2.setFont(oldFont);
}

From source file:org.kalypso.kalypso1d2d.pjt.map.HydrographManagementWidget.java

private void paintHydrographInMap(final Graphics g) {
    final Graphics2D g2 = (Graphics2D) g;
    final GM_Point point = (GM_Point) m_selectedHydrograph.getLocation();
    final IMapPanel mapPanel = getMapPanel();

    if (mapPanel == null)
        return;//from ww w .  j  a  v a  2s  . c o  m

    final GeoTransform projection = mapPanel.getProjection();
    if (projection == null || point == null)
        return;

    final int x = (int) projection.getDestX(point.getX());
    final int y = (int) projection.getDestY(point.getY());

    final int sizeOuter = 16;
    final Color defaultColor = g2.getColor();
    final Color color = new Color(255, 30, 30);
    g2.setColor(color);

    final Stroke defaultStroke = g2.getStroke();
    final BasicStroke stroke = new BasicStroke(3);
    g2.setStroke(stroke);

    g2.drawRect(x - sizeOuter / 2, y - sizeOuter / 2, sizeOuter, sizeOuter);

    g2.setColor(defaultColor);
    g2.setStroke(defaultStroke);
}