Results 1 to 1 of 1
-
How to use TextAttribute class to change color and font of a text
Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.font.TextAttribute; import java.text.AttributedString; import javax.swing.JFrame; import javax.swing.JPanel; public class IteratorTest extends JPanel { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; String s = "Java Source and Support"; Dimension d = getSize(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font serifFont = new Font("Serif", Font.PLAIN, 48); Font sansSerifFont = new Font("Monospaced", Font.PLAIN, 48); AttributedString as = new AttributedString(s); as.addAttribute(TextAttribute.FONT, serifFont); as.addAttribute(TextAttribute.FONT, sansSerifFont, 2, 5); as.addAttribute(TextAttribute.FOREGROUND, Color.red, 5, 6); as.addAttribute(TextAttribute.FOREGROUND, Color.red, 16, 17); g2.drawString(as.getIterator(), 40, 80); } public static void main(String[] args) { JFrame f = new JFrame(); f.getContentPane().add(new IteratorTest()); f.setSize(850, 250); f.show(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
How to change color of JTable row having a particular value
By johnt in forum AWT / SwingReplies: 6Last Post: 05-14-2011, 06:48 AM -
How to change font/ font color etc in a graphic object using JCombobox?
By JavaInLove in forum AWT / SwingReplies: 5Last Post: 04-25-2009, 08:00 PM -
Change the color in my program
By carl in forum New To JavaReplies: 5Last Post: 04-03-2009, 12:20 PM -
How to change backgrnd color of link(active)
By Captainz in forum New To JavaReplies: 1Last Post: 05-02-2008, 11:10 PM -
How to change TXT color Onclick
By dave700800 in forum New To JavaReplies: 1Last Post: 12-08-2007, 01:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks