Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Show String in Oval 
 

To show a string in oval, we are providing you an example. A font 'Book Antiqua' is defined to display the string 'WELCOME'.

 

Show String in Oval

                         

This section illustrates you how to show the string in Oval.

To show a string in oval, we are providing you an example. A font 'Book Antiqua' is defined to display the string 'WELCOME'. To render the particular font on the screen, we have used the class FontMetrics. The object of FontMetrics class encapsulates all the information. 

The method drawOval() draw an oval shape. The char ch = st.charAt(k) retrieves the k character on each iteration. The method getHeight() of class FontMetrics  gets the standard height of a line of text in the given font. The method charWidth(ch) of class FontMetrics returns the width of the specified character in the given font. 

Following code draws the string in oval:

g.drawString(String.valueOf(ch), x, y + h);

Here is the code of ShowFontInOval.java

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

public class ShowFontInOval extends JPanel {
  public void paint(Graphics g) {
    g.setFont(new Font("Book Antiqua",0,90));
    g.setColor(Color.cyan);
    FontMetrics fontMetrics = getFontMetrics(new Font
       (
"Book Antiqua",0,90));
    String st = "WELCOME";
    int x = 1;
    int y = 1;
    for (int k = 0; k < st.length(); k++) {
      char ch = st.charAt(k);
      int h = fontMetrics.getHeight();
      int w = fontMetrics.charWidth(ch);
    g.drawOval(x, y, w, h);
    g.drawString(String.valueOf(ch), x, y + h);
      x= x + w;
    }
  }
 public static void main(String[] args) {
   JFrame f = new JFrame("Show string in Oval");
     f.setContentPane(new ShowFontInOval());
     f.setSize(520300);
     f.setVisible(true);
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.