 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Roedy Green Guest
|
Posted: Tue May 18, 2004 5:55 am Post subject: Suggested Newbie project, FontShower |
|
|
Here is a simple project for a newbie. If you do it, I will help you
polish it, and I will post in on my site and give you the credit.
First you get a list of all possible java fonts installed on the
machine.
import java.awt.GraphicsEnvironment;
public class ShowFonts
{
public static void main ( String [] args )
{
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();
for ( int i=0; i<names.length; i++ )
{
System.out.println( names[i] );
}
}
}
It is an applet that lets you choose:
font family, style, size, foreground colour, background colour.
then it dislays the following test message in the selected font.
The quick brown fox jumped over the lazy dog's back.
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
01234567890
!"#$%&'()*+,-./:;<=>?@[^_z{|}~
uvw wW gq9 2z 5s il17|!j oO08 `'" ;: ,. m nn rn {[()]}u
You can use a JColorChooser for the foreground and background and
a JComboBox to select the family, style, and size, and use a JTextArea
for the test message, Or do it with AWT using ColorChooser, Choice and
TextArea.
It will be a useful tool for all Java programmers.
For hints on why I selected that test message see
http://mindprod.com/projproofreaderfont.html
You need to set up listeners so that if anything changes, you
recompute the display.
If you want to get fancy, display the font family menu using a font
sample, but be careful, some fonts don't work that way, like
dingbats.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue May 18, 2004 6:23 am Post subject: Re: Suggested Newbie project, FontShower |
|
|
On Tue, 18 May 2004 05:55:36 GMT, Roedy Green
<look-on (AT) mindprod (DOT) com.invalid> wrote or quoted :
| Quote: | Here is a simple project for a newbie. If you do it, I will help you
polish it, and I will post in on my site and give you the credit.
|
for future reference, the Font Shower project is more fully written up
at http://mindprod.com/projfontshower.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Fahd Shariff Guest
|
Posted: Tue May 18, 2004 3:30 pm Post subject: Re: Suggested Newbie project, FontShower |
|
|
see http://mindprod.com/jgloss/homework.html
/////
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
/**
* An applet that lets you choose the font family, size, style
* text color, background color and displays a message in the selected
* font.
*
* @author Fahd SHARIFF
* @version 1.4.2_01
*/
public class FontShower extends JApplet
{
private JToolBar toolBar ;
private JComboBox fonts, sizes ;
private JToggleButton boldButton, italicButton ;
private JButton bgButton, fgButton ;
private JTextArea textArea ;
private final String message = "The quick brown fox jumped over
the lazy dog's back.n"+
"abcdefghijklmnopqrstuvwxyzn"+
"ABCDEFGHIJKLMNOPQRSTUVWXYZn"+
"01234567890n"+
"!"#$%&'()*+,-./:;<=>?@[\^_z{|}~n"+
"uvw wW gq9 2z 5s il17|!j oO08 `'" ;: ,. m nn rn {[()]}un" ;
/**
* Initialise the applet
*/
public void init()
{
toolBar = new JToolBar() ;
toolBar.setRollover(true) ;
toolBar.setFloatable(false) ;
//font names
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
fonts = new JComboBox(fontNames) ;
fonts.setMaximumSize(fonts.getPreferredSize());
fonts.setEditable(true);
fonts.setSelectedItem("Arial") ;
fonts.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = textArea.getFont() ;
textArea.setFont(new Font(fonts.getSelectedItem().toString(),
font.getStyle(), font.getSize())) ;
}
});
toolBar.add(fonts) ;
toolBar.addSeparator() ;
//font sizes
String[] sizeItems = {"8", "9", "10", "11", "12", "14", "16", "18",
"20", "22", "24", "26", "28", "36", "48", "72"};
sizes = new JComboBox(sizeItems) ;
sizes.setMaximumSize(sizes.getPreferredSize());
sizes.setEditable(true);
sizes.setSelectedIndex(7) ;
sizes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String chosenSize = sizes.getSelectedItem().toString() ;
changeSize(chosenSize);
}
});
toolBar.add(sizes) ;
toolBar.addSeparator() ;
//bold button
boldButton = new JToggleButton("Bold",false) ;
boldButton.setMnemonic(KeyEvent.VK_B) ;
boldButton.setToolTipText("Bold") ;
boldButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeStyle() ;
}
});
toolBar.add(boldButton);
//italic button
italicButton = new JToggleButton("Italics" ,false) ;
italicButton.setMnemonic(KeyEvent.VK_I) ;
italicButton.setToolTipText("Italic") ;
italicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeStyle();
}
});
toolBar.add(italicButton);
toolBar.addSeparator() ;
//bg button
bgButton = new JButton("Background Color") ;
bgButton.setToolTipText("Change Background Color") ;
bgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color c = getColorChoice() ;
if(c!=null)textArea.setBackground(c) ;
}
});
toolBar.add(bgButton);
//fg button
fgButton = new JButton("Text Color") ;
fgButton.setToolTipText("Change Text Color") ;
fgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color c = getColorChoice() ;
if(c!=null)textArea.setForeground(c) ;
}
});
toolBar.add(fgButton);
toolBar.addSeparator() ;
//text area in a scrollpane
textArea = new JTextArea(message) ;
textArea.setEditable(false) ;
textArea.setBackground(Color.yellow) ;
textArea.setForeground(Color.blue) ;
textArea.setFont(new Font("Arial",Font.PLAIN,1 ) ;
JScrollPane scroller = new JScrollPane() ;
scroller.getViewport().add(textArea,null) ;
getContentPane().add(toolBar, BorderLayout.NORTH) ;
getContentPane().add(scroller, BorderLayout.CENTER) ;
}
/**
* Changes the size of the selected text with the specified size
*
* @param chosenSize the new size
*/
public void changeSize(String chosenSize)
{
Integer fontSize = new Integer(1 ;
try
{
fontSize = new Integer(Integer.parseInt(chosenSize)) ;
}
catch(NumberFormatException e) {e.printStackTrace() ;}
textArea.setFont(textArea.getFont().deriveFont(fontSize.floatValue()))
;
}
/**
* Changes the style (Bold, Italic) of the selected text by
checking the style buttons
*
*/
public void changeStyle()
{
if(!boldButton.isSelected() && !italicButton.isSelected())
textArea.setFont(textArea.getFont().deriveFont(Font.PLAIN)) ;
else if(boldButton.isSelected() && italicButton.isSelected())
textArea.setFont(textArea.getFont().deriveFont(3)) ;
else if(boldButton.isSelected())
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD)) ;
else textArea.setFont(textArea.getFont().deriveFont(Font.ITALIC)) ;
}
/**
* Displays a color chooser and returns the selected color.
*
* @return the selected Color
*/
public Color getColorChoice()
{
Color newColor = JColorChooser.showDialog(this,"Choose
Color",textArea.getBackground());
if(newColor!=null) return newColor ;
else return null ;
}
}
////
Fahd Shariff
"Let the code do the talking..."
|
|
| Back to top |
|
 |
Fahd Shariff Guest
|
Posted: Tue May 18, 2004 3:30 pm Post subject: Re: Suggested Newbie project, FontShower |
|
|
see http://mindprod.com/jgloss/homework.html
/////
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
/**
* An applet that lets you choose the font family, size, style
* text color, background color and displays a message in the selected
* font.
*
* @author Fahd SHARIFF
* @version 1.4.2_01
*/
public class FontShower extends JApplet
{
private JToolBar toolBar ;
private JComboBox fonts, sizes ;
private JToggleButton boldButton, italicButton ;
private JButton bgButton, fgButton ;
private JTextArea textArea ;
private final String message = "The quick brown fox jumped over
the lazy dog's back.n"+
"abcdefghijklmnopqrstuvwxyzn"+
"ABCDEFGHIJKLMNOPQRSTUVWXYZn"+
"01234567890n"+
"!"#$%&'()*+,-./:;<=>?@[\^_z{|}~n"+
"uvw wW gq9 2z 5s il17|!j oO08 `'" ;: ,. m nn rn {[()]}un" ;
/**
* Initialise the applet
*/
public void init()
{
toolBar = new JToolBar() ;
toolBar.setRollover(true) ;
toolBar.setFloatable(false) ;
//font names
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
fonts = new JComboBox(fontNames) ;
fonts.setMaximumSize(fonts.getPreferredSize());
fonts.setEditable(true);
fonts.setSelectedItem("Arial") ;
fonts.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = textArea.getFont() ;
textArea.setFont(new Font(fonts.getSelectedItem().toString(),
font.getStyle(), font.getSize())) ;
}
});
toolBar.add(fonts) ;
toolBar.addSeparator() ;
//font sizes
String[] sizeItems = {"8", "9", "10", "11", "12", "14", "16", "18",
"20", "22", "24", "26", "28", "36", "48", "72"};
sizes = new JComboBox(sizeItems) ;
sizes.setMaximumSize(sizes.getPreferredSize());
sizes.setEditable(true);
sizes.setSelectedIndex(7) ;
sizes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String chosenSize = sizes.getSelectedItem().toString() ;
changeSize(chosenSize);
}
});
toolBar.add(sizes) ;
toolBar.addSeparator() ;
//bold button
boldButton = new JToggleButton("Bold",false) ;
boldButton.setMnemonic(KeyEvent.VK_B) ;
boldButton.setToolTipText("Bold") ;
boldButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeStyle() ;
}
});
toolBar.add(boldButton);
//italic button
italicButton = new JToggleButton("Italics" ,false) ;
italicButton.setMnemonic(KeyEvent.VK_I) ;
italicButton.setToolTipText("Italic") ;
italicButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
changeStyle();
}
});
toolBar.add(italicButton);
toolBar.addSeparator() ;
//bg button
bgButton = new JButton("Background Color") ;
bgButton.setToolTipText("Change Background Color") ;
bgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color c = getColorChoice() ;
if(c!=null)textArea.setBackground(c) ;
}
});
toolBar.add(bgButton);
//fg button
fgButton = new JButton("Text Color") ;
fgButton.setToolTipText("Change Text Color") ;
fgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color c = getColorChoice() ;
if(c!=null)textArea.setForeground(c) ;
}
});
toolBar.add(fgButton);
toolBar.addSeparator() ;
//text area in a scrollpane
textArea = new JTextArea(message) ;
textArea.setEditable(false) ;
textArea.setBackground(Color.yellow) ;
textArea.setForeground(Color.blue) ;
textArea.setFont(new Font("Arial",Font.PLAIN,1 ) ;
JScrollPane scroller = new JScrollPane() ;
scroller.getViewport().add(textArea,null) ;
getContentPane().add(toolBar, BorderLayout.NORTH) ;
getContentPane().add(scroller, BorderLayout.CENTER) ;
}
/**
* Changes the size of the selected text with the specified size
*
* @param chosenSize the new size
*/
public void changeSize(String chosenSize)
{
Integer fontSize = new Integer(1 ;
try
{
fontSize = new Integer(Integer.parseInt(chosenSize)) ;
}
catch(NumberFormatException e) {e.printStackTrace() ;}
textArea.setFont(textArea.getFont().deriveFont(fontSize.floatValue()))
;
}
/**
* Changes the style (Bold, Italic) of the selected text by
checking the style buttons
*
*/
public void changeStyle()
{
if(!boldButton.isSelected() && !italicButton.isSelected())
textArea.setFont(textArea.getFont().deriveFont(Font.PLAIN)) ;
else if(boldButton.isSelected() && italicButton.isSelected())
textArea.setFont(textArea.getFont().deriveFont(3)) ;
else if(boldButton.isSelected())
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD)) ;
else textArea.setFont(textArea.getFont().deriveFont(Font.ITALIC)) ;
}
/**
* Displays a color chooser and returns the selected color.
*
* @return the selected Color
*/
public Color getColorChoice()
{
Color newColor = JColorChooser.showDialog(this,"Choose
Color",textArea.getBackground());
if(newColor!=null) return newColor ;
else return null ;
}
}
////
Fahd Shariff
"Let the code do the talking..."
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Tue May 18, 2004 4:06 pm Post subject: Re: Suggested Newbie project, FontShower |
|
|
On 18 May 2004 08:30:20 -0700, Fahd Shariff wrote:
| Quote: | "Let the code do the talking..."
|
A good philosophy Fahd, but could I
get you to look into the fact that
your newsreader seems to have an
echo? echo..? echo...? ;-)
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue May 18, 2004 11:24 pm Post subject: Re: Suggested Newbie project, FontShower |
|
|
On 18 May 2004 08:30:20 -0700, [email]fahdshariff (AT) yahoo (DOT) com[/email] (Fahd Shariff)
wrote or quoted :
| Quote: | /**
* An applet that lets you choose the font family, size, style
* text color, background color and displays a message in the selected
* font.
*
* @author Fahd SHARIFF
* @version 1.4.2_01
|
I have polished your code a bit, adding an about box, some more
pangrams and samples and made it run also standalone.
See http://mindprod.com/fontshower.html
I changed the default font, since the Arial you had is
windows-proprietary.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Fahd Shariff Guest
|
Posted: Wed May 19, 2004 9:34 am Post subject: Re: Suggested Newbie project, FontShower |
|
|
Thanks Roedy,
Using a JSpinner was a great idea for changing font sizes. I also love
what you've done with the changeStyle() method. I knew that there had
to be a more elegant way to do it!
PS: Yes Andrew, my newsreader seems to have a problem and I'm
currently deleting all the duplicates. Sorry for any inconvenience
caused.
--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Wed May 19, 2004 5:39 pm Post subject: Re: Suggested Newbie project, FontShower |
|
|
On 19 May 2004 02:34:39 -0700, [email]fahdshariff (AT) yahoo (DOT) com[/email] (Fahd Shariff)
wrote or quoted :
| Quote: | Using a JSpinner was a great idea for changing font sizes. I also love
what you've done with the changeStyle() method. I knew that there had
to be a more elegant way to do it!
|
It is amazing how much work a trivial little applet like that is.
I'm wondering if perhaps such code could be stomped out with an Applet
cookie cutter. You customise it to generate code the way you like,
and most of the time you just remove unnecessary code, or modify it
later, but the framework of all the usual method calls is there.
Part of the problem if you generate code for a button you need to put
code in several separated places, declare, instantiate, add/layout,
and listeners.
Java has no ability to put such code in separate files.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Wed May 19, 2004 10:21 pm Post subject: Re: Suggested Newbie project, FontShower |
|
|
On 19 May 2004 02:34:39 -0700, Fahd Shariff wrote:
| Quote: | PS: Yes Andrew, my newsreader seems to have a problem and I'm
currently deleting all the duplicates.
|
Cool. Have you considered changing
news readers? Is that an option?
| Quote: | ..Sorry for any inconvenience
caused.
|
s'O.K.
[ nice code BTW ..better post good code
twice, than 186 lines of abuse/silly puns ]
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|