|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Finding Out Which Fonts Are AvailableIf youre using Windows or a similar operating system environment, you can have many fonts installed on your system. Unfortunately, Java applets cant access your system fonts. Java applications, however, can. For Java applets, depending on your version of the Java Virtual Machine (JVM), the fonts that are available to the JVM might differ slightly from those of another user. To help you identify the fonts you have available, you can use the getFontList() method.
The source code for the complete FontQuery1 applet is shown in Listing 10.5. The applet is shown running in Figure 10.7. Listing 10.5 The FontQuery1 Applet 1 // FontQuery1.java 2 3 import java.awt.*; 4 import java.applet.*; 5 6 public class FontQuery1 extends Applet 7 { 8 public void init() 9 { 10 } 11 12 TextArea txArea = new TextArea(); 13 14 public void populateFontList() 15 { 16 Toolkit toolkit = Toolkit.getDefaultToolkit(); 17 String strFontList[] = toolkit.getFontList(); 18 for( int i=0; i<strFontList.length; i++ ) 19 txArea.append( strFontList[i] + "\n" ); 20 } 21 22}
Fonts in Visual J++ ApplicationsIf youre writing an application in Visual J++, selecting and setting fonts couldnt be easier. All you have to do to select the font for a label, or any other control for that matter, is pull up the Font Selector Common dialog box. Lets start by creating a new Visual J++ Windows application named ChangeFonts. Double-click the Form1.java file in the Project Explorer window so that the form window is displayed. Place four labels on the form as shown in Figure 10.8.
In the Properties window, find the font property as shown in Figure 10.9.
If you click on the button thats to the right of the font property field, a font selector box will appear. Itll allow you to select the font type, style, and size as shown in Figure 10.10. Go ahead now and change the font type for all four labels. You can see my program in Figure 10.11all four labels have a different font.
A Marquee AppletAnd now for the question everyone who has a home page is asking: How do I create one of those marquee applets that continuously scrolls a text message? When programmers who arent familiar with Java find out I know Java, thats one of the first questions they ask. Well, this section is dedicated to those individuals who are dying to put such an applet on their home page. Figure 10.12 shows a simple marquee applet thats running. (Its hard to capture the motion in a book!) The source code for the applet is given in Listing 10.6.
Listing 10.6 Source Code for a Simple Marquee Applet 1 // Applet1.java 2 3 import java.awt.*; 4 import java.applet.*; 5 6 /** 7 * This class reads PARAM tags from its HTML host page and sets 8 * the color and label properties of the applet. Program execution 9 * begins with the init() method. 10 */ 11 public class Applet1 extends Applet implements Runnable 12 { 13 private Thread m_Applet1 = null; 14 int x = 740; 15 int y = 45; 16 String m_strMessage = "Visual J++ is really cool. AND I MEAN ⇒COOL!!!"; 17 18 /** 19 * The entry point for the applet. 20 */ 21 public void init() 22 { 23 initForm(); 24 } 25 26 /** 27 * Initializes values for the applet and its components 28 */ 29 void initForm() 30 { 31 this.setBackground(Color.lightGray); 32 this.setForeground(Color.black); 33 } 34 35 public void destroy() 36 { 37 } 38 39 public void paint(Graphics g) 40 { 41 FontMetrics fm = g.getFontMetrics(); 42 g.drawString( m_strMessage, x, y ); 43 x -= 5; 44 if( x < -fm.stringWidth( m_strMessage ) ) 45 x = 740; 46 } 47 48 public void start() 49 { 50 if (m_Applet1 == null) 51 { 52 m_Applet1 = new Thread(this); 53 m_Applet1.start(); 54 } 55 } 56 57 public void stop() 58 { 59 if (m_Applet1 != null) 60 { 61 m_Applet1.stop(); 62 m_Applet1 = null; 63 } 64 } 65 66 public void run() 67 { 68 while (true) 69 { 70 try 71 { 72 repaint(); 73 Thread.sleep(50); 74 } 75 catch (InterruptedException e) 76 { 77 stop(); 78 } 79 } 80 } 81 82} Setting the Font of a ControlAt times you will want to change the font of a control in your program. Controls use a default font that can change from machine to machine. If you want complete control over what your controls look like, youll want to explicitly set their font instead of relying on the default font, which might not be what youre expecting. Setting the font of a control is easy: simply create a new font and then use the controls setFont() method. The following example creates a Label control, creates a new font, calls the labels setFont() method, and then adds the label to the container: Label label1 = new Label( "This is a new label" ); Font font = new Font( "TimesRoman", Font.PLAIN, 25 ); label1.setFont( font ); add( label1 );
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |