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:com.quinsoft.zeidon.objectbrowser.EntitySquare.java

private void paintCenteredText(Graphics2D graphics2, int y, String text, Color color) {
    Color prevColor = graphics2.getColor();
    if (color != null)
        graphics2.setColor(color);//w  ww.  j a  va  2  s  . c o  m

    FontMetrics fm = graphics2.getFontMetrics();

    String lines[] = text.split("\n");

    // Adjust y if there is more than one line.
    y -= (lines.length - 1) * fm.getHeight() / 2;

    for (String line : lines) {
        int lth = fm.stringWidth(line);
        int mid = size.width / 2;
        graphics2.drawString(line, mid - lth / 2, y);
        y += fm.getHeight();
    }

    if (color != null)
        graphics2.setColor(prevColor);
}

From source file:org.processmining.analysis.performance.sequence.Pattern.java

/**
 * Draws a rectangle of width 20, height length and starting point
 * (startX,startY) in the northwest corner of the rectangle. In case
 * logicSteps is true, the height is 10.
 * //  ww w . j  a  v  a 2  s .  co m
 * @param startX
 *            double
 * @param startY
 *            double
 * @param length
 *            double
 * @param logicSteps
 *            boolean
 * @param g
 *            Graphics2D
 */
public void drawRectangle(double startX, double startY, double length, boolean logicSteps, Graphics2D g) {
    Rectangle2D r = new Rectangle2D.Double(startX, startY, 20, length);
    if (logicSteps) {
        r = new Rectangle2D.Double(startX, startY, 20, 10);
    }
    Color initialColor = g.getColor();
    Paint initialPaint = g.getPaint();
    GradientPaint towhite = new GradientPaint(((Double) startX).floatValue(), ((Double) startY).floatValue(),
            initialColor, ((Double) startX).floatValue() + 20, ((Double) (startY)).floatValue(), Color.WHITE);
    g.setPaint(towhite);
    g.fill(r);
    g.setPaint(initialPaint);
    g.setColor(Color.BLACK);
    g.draw(r);
    g.setColor(initialColor);
}

From source file:savant.view.tracks.TrackRenderer.java

public void drawFeatureLabel(Graphics2D g2, String geneName, double startXPos, double y) {
    FontMetrics fm = g2.getFontMetrics();
    double stringstartx = startXPos - fm.stringWidth(geneName) - 5;

    if (stringstartx <= 0) {
        Rectangle2D r = fm.getStringBounds(geneName, g2);

        int b = 2;
        Color textColor = g2.getColor();
        g2.setColor(new Color(255, 255, 255, 200));
        g2.fill(new RoundRectangle2D.Double(3.0, y - (fm.getHeight() - fm.getDescent()) - b,
                r.getWidth() + 2 * b, r.getHeight() + 2 * b, 8.0, 8.0));
        g2.setColor(textColor);//from   ww  w.j a  v  a  2  s.c om
        g2.drawString(geneName, 5.0F, (float) y);
    } else {
        g2.drawString(geneName, (float) stringstartx, (float) y);
    }
}

From source file:pl.edu.icm.visnow.lib.basic.viewers.Viewer2D.Display2DPanel.java

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Font f = g2d.getFont();//from   www .  j a v a2 s .co  m
    Color c = g2d.getColor();
    Rectangle clear = new Rectangle(0, 0, getWidth(), getHeight());
    g2d.setPaint(bgColor);
    g2d.fill(clear);

    draw2D(g2d);

    g2d.setFont(titleFont);
    g2d.setColor(titleColor);
    g2d.drawString(titleText, 50, 10 + titleFont.getSize());
    g2d.setFont(f);
    g2d.setColor(c);
    if (storingFrames && !dontWrite) {
        if (storingJPEG) {
            writeImage(controlsFrame.getMovieCreationPanel().getCurrentFrameFileName(), FORMAT_JPEG);
        } else {
            writeImage(controlsFrame.getMovieCreationPanel().getGenericFrameFileName(), FORMAT_PNG);
        }
    }
}

From source file:org.knime.knip.core.ui.imgviewer.panels.HistogramBC.java

private XYAnnotation slopeLine() {
    return new XYAnnotation() {

        private double x1, y1, x2, y2;

        @Override/*  w w w. j  a  v  a 2 s.  co m*/
        public void removeChangeListener(final AnnotationChangeListener listener) {
            // ignore
        }

        @Override
        public void addChangeListener(final AnnotationChangeListener listener) {
            // ignore
        }

        @Override
        public void draw(final Graphics2D g2, final XYPlot plot, final Rectangle2D dataArea,
                final ValueAxis domainAxis, final ValueAxis rangeAxis, final int rendererIndex,
                final PlotRenderingInfo info) {
            calcLineCoords(dataArea);
            drawLine(g2);
        }

        private void drawLine(final Graphics2D g2) {
            final Color origColor = g2.getColor();
            g2.setColor(Color.black);
            g2.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
            g2.setColor(Color.lightGray);
            g2.drawLine((int) x1, 0, (int) x1, 192);
            g2.drawLine((int) x2, 0, (int) x2, 192);
            g2.setColor(origColor);
        }

        @SuppressWarnings("synthetic-access")
        private void calcLineCoords(final Rectangle2D rect) {
            // offset necessary since chart is not drawn on whole rectangle
            int offset = 12;
            final double x = rect.getMinX() + offset;
            final double y = rect.getMinY();
            final double w = rect.getWidth() - 2 * offset;
            final double h = rect.getHeight();
            final double min = bundle.getTheoreticalMin();
            final double max = bundle.getTheoreticalMax();
            final double defaultMin = bundle.getDataMin();
            final double defaultMax = bundle.getDataMax();
            final double scale = w / (defaultMax - defaultMin);
            double slope = 0.0;
            if (max != min) {
                slope = h / (max - min);
            }
            if (min >= defaultMin) {
                x1 = scale * (min - defaultMin);
                y1 = h;
            } else {
                x1 = 0;
                if (max > min) {
                    y1 = h - ((defaultMin - min) * slope);
                } else {
                    y1 = h;
                }
            }
            if (max <= defaultMax) {
                x2 = (scale * (max - defaultMin));
                y2 = 0;
            } else {
                x2 = w;
                if (max > min) {
                    y2 = h - ((defaultMax - min) * slope);
                } else {
                    y2 = 0;
                }
            }
            x1 += x;
            x2 += x;
            y1 += y;
            y2 += y;
        }
    };
}

From source file:net.sf.maltcms.chromaui.charts.overlay.Peak1DHeatmapOverlay.java

/**
 *
 * @param g2/*from   w w w  .  j ava  2 s .  c o  m*/
 * @param chartPanel
 */
@Override
public void paintOverlay(Graphics2D g2, ChartPanel chartPanel) {
    if (isVisible()) {
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        g2.clip(dataArea);
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        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))) {
            Logger.getLogger(getClass().getName())
                    .info("Peak annotation color was null or white, using color from treatment group!");
            fillColor = peakAnnotations.getChromatogram().getTreatmentGroup().getColor();
        }
        g2.setColor(ChartCustomizer.withAlpha(fillColor, 0.5f));
        for (IPeakAnnotationDescriptor descr : peakAnnotations.getMembers()) {
            double x = descr.getApexTime();
            double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
            double width = xAxis.valueToJava2D(1, dataArea, xAxisEdge);
            double mzRange = (descr.getMassValues()[descr.getMassValues().length - 1]
                    - descr.getMassValues()[0]);
            double y = mzRange / 2.0d;
            double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
            double height = yAxis.valueToJava2D(mzRange, dataArea, yAxisEdge);
            AffineTransform at = AffineTransform.getTranslateInstance(xx, yy);
            at.concatenate(AffineTransform.getTranslateInstance(-x, -y));
            Rectangle2D.Double r = new Rectangle2D.Double(xx - (width / 2.0d), yy, width, height);
            g2.fill(at.createTransformedShape(r));
        }
        g2.setColor(c);
        g2.setClip(savedClip);
    }
}

From source file:net.sf.maltcms.common.charts.api.overlay.SelectionOverlay.java

private void drawEntity(Shape entity, Graphics2D g2, Color fill, ChartPanel chartPanel, boolean scale) {
    if (entity != null) {
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        Color c = g2.getColor();
        Composite comp = g2.getComposite();
        g2.clip(dataArea);/*from   w  w w.j  a v  a 2  s.c  o  m*/
        g2.setColor(fill);
        AffineTransform originalTransform = g2.getTransform();
        Shape transformed = entity;
        if (scale) {
            transformed = scaleAtOrigin(entity, hoverScaleX, hoverScaleY).createTransformedShape(entity);
        }
        transformed = getTranslateInstance(
                entity.getBounds2D().getCenterX() - transformed.getBounds2D().getCenterX(),
                entity.getBounds2D().getCenterY() - transformed.getBounds2D().getCenterY())
                        .createTransformedShape(transformed);
        g2.setComposite(getInstance(AlphaComposite.SRC_OVER, fillAlpha));
        g2.fill(transformed);
        g2.setColor(Color.DARK_GRAY);
        g2.draw(transformed);
        g2.setComposite(comp);
        g2.setColor(c);
        g2.setClip(savedClip);
    }
}

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

private void drawEntity(Shape entity, Graphics2D g2, Color fill, Color stroke, ChartPanel chartPanel,
        boolean scale, float alpha) {
    if (entity != null) {
        //System.out.println("Drawing entity with bbox: "+entity.getBounds2D());
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        Color c = g2.getColor();
        Composite comp = g2.getComposite();
        g2.clip(dataArea);//from  w ww. j a  v  a2s  .c o m
        g2.setColor(fill);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform originalTransform = g2.getTransform();
        Shape transformed = entity;
        FlatteningPathIterator iter = new FlatteningPathIterator(
                transformed.getPathIterator(new AffineTransform()), 1);
        Path2D.Float path = new Path2D.Float();
        path.append(iter, false);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2.fill(path);
        if (stroke != null) {
            g2.setColor(stroke);
            g2.draw(path);
        }
        g2.setComposite(comp);
        g2.setColor(c);
        g2.setClip(savedClip);
    } else {
        Logger.getLogger(getClass().getName()).info("Entity is null!");
    }
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreeRenderer.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Dimension d = getSize();/*from w ww.j ava2  s  .c  om*/
    //System.out.println("d: "+d+"     "+g.getClipBounds());

    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHints(renderingHints);

    FontMetrics fm = g2d.getFontMetrics();
    int imgY = img1 != null ? (d.height - 24) / 2 : 0;
    int imgY2 = img1 != null ? (d.height - 16) / 2 : 0;
    int txtY = ((d.height - fm.getHeight()) / 2) + fm.getAscent();
    int x = 0;

    Color color = g2d.getColor();

    if (img1 != null) {
        g2d.drawImage(img1.getImage(), x, imgY, null);
        x += img1.getIconWidth();
    }

    int iconInx = 0;

    if (txt1 != null) {
        x += getIconTextGap();
        g2d.setColor(getForeground());
        g.drawString(txt1, x, txtY);
        x += fm.stringWidth(txt1);
        g2d.setColor(color);
    }

    if (isContainer) {
        //if (isSelected  && isEditable)
        //{
        //    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++); // Delete the container
        //}

        if (hasColObj) {
            if (img2 != null) {
                x += 1;
                x += iconSep;
                g2d.drawImage(img2.getImage(), x, imgY2, null);
                x += img2.getIconWidth();
            }

            if (txt2 != null) {
                x += getIconTextGap();
                g2d.setColor(getForeground());
                g.drawString(txt2, x, txtY);
                x += fm.stringWidth(txt2);
                g2d.setColor(color);
            }

            if (isSelected) {
                x += iconSep;
                x += drawIcon(g2d, x, imgY2, viewImgIcon, iconInx++);

                if (isEditable) {
                    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++);
                }
            }
        } else if (isSelected) // No Col Obj
        {
            x += iconSep;
            x += drawIcon(g2d, x, imgY2, schImgIcon, iconInx++);
            x += drawIcon(g2d, x, imgY2, addImgIcon, iconInx++);
        }

    } else if (isSelected) {
        x += iconSep;
        x += drawIcon(g2d, x, imgY2, viewImgIcon, iconInx++); // View for Collection Object

        //if (!isViewMode)
        //{
        //    x += iconSep;
        //    x += drawIcon(g2d, x, imgY2, delImgIcon, iconInx++); // Delete for Collection Object
        //}
    }

    g2d.dispose();
}

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

private void drawOutline(Shape entity, Graphics2D g2, Color fill, Color stroke, ChartPanel chartPanel,
        boolean scale, float alpha) {
    if (entity != null) {
        //System.out.println("Drawing entity with bbox: "+entity.getBounds2D());
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        Color c = g2.getColor();
        Composite comp = g2.getComposite();
        g2.clip(dataArea);/*from   w ww. j  av a2s  . co m*/
        g2.setColor(fill);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        ValueAxis xAxis = plot.getDomainAxis();
        ValueAxis yAxis = plot.getRangeAxis();
        RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
        RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
        Rectangle2D entityBounds = entity.getBounds2D();
        double viewX = xAxis.valueToJava2D(entityBounds.getCenterX(), dataArea, xAxisEdge);
        double viewY = yAxis.valueToJava2D(entityBounds.getCenterY(), dataArea, yAxisEdge);
        double viewW = xAxis.lengthToJava2D(entityBounds.getWidth(), dataArea, xAxisEdge);
        double viewH = yAxis.lengthToJava2D(entityBounds.getHeight(), dataArea, yAxisEdge);
        PlotOrientation orientation = plot.getOrientation();

        //transform model to origin (0,0) in model coordinates
        AffineTransform toOrigin = AffineTransform.getTranslateInstance(-entityBounds.getCenterX(),
                -entityBounds.getCenterY());
        //transform from origin (0,0) to model location
        AffineTransform toModelLocation = AffineTransform.getTranslateInstance(entityBounds.getCenterX(),
                entityBounds.getCenterY());
        //transform from model scale to view scale
        double scaleX = viewW / entityBounds.getWidth();
        double scaleY = viewH / entityBounds.getHeight();
        Logger.getLogger(getClass().getName()).log(Level.FINE, "Scale x: {0} Scale y: {1}",
                new Object[] { scaleX, scaleY });
        AffineTransform toViewScale = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransform toViewLocation = AffineTransform.getTranslateInstance(viewX, viewY);
        AffineTransform flipTransform = AffineTransform.getScaleInstance(1.0f, -1.0f);
        AffineTransform modelToView = new AffineTransform(toOrigin);
        modelToView.preConcatenate(flipTransform);
        modelToView.preConcatenate(toViewScale);
        modelToView.preConcatenate(toViewLocation);
        //
        //            if (orientation == PlotOrientation.HORIZONTAL) {
        //                entity = ShapeUtilities.createTranslatedShape(entity, viewY,
        //                        viewX);
        //            } else if (orientation == PlotOrientation.VERTICAL) {
        //                entity = ShapeUtilities.createTranslatedShape(entity, viewX,
        //                        viewY);
        //            }
        FlatteningPathIterator iter = new FlatteningPathIterator(modelToView.createTransformedShape(entity)
                .getPathIterator(AffineTransform.getTranslateInstance(0, 0)), 5);
        Path2D.Float path = new Path2D.Float();
        path.append(iter, false);

        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2.fill(path);
        if (stroke != null) {
            g2.setColor(stroke);
            g2.draw(path);
        }
        g2.setComposite(comp);
        g2.setColor(c);
        g2.setClip(savedClip);
    } else {
        Logger.getLogger(getClass().getName()).info("Entity is null!");
    }
}