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

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

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);/* w ww  . j a  v a  2s . co  m*/

    String s = "www.java2s.com";
    float x = 50, y = 150;

    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.lightGray);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}

From source file:edu.ku.brc.ui.GradiantButton.java

/**
 * Draws the button body//w w w  .  j a  v  a  2 s.c o m
 * @param g2 the graphics to be painted into
 * @param w the width of the control
 * @param h the height of the control
 * @param color the of the background
 */
protected void drawButtonBody(Graphics2D g2, int w, int h, Color color) {
    // draw the button body
    Color grad_top = color.brighter();
    Color grad_bot = color.darker();
    GradientPaint bg = new GradientPaint(new Point(0, 0), grad_top, new Point(0, h), grad_bot);
    g2.setPaint(bg);
    g2.fillRect(0, 0, w, h);
}

From source file:TexturedText.java

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

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Times New Roman", Font.PLAIN, 72);
    g2.setFont(font);/*from   w  w w.  j av  a2 s .  c o  m*/

    String s = "Java Source and Support";
    Dimension d = getSize();
    float x = 20, y = 100;

    BufferedImage bi = getTextureImage();
    Rectangle r = new Rectangle(0, 0, bi.getWidth(), bi.getHeight());
    TexturePaint tp = new TexturePaint(bi, r);
    g2.setPaint(tp);

    g2.drawString(s, x, y);
}

From source file:org.jcurl.core.swing.SumDisplayBase.java

protected void paintRock(final Graphics2D g2, final int idx8, final boolean isDark) {
    final float r = 0.35F * getWidth();
    // vertical display:
    final float cx = 0.5F * getWidth();
    float cy = getHeight() / RockSet.ROCKS_PER_SET;
    if (isDark)/*from   www  .  j a v a2s.  co m*/
        cy *= idx8 + 0.5F;
    else
        cy *= RockSet.ROCKS_PER_SET - (idx8 + 0.5F);
    g2.setPaint(isDark ? colors.dark : colors.light);
    g2.fillArc((int) (cx - r), (int) (cy - r), (int) (2 * r), (int) (2 * r), 0, 360);
    g2.setColor(Color.BLACK);
    g2.drawArc((int) (cx - r), (int) (cy - r), (int) (2 * r), (int) (2 * r), 0, 360);
}

From source file:Textures.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(new Color(212, 212, 212));
    g2d.drawRect(10, 15, 90, 60);//from  ww  w . ja  va 2  s .c  o  m

    BufferedImage bimage1 = null;

    URL url1 = ClassLoader.getSystemResource("a.png");

    try {
        bimage1 = ImageIO.read(url1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    Rectangle rect1 = new Rectangle(0, 0, bimage1.getWidth(), bimage1.getHeight());
    TexturePaint texture1 = new TexturePaint(bimage1, rect1);

    g2d.setPaint(texture1);
    g2d.fillRect(10, 15, 90, 60);
}

From source file:org.jcurl.core.swing.CurvePainter.java

/**
 * Paint all curves of a store, delegate to
 * {@link #doPaint(Graphics2D, Iterator, double[], float, double[], double[], double[], double[])}.
 * //w  ww .  ja va 2 s .  c o  m
 * @param g2
 *            where to draw
 * @param cs
 *            store
 */
public void doPaint(final Graphics2D g2, final Iterable<Iterable<Entry<Double, R1RNFunction>>> cs) {
    g2.setStroke(stroke);
    int i = 0;
    for (final Iterable<Entry<Double, R1RNFunction>> name : cs) {
        if (log.isDebugEnabled())
            log.debug("i=" + i + " " + (RockSet.isDark(i) ? "dark" : "light") + " " + RockSet.toIdx8(i));
        g2.setPaint(RockSet.isDark(i++) ? dark : light);
        doPaint(g2, name.iterator(), sections, 1, t1, t2, t3, t4);
    }
}

From source file:Main.java

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

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);/* www . j a  v a 2  s. c  om*/

    String s = "this is a test";
    float x = 50, y = 150;

    // Draw the baseline.
    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.red);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}

From source file:net.sourceforge.processdash.ui.lib.chart.StandardDiscItemRenderer.java

protected void drawDisc(Graphics2D g2, Ellipse2D shape, Paint discPaint, int item) {
    g2.setPaint(discPaint);
    g2.fill(shape);
}

From source file:Main.java

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

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

    int cx = getSize().width / 2;
    int cy = getSize().height / 2;

    g2.translate(cx, cy);/*from  ww  w  .ja v a2s  .  c  o m*/
    g2.rotate(theta * Math.PI / 180);

    Shape oldClip = g2.getClip();
    Shape e = new Ellipse2D.Float(-cx, -cy, cx * 2, cy * 2);
    g2.clip(e);

    Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 2);
    g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
    g2.fill(c);

    g2.setPaint(Color.yellow);
    g2.fillOval(cx / 4, 0, cx, cy);

    g2.setClip(oldClip);

    g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
    g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
    g2.drawString("Hello, 2D!", -cx * 3 / 4, cy / 4);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) .75);
    g2.setComposite(ac);

    Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4, cx * 3 / 4, cy * 3 / 4, 20, 20);
    g2.setStroke(new BasicStroke(4));
    g2.setPaint(Color.magenta);
    g2.fill(r);
    g2.setPaint(Color.green);
    g2.draw(r);

    g2.drawImage(image, -cx / 2, -cy / 2, this);
}

From source file:LineMetricsIllustration.java

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

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    Font font = new Font("Serif", Font.PLAIN, 72);
    g2.setFont(font);/*from   ww  w .j  ava2s.  c  om*/

    String s = "Java Source and Support";
    float x = 50, y = 150;

    // Draw the baseline.
    FontRenderContext frc = g2.getFontRenderContext();
    float width = (float) font.getStringBounds(s, frc).getWidth();
    Line2D baseline = new Line2D.Float(x, y, x + width, y);
    g2.setPaint(Color.red);
    g2.draw(baseline);

    // Draw the ascent.
    LineMetrics lm = font.getLineMetrics(s, frc);
    Line2D ascent = new Line2D.Float(x, y - lm.getAscent(), x + width, y - lm.getAscent());
    g2.draw(ascent);

    // Draw the descent.
    Line2D descent = new Line2D.Float(x, y + lm.getDescent(), x + width, y + lm.getDescent());
    g2.draw(descent);

    // Draw the leading.
    Line2D leading = new Line2D.Float(x, y + lm.getDescent() + lm.getLeading(), x + width,
            y + lm.getDescent() + lm.getLeading());
    g2.draw(leading);

    // Render the string.
    g2.setPaint(Color.black);
    g2.drawString(s, x, y);
}