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:GradientPaintEllipse.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    int x = 5;//from  w  w w  .ja  v a  2s  . c  o  m
    int y = 7;

    // fill Ellipse2D.Double
    GradientPaint redtowhite = new GradientPaint(x, y, Color.red, 200, y, Color.white);
    g2.setPaint(redtowhite);
    g2.fill(new Ellipse2D.Double(x, y, 200, 200));
    g2.setPaint(Color.black);
    g2.drawString("Filled Ellipse2D", x, 250);
}

From source file:RoundGradientPaintFill.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    RoundRectangle2D r = new RoundRectangle2D.Float(5, 5, 150, 150, 25, 25);
    RoundGradientPaint rgp = new RoundGradientPaint(75, 75, Color.magenta, new Point2D.Double(0, 85),
            Color.blue);/* w  ww. j a  va  2s .  co  m*/
    g2.setPaint(rgp);
    g2.fill(r);
}

From source file:OptionDialogTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    Rectangle2D rect = new Rectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1);
    g2.setPaint(Color.YELLOW);
    g2.fill(rect);/*w  w  w.  ja  v a2  s  .c  om*/
    g2.setPaint(Color.BLUE);
    g2.draw(rect);
}

From source file:org.jfree.chart.demo.CircleDrawer.java

/**
 * Draws the circle.//from   w  w  w  .  j  ava2 s. c  om
 * 
 * @param g2  the graphics device.
 * @param area  the area in which to draw.
 */
public void draw(final Graphics2D g2, final Rectangle2D area) {
    final Ellipse2D ellipse = new Ellipse2D.Double(area.getX(), area.getY(), area.getWidth(), area.getHeight());
    if (this.fillPaint != null) {
        g2.setPaint(this.fillPaint);
        g2.fill(ellipse);
    }
    if (this.outlinePaint != null && this.outlineStroke != null) {
        g2.setPaint(this.outlinePaint);
        g2.setStroke(this.outlineStroke);
        g2.draw(ellipse);
    }

    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(1.0f));
    final Line2D line1 = new Line2D.Double(area.getCenterX(), area.getMinY(), area.getCenterX(),
            area.getMaxY());
    final Line2D line2 = new Line2D.Double(area.getMinX(), area.getCenterY(), area.getMaxX(),
            area.getCenterY());
    g2.draw(line1);
    g2.draw(line2);
}

From source file:Main.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    GradientPaint gp1 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);

    gp1.createContext(ColorModel.getRGBdefault(), new Rectangle(0, 0, 30, 40), new Rectangle(0, 0, 30, 40),
            new AffineTransform(), null);

    System.out.println(gp1.getTransparency());
    g2d.setPaint(gp1);
    g2d.fillRect(20, 20, 300, 40);/*from w  w  w.j av  a 2s  .com*/

}

From source file:org.n52.oxf.render.sos.ScatterPlotChartRenderer.java

@Override
public void render(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, CrosshairState crosshairState) {
    float[][] data = getData();

    g2.setPaint(Color.red);

    if (super.getData() != null) {
        for (int i = 0; i < data[0].length; i++) {
            float x = data[0][i];
            float y = data[1][i];

            int transX = (int) getDomainAxis().valueToJava2D(x, dataArea, RectangleEdge.BOTTOM);
            int transY = (int) getRangeAxis().valueToJava2D(y, dataArea, RectangleEdge.LEFT);
            g2.fillRect(transX, transY, dotSize, dotSize);
        }//from  w w w.  j  a va 2 s.  co m
    }
}

From source file:GradientPanel.java

public void paintComponent(Graphics g) {
    if (isOpaque()) {
        super.paintComponent(g);
        return;//from w  w  w . j  a v a2s.  com
    }

    int width = getWidth();
    int height = getHeight();

    // Create the gradient paint
    GradientPaint paint = null;

    Color sc = getForeground();
    Color ec = getBackground();

    switch (direction) {
    case HORIZONTAL: {
        paint = new GradientPaint(0, height / 2, sc, width, height / 2, ec, cyclic);
        break;
    }
    case VERTICAL: {
        paint = new GradientPaint(width / 2, 0, sc, width / 2, maxLength > 0 ? maxLength : height, ec, cyclic);
        break;
    }
    case DIAGONAL_LEFT: {
        paint = new GradientPaint(0, 0, sc, width, height, ec, cyclic);
        break;
    }
    case DIAGONAL_RIGHT: {
        paint = new GradientPaint(width, 0, sc, 0, height, ec, cyclic);
        break;
    }
    }

    if (paint == null) {
        throw new RuntimeException("Invalid direction specified in GradientPanel");
    }

    // we need to cast to Graphics2D for this operation
    Graphics2D g2d = (Graphics2D) g;

    // save the old paint
    Paint oldPaint = g2d.getPaint();

    // set the paint to use for this operation
    g2d.setPaint(paint);

    // fill the background using the paint
    g2d.fillRect(0, 0, width, height);

    // restore the original paint
    g2d.setPaint(oldPaint);

    super.paintComponent(g);
}

From source file:ImageBorderHack.java

public void fillTexture(Graphics2D g2, Image img, int x, int y, int w, int h) {
    BufferedImage buff = createBufferedImage(img);
    Rectangle anchor = new Rectangle(x, y, img.getWidth(null), img.getHeight(null));
    TexturePaint paint = new TexturePaint(buff, anchor);
    g2.setPaint(paint);
    g2.fillRect(x, y, w, h);//  ww w. j a  va  2s.c o  m
}

From source file:TexturePaintDemo.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
    Graphics2D big = bi.createGraphics();
    big.setColor(Color.blue);/*from  w  ww.ja va 2s .c o m*/
    big.fillRect(0, 0, 5, 5);
    big.setColor(Color.lightGray);
    big.fillOval(0, 0, 5, 5);
    Rectangle r = new Rectangle(0, 0, 5, 5);
    g2.setPaint(new TexturePaint(bi, r));

    Rectangle rect = new Rectangle(5, 5, 200, 200);

    g2.fill(rect);
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.legend.ColoredBlockContainer.java

/**
 * Draws a colored background. Returns the area wich has been filled.
 *//*w ww. j a v  a2s .  c om*/
private Rectangle2D drawFill(Graphics2D g2, Rectangle2D area) {
    Rectangle2D filledArea = (Rectangle2D) area.clone();
    filledArea = trimMargin(filledArea);
    filledArea = trimBorder(filledArea);
    area = trimPadding(area);
    g2.setPaint(this.fillPaint);
    g2.fill(filledArea);
    drawBorder(g2, filledArea);
    return filledArea;
}