Powered by Blogger.

Java:~ code for change Font in javaScript Program

Description:

The java.awt.Font class is used to create Font objects to set the font for drawing text, labels, text fields, buttons, etc.

Example:

       JButton b = new JButton("OK");
       b.setFont(new Font("sansserif", Font.BOLD, 32));

Available System Fonts:

For maximum portability, use the generic font names, but you can use any font installed in the system. It is suggested to use a font family name, and create the font from that, but you can also use the fonts directly. You can get an array of all available font family names or all fonts.

 // Font info is obtained from the current graphics environment.

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

//--- Get an array of font names (smaller than the number of fonts)

String[] fontNames = ge.getAvailableFontFamilyNames();

//--- Get an array of fonts.  It's preferable to use the names above.

Font[] allFonts = ge.getAllFonts();

Using Fonts for Graphics:

      Font f;
      f = new Font(String name, int style, int size);    // creates a new font
name is "Serif", "SansSerif", or "Monospaced", or a font on the system. style is Font.PLAIN. Font.BOLD, Font.ITALIC, or Font.BOLD+Font.ITALIC. size is the point size, typically in the range 8-48.

Example:

Font big = new Font("SansSerif", Font.Bold, 48);
. . .
g.setFont(big);
g.drawString("Greetings Earthling");

Fonts in Java:

In Java, Font class manages the font of Strings. The constructor of Font class takes three arguments: the font name, font style, and font size. The font name is any font currently supported by the system where the program is running such as standard Java fonts Monospaced, SansSerif, and Serif. The font style is Font.PLAIN, Font.ITALIC, or Font.BOLD. Font styles can be used in combination such as: Font.ITALIC + Font.BOLD.

Fonts in Java Example:

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

 public class TestFonts extends JFrame{
   private static final long serialVersionUID=0;

   public TestFonts(){
     super(”Font Examples”);
     setSize(300, 150);
     show();
   }

   public void paint(Graphics g){
     g.drawString(”Welcome to Java.”, 20, 50);
     g.setColor(Color.red);
     g.setFont(new Font(”Monospaced”, Font.BOLD, 14));
     g.drawString(”Welcome to Java.”, 20, 70);
     g.setColor(Color.blue);
     g.setFont(new Font(”SansSerif”, Font.BOLD +Font. ITALIC, 16));
     g.drawString(”Welcome to Java.”, 20, 90);
   }

   public static void main(String[]args){
     TestFonts tf = new TestFonts();
     tf.addWindowListener(
        new WindowAdapter(){
          public void windowClosing(WindowEvent e){
            System.exit(0);
          }
        }
      ) ;
    }
 }

Code for Change Font of Text in Java:

import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.font.*;

public class ShowColorAndFont extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    String text = "Java is an Object Oriented Programming Language.";
    Dimension dimension = getSize();

    Font font1 = new Font("Book Antiqua", Font.PLAIN, 30);
    Font font2 = new Font("Monotype Corsiva", Font.PLAIN, 30);

    AttributedString attributedString = new AttributedString(text);
    attributedString.addAttribute(TextAttribute.FONT, font1);
    attributedString.addAttribute(TextAttribute.FONT, font2, 1, 4);
   attributedString.addAttribute(TextAttribute.FONT, font2, 6, 7);
   attributedString.addAttribute(TextAttribute.FONT, font2, 9, 10);
   attributedString.addAttribute(TextAttribute.FONT, font2, 12, 17);
   attributedString.addAttribute(TextAttribute.FONT, font2, 19, 26);
   attributedString.addAttribute(TextAttribute.FONT, font2, 28, 38);
   attributedString.addAttribute(TextAttribute.FONT, font2, 40, 47);

  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 1);
  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 5, 6);
  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 8, 9);
  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 11, 12);
  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 18, 19);
  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 27, 28);
  attributedString.addAttribute(TextAttribute.FOREGROUND, Color.red, 39, 40);
  
    g2d.drawString(attributedString.getIterator(), 40, 80);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Show Color and Font");
    frame.getContentPane().add(new ShowColorAndFont());
    frame.setSize(700, 200);
    frame.show();

  }

2 comments:

  1. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete
  2. I appreciate you sharing this article. Really thank you! Much obliged.
    This is one awesome blog article. Much thanks again.

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete