Example usage for java.awt Graphics2D setPaint

List of usage examples for java.awt Graphics2D setPaint

Introduction

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

Prototype

public abstract void setPaint(Paint paint);

Source Link

Document

Sets the Paint attribute for the Graphics2D context.

Usage

From source file:XMLWriteTest.java

public void paintComponent(Graphics g) {
    if (rects.size() == 0)
        newDrawing();//from w w w  . jav  a  2 s  .  co m
    Graphics2D g2 = (Graphics2D) g;

    // draw all rectangles
    for (int i = 0; i < rects.size(); i++) {
        g2.setPaint(colors.get(i));
        g2.fill(rects.get(i));
    }
}

From source file:Paints.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    // Paint the entire background using a GradientPaint.
    // The background color varies diagonally from deep red to pale blue
    g.setPaint(new GradientPaint(0, 0, new Color(150, 0, 0), WIDTH, HEIGHT, new Color(200, 200, 255)));
    g.fillRect(0, 0, WIDTH, HEIGHT); // fill the background

    // Use a different GradientPaint to draw a box.
    // This one alternates between deep opaque green and transparent green.
    // Note: the 4th arg to Color() constructor specifies color opacity
    g.setPaint(new GradientPaint(0, 0, new Color(0, 150, 0), 20, 20, new Color(0, 150, 0, 0), true));
    g.setStroke(new BasicStroke(15)); // use wide lines
    g.drawRect(25, 25, WIDTH - 50, HEIGHT - 50); // draw the box

    // The glyphs of fonts can be used as Shape objects, which enables
    // us to use Java2D techniques with letters Just as we would with
    // any other shape. Here we get some letter shapes to draw.
    Font font = new Font("Serif", Font.BOLD, 10); // a basic font
    Font bigfont = // a scaled up version
            font.deriveFont(AffineTransform.getScaleInstance(30.0, 30.0));
    GlyphVector gv = bigfont.createGlyphVector(g.getFontRenderContext(), "JAV");
    Shape jshape = gv.getGlyphOutline(0); // Shape of letter J
    Shape ashape = gv.getGlyphOutline(1); // Shape of letter A
    Shape vshape = gv.getGlyphOutline(2); // Shape of letter V

    // We're going to outline the letters with a 5-pixel wide line
    g.setStroke(new BasicStroke(5.0f));

    // We're going to fake shadows for the letters using the
    // following Paint and AffineTransform objects
    Paint shadowPaint = new Color(0, 0, 0, 100); // Translucent black
    AffineTransform shadowTransform = AffineTransform.getShearInstance(-1.0, 0.0); // Shear to the right
    shadowTransform.scale(1.0, 0.5); // Scale height by 1/2

    // Move to the baseline of our first letter
    g.translate(65, 270);/*from w ww  .ja v a 2s .  c o m*/

    // Draw the shadow of the J shape
    g.setPaint(shadowPaint);
    g.translate(15, 20); // Compensate for the descender of the J
    // transform the J into the shape of its shadow, and fill it
    g.fill(shadowTransform.createTransformedShape(jshape));
    g.translate(-15, -20); // Undo the translation above

    // Now fill the J shape with a solid (and opaque) color
    g.setPaint(Color.blue); // Fill with solid, opaque blue
    g.fill(jshape); // Fill the shape
    g.setPaint(Color.black); // Switch to solid black
    g.draw(jshape); // And draw the outline of the J

    // Now draw the A shadow
    g.translate(75, 0); // Move to the right
    g.setPaint(shadowPaint); // Set shadow color
    g.fill(shadowTransform.createTransformedShape(ashape)); // draw shadow

    // Draw the A shape using a solid transparent color
    g.setPaint(new Color(0, 255, 0, 125)); // Transparent green as paint
    g.fill(ashape); // Fill the shape
    g.setPaint(Color.black); // Switch to solid back
    g.draw(ashape); // Draw the outline

    // Move to the right and draw the shadow of the letter V
    g.translate(175, 0);
    g.setPaint(shadowPaint);
    g.fill(shadowTransform.createTransformedShape(vshape));

    // We're going to fill the next letter using a TexturePaint, which
    // repeatedly tiles an image. The first step is to obtain the image.
    // We could load it from an image file, but here we create it
    // ourselves by drawing a into an off-screen image. Note that we use
    // a GradientPaint to fill the off-screen image, so the fill pattern
    // combines features of both Paint classes.
    BufferedImage tile = // Create an image
            new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D tg = tile.createGraphics(); // Get its Graphics for drawing
    tg.setColor(Color.pink);
    tg.fillRect(0, 0, 50, 50); // Fill tile background with pink
    tg.setPaint(new GradientPaint(40, 0, Color.green, // diagonal gradient
            0, 40, Color.gray)); // green to gray
    tg.fillOval(5, 5, 40, 40); // Draw a circle with this gradient

    // Use this new tile to create a TexturePaint and fill the letter V
    g.setPaint(new TexturePaint(tile, new Rectangle(0, 0, 50, 50)));
    g.fill(vshape); // Fill letter shape
    g.setPaint(Color.black); // Switch to solid black
    g.draw(vshape); // Draw outline of letter

    // Move to the right and draw the shadow of the final A
    g.translate(160, 0);
    g.setPaint(shadowPaint);
    g.fill(shadowTransform.createTransformedShape(ashape));

    g.fill(ashape); // Fill letter A
    g.setPaint(Color.black); // Revert to solid black
    g.draw(ashape); // Draw the outline of the A
}

From source file:edu.ku.brc.af.prefs.PrefsToolbar.java

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

    Color base = getBackground();
    Dimension size = getSize();//  w  ww  .j  av  a  2 s .  co m

    Color grad_top = base;
    Color grad_bot = UIHelper.makeDarker(base, UIHelper.isMacOS() ? 0.15 : 0.1);
    GradientPaint bg = new GradientPaint(new Point(0, 0), grad_top, new Point(0, size.height), grad_bot);
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(bg);
    g2.fillRect(0, 0, size.width, size.height);

    g.setColor(UIHelper.makeDarker(base, 0.5));
    g.drawLine(0, size.height - 1, size.width, size.height - 1);
}

From source file:BookTest.java

public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page >= 1)
        return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(Color.black);
    g2.translate(pf.getImageableX(), pf.getImageableY());
    FontRenderContext context = g2.getFontRenderContext();
    Font f = g2.getFont();//from  www .ja va 2s .  co m
    TextLayout layout = new TextLayout(title, f, context);
    float ascent = layout.getAscent();
    g2.drawString(title, 0, ascent);
    return Printable.PAGE_EXISTS;
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;

    Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f);
    float width = 300;
    float height = 50;
    GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g1);
    g2D.fill(rect1);/*ww  w . j  a  v a2  s  .c o  m*/
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));

}

From source file:org.jfree.experimental.chart.plot.dial.DialCap.java

/**
 * Draws the background to the specified graphics device.  If the dial
 * frame specifies a window, the clipping region will already have been 
 * set to this window before this method is called.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param plot  the plot (ignored here).
 * @param frame  the dial frame (ignored here).
 * @param view  the view rectangle (<code>null</code> not permitted). 
 *///from   ww  w . ja  va2 s.  c om
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    g2.setPaint(this.fillPaint);

    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius, this.radius);
    Ellipse2D e = new Ellipse2D.Double(f.getX(), f.getY(), f.getWidth(), f.getHeight());
    g2.fill(e);
    g2.setPaint(this.outlinePaint);
    g2.setStroke(this.outlineStroke);
    g2.draw(e);

}

From source file:dbseer.gui.panel.DBSeerExplainChartPanel.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    selectRectangle = new Rectangle2D.Double(x, y, width, height);
    g2d.setPaint(Color.black);
    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f);
    g2d.setComposite(c);/* ww  w . j a  v a 2  s .c  o  m*/
    g2d.fill(selectRectangle);
}

From source file:org.fhcrc.cpl.viewer.quant.gui.LogRatioHistMouseListener.java

/**
 * Draw the ratio in its box.  Separated from drawBoxForRatio so the box can be drawn empty
 * @param g/*from ww w  .java 2 s.  c o m*/
 */
protected void drawRatioInBox(Graphics2D g) {
    drawBoxForRatio(g);

    g.setFont(new Font("Verdana", Font.PLAIN, 10));
    g.setColor(Color.BLACK);
    g.setPaint(Color.BLACK);
    g.drawString("Ratio: " + lastMousedRatio, 16, 24);
}

From source file:DrawShapes_2008.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    // Paint a gradient for the sky
    GradientPaint background = new GradientPaint(0f, 0f, Color.GRAY.darker(), 0f, (float) getHeight(),
            Color.GRAY.brighter());
    g2d.setPaint(background);
    g2d.fillRect(0, 0, getWidth(), 4 * getHeight() / 5);

    // Paint a gradient for the ground
    background = new GradientPaint(0f, (float) 4 * getHeight() / 5, Color.BLACK, 0f, (float) getHeight(),
            Color.GRAY.darker());
    g2d.setPaint(background);//from w w  w  . ja  v  a 2  s  .c  om
    g2d.fillRect(0, 4 * getHeight() / 5, getWidth(), getHeight() / 5);

    // Enable anti-aliasing to get smooth outlines
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Iterate through all of the current shapes
    for (Shape shape : shapes) {
        // Get the bounds to compute the RadialGradient properties
        Rectangle rect = shape.getBounds();
        Point2D center = new Point2D.Float(rect.x + (float) rect.width / 2.0f,
                rect.y + (float) rect.height / 2.0f);
        float radius = (float) rect.width / 2.0f;
        float[] dist = { 0.1f, 0.9f };
        Color[] colors = { Color.WHITE, Color.BLACK };

        // Create and set a RadialGradient centered on the object,
        // going from white at the center to black at the edges
        RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
        g2d.setPaint(paint);

        // Finally, render our shape
        g2d.fill(shape);
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;

    Point2D.Float p1 = new Point2D.Float(150.f, 75.f);
    Point2D.Float p2 = new Point2D.Float(250.f, 75.f);
    float width = 300;
    float height = 50;
    GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY);
    Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g1);
    g2D.fill(rect1);/*  w  w w.  j  a v  a 2s .  c om*/
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Cyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);

    p1.setLocation(150, 200);
    p2.setLocation(250, 200);
    GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false);
    rect1.setRect(p1.x - 100, p1.y - 25, width, height);
    g2D.setPaint(g2);
    g2D.fill(rect1);
    g2D.setPaint(Color.BLACK);
    g2D.draw(rect1);
    g2D.draw(new Line2D.Float(p1, p2));
    g2D.drawString("Acyclic Gradient Paint", p1.x - 100, p1.y - 50);
    g2D.drawString("p1", p1.x - 20, p1.y);
    g2D.drawString("p2", p2.x + 10, p2.y);
}