Example usage for java.awt Graphics2D setColor

List of usage examples for java.awt Graphics2D setColor

Introduction

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

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:TrackerPanel.java

private void writeStats(Graphics2D g2d) {
    g2d.setColor(Color.WHITE);
    int panelHeight = getHeight();
    if (imageCount > 0) {
        double avgGrabTime = (double) totalTime / imageCount;
        g2d.drawString("Pic " + imageCount + "  " + df.format(avgGrabTime) + " ms", 5, panelHeight); // bottom left
    } else//from w ww. ja  v  a  2  s  .c o  m
        // no image yet
        g2d.drawString("Loading...", 5, panelHeight);
}

From source file:org.n52.io.measurement.img.ChartIoHandler.java

private BufferedImage createImage() {
    int width = getChartStyleDefinitions().getWidth();
    int height = getChartStyleDefinitions().getHeight();
    BufferedImage chartImage = new BufferedImage(width, height, TYPE_INT_RGB);
    Graphics2D chartGraphics = chartImage.createGraphics();
    chartGraphics.fillRect(0, 0, width, height);
    chartGraphics.setColor(WHITE);

    chart.setTextAntiAlias(true);//from  ww  w .  ja v a  2  s  .  c  o  m
    chart.setAntiAlias(true);
    if (chart.getLegend() != null) {
        chart.getLegend().setFrame(BlockBorder.NONE);
    }
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));
    return chartImage;
}

From source file:io.github.karols.hocr4j.PageRenderer.java

/**
 * Renders this page on the given image.
 * The image is modified, not copied.//  w  w  w  . j a  v  a  2  s.  c om
 *
 * @param page page to render
 */
public void renderOnTop(@Nonnull Page page, @Nonnull BufferedImage img) {
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(Color.RED);
    for (Area a : page) {
        for (Paragraph p : a) {
            for (Line l : p) {
                for (Word w : l.words) {
                    if (w.isBold()) {
                        if (w.isItalic()) {
                            g.setFont(boldItalicFont);
                        } else {
                            g.setFont(boldFont);
                        }
                    } else if (w.isItalic()) {
                        g.setFont(italicFont);
                    } else {
                        g.setFont(plainFont);
                    }
                    Bounds b = w.getBounds().scale(scale);
                    g.drawString(w.getText(), b.getLeft(), b.getBottom());
                }
            }
        }
    }
    g.setStroke(new BasicStroke(strokeWidth));
    g.setColor(defaultRectangleColor);
    for (Bounds rect : rectanglesToDraw) {
        if (rect != null) {
            Bounds b = rect.scale(scale);
            g.drawRect(b.getLeft(), b.getTop(), b.getWidth(), b.getHeight());
        }
    }
    for (Pair<Color, Bounds> rect : coloredRectanglesToDraw) {
        if (rect != null) {
            g.setColor(rect.getLeft());
            Bounds b = rect.getRight().scale(scale);
            g.drawRect(b.getLeft(), b.getTop(), b.getWidth(), b.getHeight());
        }
    }
}

From source file:org.apache.fop.util.BitmapImageUtilTestCase.java

private BufferedImage createTestImage() {
    BufferedImage buf = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = buf.createGraphics();
    g2d.setBackground(Color.WHITE);
    g2d.clearRect(0, 0, buf.getWidth(), buf.getHeight());

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    copy.translate(170, 170);//from ww  w .  jav a 2  s  . c  om
    int c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(0, 0, 120, 120);
        copy.rotate(-2 * Math.PI / c);
    }
    copy.dispose();

    //the same in gray scales
    copy = (Graphics2D) g2d.create();
    copy.translate(470, 310);
    c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(f, f, f);
        copy.setColor(col);
        copy.fillRect(0, 0, 120, 120);
        copy.rotate(-2 * Math.PI / c);
    }
    copy.dispose();
    return buf;
}

From source file:Slice.java

void drawPie(Graphics2D g, Rectangle area, Slice[] slices) {
    double total = 0.0D;
    for (int i = 0; i < slices.length; i++) {
        total += slices[i].value;//w ww  . j a v a  2s .  c  o m
    }

    double curValue = 0.0D;
    int startAngle = 0;
    for (int i = 0; i < slices.length; i++) {
        startAngle = (int) (curValue * 360 / total);
        int arcAngle = (int) (slices[i].value * 360 / total);

        g.setColor(slices[i].color);
        g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
        curValue += slices[i].value;
    }
}

From source file:HitTestSample.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);//from w  w  w. j a  v a2  s.c o m
    Graphics2D graphics2D = (Graphics2D) g;

    Point2D origin = computeLayoutOrigin();

    graphics2D.translate(origin.getX(), origin.getY());

    // Draw textLayout.
    textLayout.draw(graphics2D, 0, 0);

    // Retrieve caret Shapes for insertionIndex.
    Shape[] carets = textLayout.getCaretShapes(insertionIndex);

    graphics2D.setColor(STRONG_CARET_COLOR);
    graphics2D.draw(carets[0]);

    if (carets[1] != null) {
        graphics2D.setColor(WEAK_CARET_COLOR);
        graphics2D.draw(carets[1]);
    }
}

From source file:juicebox.track.EigenvectorTrack.java

/**
 * Render the track in the supplied rectangle.  It is the responsibility of the track to draw within the
 * bounds of the rectangle.//from w  w w  . j  av a2s.  c o m
 *
 * @param g2d      the graphics context
 * @param rect     the track bounds, relative to the enclosing DataPanel bounds.
 * @param gridAxis
 */

@Override
public void render(Graphics2D g2d, Context context, Rectangle rect, TrackPanel.Orientation orientation,
        HiCGridAxis gridAxis) {

    g2d.setColor(color);

    int height = orientation == TrackPanel.Orientation.X ? rect.height : rect.width;
    int width = orientation == TrackPanel.Orientation.X ? rect.width : rect.height;
    int y = orientation == TrackPanel.Orientation.X ? rect.y : rect.x;
    int x = orientation == TrackPanel.Orientation.X ? rect.x : rect.y;

    MatrixZoomData zd;
    try {
        zd = hic.getZd();
    } catch (Exception e) {
        return;
    }

    int zoom = zd.getZoom().getBinSize();
    if (zoom != currentZoom) {
        clearDataCache();
    }

    int chrIdx = orientation == TrackPanel.Orientation.X ? zd.getChr1Idx() : zd.getChr2Idx();
    double[] eigen = dataCache.get(chrIdx);
    if (eigen == null) {
        eigen = hic.getEigenvector(chrIdx, 0);
        currentZoom = zoom;
        setData(chrIdx, eigen);
    }

    if (eigen == null || eigen.length == 0) {
        Font original = g2d.getFont();
        g2d.setFont(FontManager.getFont(12));

        if (orientation == TrackPanel.Orientation.X) {
            GraphicUtils.drawCenteredText("Eigenvector not available at this resolution", rect, g2d);
        } else {
            drawRotatedString(g2d, "Eigenvector not available at this resolution", (2 * rect.height) / 3,
                    rect.x + 15);
        }

        g2d.setFont(original);
        return;
    }

    double dataMax = dataMaxCache.get(chrIdx);
    double median = medianCache.get(chrIdx);

    int h = height / 2;

    for (int bin = (int) context.getBinOrigin(); bin < eigen.length; bin++) {

        if (Double.isNaN(eigen[bin]))
            continue;

        int xPixelLeft = x + (int) ((bin - context.getBinOrigin()) * hic.getScaleFactor());
        int xPixelRight = x + (int) ((bin + 1 - context.getBinOrigin()) * hic.getScaleFactor());

        if (xPixelRight < x) {
            continue;
        } else if (xPixelLeft > x + width) {
            break;
        }

        double x2 = eigen[bin] - median;
        double max = dataMax - median;

        int myh = (int) ((x2 / max) * h);
        if (x2 > 0) {
            g2d.fillRect(xPixelLeft, y + h - myh, (xPixelRight - xPixelLeft), myh);
        } else {
            g2d.fillRect(xPixelLeft, y + h, (xPixelRight - xPixelLeft), -myh);
        }
    }

}

From source file:adams.gui.visualization.stats.paintlet.Gamma.java

/**
 * The paint routine of the paintlet./* w w w .  j  a va2s.  com*/
 *
 * @param g      the graphics context to use for painting
 * @param moment   what {@link PaintMoment} is currently being painted
 */
@Override
protected void doPerformPaint(Graphics g, PaintMoment moment) {
    if ((m_Data != null) && (m_Sorted != null) && m_Shape != -1.0) {
        GUIHelper.configureAntiAliasing(g, m_AntiAliasingEnabled);

        for (int i = 0; i < m_Sorted.length; i++) {
            Graphics2D g2d = (Graphics2D) g;
            //If data points are to be filled
            if (m_Fill) {
                g2d.setColor(m_FillColor);
                g2d.setStroke(new BasicStroke(0));
                g2d.fillOval(m_AxisBottom.valueToPos(m_Sorted[i]) - m_Size / 2,
                        m_AxisLeft.valueToPos(m_TransformedY[i]) - m_Size / 2, m_Size, m_Size);
            }
            //outline of data point
            g2d.setStroke(new BasicStroke(m_StrokeThickness));
            g2d.setColor(m_Color);
            g2d.drawOval(m_AxisBottom.valueToPos(m_Sorted[i]) - m_Size / 2,
                    m_AxisLeft.valueToPos(m_TransformedY[i]) - m_Size / 2, m_Size, m_Size);
        }

        //If drawing regression fit diagonal
        if (m_RegressionLine) {
            g.setColor(Color.BLACK);
            double[] newData = new double[m_Sorted.length];
            for (int i = 0; i < m_Sorted.length; i++) {
                newData[i] = Math.log(m_Sorted[i]);
            }
            GammaDistributionImpl gd = new GammaDistributionImpl(m_Shape, m_Scale);
            //draw the expected diagonal line using the gamma distribution
            for (int i = 0; i < m_Sorted.length - 1; i++) {
                double p1;
                try {
                    p1 = gd.cumulativeProbability(newData[i]);
                } catch (MathException e) {
                    p1 = 0;
                }
                double p2;
                try {
                    p2 = gd.cumulativeProbability(newData[i + 1]);
                } catch (MathException e) {
                    p2 = 0;
                }
                g.drawLine(m_AxisBottom.valueToPos(m_Sorted[i]), m_AxisLeft.valueToPos(p1),
                        m_AxisBottom.valueToPos(m_Sorted[i + 1]), m_AxisLeft.valueToPos(p2));
            }
        }
    }
}

From source file:SimpleAttributes.java

public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setBackground(Color.GRAY);
    g2d.clearRect(0, 0, getWidth(), getHeight());

    // String and line with default attributes
    g2d.drawString("Default Font", 10, 20);
    g2d.drawLine(10, 22, 80, 22);/*from w w w  .ja v a 2  s.  c o  m*/

    // Change the font, foreground color, and Stroke
    g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC, 24f));
    g2d.setColor(Color.WHITE);
    g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));

    // String and line with new attributes
    g2d.drawString("New Font", 10, 50);
    g2d.drawLine(10, 57, 120, 57);
    g2d.dispose();
}

From source file:MainClass.java

public void paintToPDF(JTextPane ta) {
    try {//from   ww  w .  j  ava2s.  c o  m
        ta.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60));

        Document document = new Document();
        FileOutputStream fos = new FileOutputStream("2.pdf");
        PdfWriter writer = PdfWriter.getInstance(document, fos);

        document.setPageSize(new com.lowagie.text.Rectangle(612, 792));
        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        cb.concatCTM(1, 0, 0, 1, 0, 0);

        DefaultFontMapper mapper = new DefaultFontMapper();
        mapper.insertDirectory("c:/windows/fonts");

        Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f);

        AffineTransform at = new AffineTransform();
        at.translate(convertToPixels(20), convertToPixels(20));
        at.scale(pixelToPoint, pixelToPoint);

        g2.transform(at);

        g2.setColor(Color.WHITE);
        g2.fill(ta.getBounds());

        Rectangle alloc = getVisibleEditorRect(ta);
        ta.getUI().getRootView(ta).paint(g2, alloc);

        g2.setColor(Color.BLACK);
        g2.draw(ta.getBounds());

        g2.dispose();
        cb.restoreState();
        document.close();
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}