Text effect: image texture : Gradient Paint « 2D Graphics GUI « Java






Text effect: image texture

    

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TexturedText extends JPanel {
  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);

    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);
  }

  private BufferedImage getTextureImage() {
    // Create the test image.
    int size = 8;
    BufferedImage bi = new BufferedImage(size, size,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bi.createGraphics();
    g2.setPaint(Color.red);
    g2.fillRect(0, 0, size / 2, size / 2);
    g2.setPaint(Color.yellow);
    g2.fillRect(size / 2, 0, size, size / 2);
    g2.setPaint(Color.green);
    g2.fillRect(0, size / 2, size / 2, size);
    g2.setPaint(Color.blue);
    g2.fillRect(size / 2, size / 2, size, size);
    return bi;
  }

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.getContentPane().add(new TexturedText());
    f.setSize(800, 250);
    f.show();

  }
}

           
         
    
    
    
  








Related examples in the same category

1.Gradients: a smooth blending of shades from light to dark or from one color to another
2.Gradient Shapes
3.GradientPaint demoGradientPaint demo
4.GradientPaint EllipseGradientPaint Ellipse
5.Another GradientPaint DemoAnother GradientPaint Demo
6.Text effect: rotation and transparentText effect: rotation and transparent
7.Texture paint Texture paint
8.Round GradientPaint Fill demoRound GradientPaint Fill demo
9.GradientPaint: ironGradientPaint: iron
10.Color gradientColor gradient
11.Drawing with a Gradient Color
12.A non-cyclic gradient
13.A cyclic gradient
14.PaintsPaints
15.Horizontal Gradients
16.Vertical Gradient Paint
17.Gradients in the middle
18.Control the direction of Gradients
19.Returns true if the two Paint objects are equal OR both null.
20.Gradient effects