Set Text Attribute : Texture « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » TextureScreenshots 
Set Text Attribute
  

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GraphicAttribute;
import java.awt.font.ImageGraphicAttribute;
import java.awt.font.ShapeGraphicAttribute;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

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

public class AttributesApp extends JPanel {
  String text = "Java Source and Support";

  AttributedString attribString;

  AttributedCharacterIterator attribCharIterator;

  Image image;
  public static void main(String arg[]) {
    JFrame frame = new JFrame();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.getContentPane().add("Center"new AttributesApp());
    frame.setSize(500200);
    frame.setVisible(true);
  }

  public AttributesApp() {
    setBackground(Color.lightGray);
    setSize(500200);

    attribString = new AttributedString(text);

    GeneralPath star = new GeneralPath();
    star.moveTo(00);
    star.lineTo(1030);
    star.lineTo(-1010);
    star.lineTo(1010);
    star.lineTo(-1030);
    star.closePath();
    GraphicAttribute starShapeAttr = new ShapeGraphicAttribute(star,
        GraphicAttribute.TOP_ALIGNMENT, false);
    attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT,
        starShapeAttr, 01);
    attribString.addAttribute(TextAttribute.FOREGROUND, new Color(255255,
        0)01);

    int index = text.indexOf("Java Source");
    attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue, index,
        index + 7);
    Font font = new Font("sanserif", Font.ITALIC, 40);
    attribString.addAttribute(TextAttribute.FONT, font, index, index + 7);

    loadImage();
    BufferedImage bimage = new BufferedImage(image.getWidth(this), image
        .getHeight(this), BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bimage.createGraphics();
    big.drawImage(image, null, this);
    GraphicAttribute javaImageAttr = new ImageGraphicAttribute(bimage,
        GraphicAttribute.TOP_ALIGNMENT, 00);

    index = text.indexOf("Java");
    attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT,
        javaImageAttr, index - 1, index);

    font = new Font("serif", Font.BOLD, 60);
    attribString.addAttribute(TextAttribute.FONT, font, index, index + 4);
    attribString.addAttribute(TextAttribute.FOREGROUND, new Color(24363,
        163), index, index + 4)// Start and end indexes.

    index = text.indexOf("source");
    attribString.addAttribute(TextAttribute.UNDERLINE,
        TextAttribute.UNDERLINE_ON, index, index + 2);

    index = text.indexOf("and support");
    font = new Font("sanserif", Font.ITALIC, 40);
    attribString.addAttribute(TextAttribute.FONT, font, index, index + 10);

    attribString.addAttribute(TextAttribute.FOREGROUND, Color.white, index,
        index + 2)// Start and end indexes.
    attribString.addAttribute(TextAttribute.FOREGROUND, Color.blue,
        index + 3, index + 10)// Start and end indexes.
  }

  public void loadImage() {
    image = Toolkit.getDefaultToolkit().getImage("largeJava2sLogo.gif");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(image, 1);
    try {
      mt.waitForAll();
    catch (Exception e) {
      System.out.println("Exception while loading.");
    }

    if (image.getWidth(this== -1) {
      System.out.println("no images");
      System.exit(0);
    }
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2Dg;

    attribCharIterator = attribString.getIterator();

    FontRenderContext frc = new FontRenderContext(null, false, false);
    TextLayout layout = new TextLayout(attribCharIterator, frc);

    layout.draw(g2, 20100);
  }
}
           
         
    
  
Related examples in the same category
1.A texture is a bitmap image applied to the surface in computer graphics.
2.TexturePaint DemoTexturePaint Demo
3.Text effect: transparentText effect: transparent
4.Draw text along a curveDraw text along a curve
5.Text with a Texture
6.A texture is a bitmap image applied to a shape
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.