Important alert: (current site time 7/15/2013 12:20:12 PM EDT)
 

VB icon

FontStyleSelect

Email
Submitted on: 3/9/2007 4:48:52 AM
By: MICHAEL ESPIRITU 
Level: Beginner
User Rating: By 2 Users
Compatibility: JavaScript
Views: 3673
(About the author)
 
     Write An applet uses to Checkboxes objectto change the font style of the text displayed in the JTextfield one checkboxes applies a bold stylewhen selected and the other applies an italic style when selected. ifboth are selected the style of the font is bold and italic
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :FontStyleSelect
'**************************************
<html>
<applet code=FontStyleSelect.class height=150 width=300></applet>
</html>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: FontStyleSelect
' Description:Write An applet uses to Checkboxes objectto change the font style of the text displayed in the 
JTextfield one checkboxes applies a bold stylewhen selected and the other applies an italic style when selected. 
ifboth are selected the style of the font is bold and italic
' By: MICHAEL ESPIRITU
'
' Assumes:Write An applet uses to Checkboxes objectto change the font style of the text displayed in the 
JTextfield one checkboxes applies a bold stylewhen selected and the other applies an italic style when selected. 
ifboth are selected the style of the font is bold and italic
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5589&lngWId=14'for details.'**************************************

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class FontStyleSelect extends Applet implements ItemListener
{
 	 private Checkbox ChkBold,ChkItalic;
 private CheckboxGroup group1;
 FontMetrics fontmetrics;
 String text="Michael Espiritu the Programmer";
 public Font font;
 public String fontName;
 public int fontStyle;
 public int fontSize;
public void init()
{
 	setLayout(null);
group1 = new CheckboxGroup();
/*Declaration for a New Object*/
 	ChkBold=new Checkbox ("Bold",true,group1);
	ChkItalic= new Checkbox ("Italic",false,group1);
	
	/*Set Object Size & Location*/
	ChkBold.setLocation(65,15);
	ChkBold.setSize(100,30);
ChkItalic.setLocation(170,15);
	ChkItalic.setSize(100,30);
	
/*Add the Object to the Applet*/
	add(ChkBold);
add(ChkItalic);
	
/*Add Action Listener to the Object*/
ChkBold.addItemListener(this);
ChkItalic.addItemListener(this);
/*Set the first Text Appearance*/
fontName="Arial";
fontStyle=Font.PLAIN;
fontSize=15;
}
public void paint(Graphics g)
{
 /*Note:
 Centering Text Using FontMetrics Had an Error 
 Specifically:
size().width [width of your applet]
size().height[height of your applet]
 
 Error:
 Note: example5.java uses or overrides a deprecated API.
 Note: Recompile with -deprecation for details.
 */
 /*I Made a Alternative*/
int x=0, y=0;
 /*I Used to get the Actual width & height of My Applet*/ 
int sWidth=285, sHeight=250;
font = new Font(fontName,fontStyle,fontSize);
g.setFont(font);
fontmetrics = getFontMetrics(font);
x=(sWidth-fontmetrics.stringWidth(text))/2;
y=(sHeight-fontmetrics.getHeight())/2;
g.setColor(Color.red);
g.drawString(text,x,y);
 
}
public void itemStateChanged(ItemEvent e)
{
 if(group1.getSelectedCheckbox()==ChkBold)
{
fontStyle=Font.BOLD;
fontSize=15;
repaint();
}
 if(group1.getSelectedCheckbox()==ChkItalic)
{
fontName="Arial";
fontStyle=Font.ITALIC;
fontSize=15;
repaint();
}
 
 }
}


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.