用java.awt.font.TextAttribute类
它扩展了java.text包中的AttributeCharacterIteraor.Attribute类
使用TextAttribute的属性字段UNDERLINE就可以了
如
AttributedString attribstring;
attribstring.addAttribute(TextAttribute.UNDELINE,TextAttribute.UNDELINE_ON,index,index+2)
index 是字窜的开始
index+2 是字窜的末尾
它扩展了java.text包中的AttributeCharacterIteraor.Attribute类
使用TextAttribute的属性字段UNDERLINE就可以了
如
AttributedString attribstring;
attribstring.addAttribute(TextAttribute.UNDELINE,TextAttribute.UNDELINE_ON,index,index+2)
index 是字窜的开始
index+2 是字窜的末尾
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.*;class DisplayPanel extends JFrame{
String text="This is Java 2D Text!!!";
AttributedString attribString;
AttributedCharacterIterator attribCharIterator;
Image javaImage;
public DisplayPanel(){
//4.Prepare the dispaly panel with sutable size and background
setBackground(Color.lightGray);
setForeground(Color.blue);
setSize(500,200);
//5.Create the attributed string object using the given text
attribString=new AttributedString(text);
//6.add a star shaped graphics attribute in the beginning of the
//given text.
GeneralPath star =new GeneralPath();
star.moveTo(0,0);// x an y coordinates;
star.lineTo(10,30);
star.lineTo(-10,10);
star.lineTo(10,10);
star.lineTo(-10,30);
GraphicAttribute starShapeAttr=new ShapeGraphicAttribute(star,
GraphicAttribute.TOP_ALIGNMENT,
false); attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT,/*Attribute Key*/
starShapeAttr,/*Attribute Value*/
0,/* Begin Index */
1);/* End Index*/
attribString.addAttribute(TextAttribute.FOREGROUND,
new Color(255,255,0),
0,1);//Start and End indexes
//7.Modify the forground and font attributes of "this is"
//in the given string text.
int index=text.indexOf("This is");
attribString.addAttribute(TextAttribute.FOREGROUND,
Color.blue,
index,index+7);//start and end indexes.
Font font=new Font("sanserif",Font.ITALIC,40);
attribString.addAttribute(TextAttribute.FONT,font,index,index+7);
/*
//8.Load the specified image and prepare the image attribute
loadImage();
BufferedImage bimage=new BufferedImage(javaImage.getWidth(this),
javaImage.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
Graphics2D big=bimage.createGraphics();
big.drawImage(javaImage,null,this);
GraphicAttribute javaImageAttr=new ImageGraphicAttribute(bimage,
GraphicAttribute.TOP_ALIGNMENT,
0,0);
//9.Add the image attribute before the "Java" subString
index=text.indexOf("Java");
attribString.addAttribute(TextAttribute.CHAR_REPLACEMENT,
javaImageAttr,index-1,index);
*/
//10.Modify the attributes of the substring "Java"
font=new Font("serif",Font.BOLD,60);
attribString.addAttribute(TextAttribute.FONT,font,index,index+4);
attribString.addAttribute(TextAttribute.FOREGROUND,
new Color(243,63,163),
index,index+4);//Start and end indexs.
//11.Underline the SubString "2D"
index=text.indexOf("2D");
attribString.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON,
index,index+2);
//12.Modify the font sizes after the substring Java.
index=text.indexOf("2D Text!!!");
font=new Font("sanserif",Font.ITALIC,40);
attribString.addAttribute(TextAttribute.FONT,font,index,index+10);
//13.Change the color of "2D" to white and the color of rmaining
//text to blue
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.
}
//14.this mehtod loads the specifed image
public void loadImage(){
//create an iamge object using the specified file
javaImage=Toolkit.getDefaultToolkit().getImage("Image/java.gif");
MediaTracker mt=new MediaTracker(this);
mt.addImage(javaImage,1);
try{
mt.waitForAll();
}catch (Exception e){
System.out.println("Exception while loading");
}
//if the iamge ha an unknown width ,the image is not created
//by using the suggested file. therefore, exit the program.
if (javaImage.getWidth(this)==-1){
System.out.println("*** Make sure you have the suggested image"
+"(ava.gif)file in the iamge directory.***");
System.exit(0);
}
}
//15.the paintcomponet method...
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
//retrive the charater iterator from the attributed string
attribCharIterator=attribString.getIterator();
//create a font render contex object and text layout
FontRenderContext frc=new FontRenderContext(null,false,false);
TextLayout layout=new TextLayout(attribCharIterator,frc);
//draw the string
layout.draw(g2,20,100);
}
public static void main (String args[]){
DisplayPanel test= new DisplayPanel();
test.show();
}
}
你應該能 明白了吧!