[ Pobierz całość w formacie PDF ]
18. How would you change the method paint in Display 18.18 so that the happy face not
only has blue eyes and a red mouth, but also has brown skin?
18.5
Fonts and the drawString Method
It is not of so much consequence what you say,
as how you say it.
Alexander Smith, Dreamthorp. On the Writing of Essays
Java has facilities to add text to drawings and to modify the font of the text. We will show
you enough to allow you to do most things you might want to do with text and fonts.
THE drawString METHOD
Display 18.21 contains a demonstration program for the method drawString. When
the program is run, the GUI displays the text "Push the button.". When the user
5640_ch18.fm Page 946 Friday, February 13, 2004 4:52 PM
946 Chapter 18 Swing II
Display 18.21 Using drawString (Part 1 of 3)
1 import javax.swing.JFrame;
2 import javax.swing.JPanel;
3 import javax.swing.JButton;
4 import java.awt.event.ActionListener;
5 import java.awt.event.ActionEvent;
6 import java.awt.Container;
7 import java.awt.BorderLayout;
8 import java.awt.Graphics;
9 import java.awt.Color;
10 import java.awt.Font;
11 public class DrawStringDemo extends JFrame
12 implements ActionListener
13 {
14 public static final int WIDTH = 350;
15 public static final int HEIGHT = 200;
16 public static final int X_START = 20;
17 public static final int Y_START = 100;
18 public static final int POINT_SIZE = 24;
19 private String theText = "Push the button.";
20 private Color penColor = Color.BLACK;
21 private Font fontObject =
22 new Font("SansSerif", Font.PLAIN, POINT_SIZE);
23 public static void main(String[] args)
24 {
25 DrawStringDemo gui = new DrawStringDemo();
26 gui.setVisible(true);
27 }
28 public DrawStringDemo()
29 {
30 setSize(WIDTH, HEIGHT);
31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32 setTitle("drawString Demonstration");
33 Container contentPane = getContentPane();
34 contentPane.setBackground(Color.WHITE);
35 contentPane.setLayout(new BorderLayout());
36 JPanel buttonPanel = new JPanel();
37 buttonPanel.setBackground(Color.ORANGE);
38 buttonPanel.setLayout(new BorderLayout());
5640_ch18.fm Page 947 Friday, February 13, 2004 4:52 PM
Fonts and the drawString Method 947
Display 18.21 Using drawString (Part 2 of 3)
39 JButton theButton = new JButton("The Button");
40 theButton.addActionListener(this);
41 buttonPanel.add(theButton, BorderLayout.CENTER);
42 contentPane.add(buttonPanel, BorderLayout.SOUTH);
43 }
44 public void paint(Graphics g)
45 {
46 super.paint(g);
47 g.setFont(fontObject);
48 g.setColor(penColor);
49 g.drawString(theText, X_START, Y_START);
50 }
51 public void actionPerformed(ActionEvent e)
52 {
53 penColor = Color.RED;
54 fontObject =
55 new Font("Serif", Font.BOLD|Font.ITALIC, POINT_SIZE);
56 theText = "Thank you. I needed that.";
57 repaint();
58 }
59 }
RESULTING GUI (Start view)
5640_ch18.fm Page 948 Friday, February 13, 2004 4:52 PM
948 Chapter 18 Swing II
Display 18.21 Using drawString (Part 3 of 3)
RESULTING GUI (After clicking the button)
clicks the button, the string is changed to "Thank you. I needed that.". The text is
written with the method drawString.
The method drawString is similar to the drawing methods in the class Graphics,
but it displays text rather than a drawing. For example, the following line from Display
18.21 writes the string stored in the variable theText starting at the x- and y-coordi-
nates X_START and Y_START:
g.drawString(theText, X_START, Y_START);
The string is written in the current font. A default font is used if no font is specified.
The details about fonts are discussed in the next subsection.
FONTS
The program in Display 18.21 illustrates how the font for the method drawString is
set. That program sets the font with the following line in the definition of the method
paint:
setFont g.setFont(fontObject);
In that program fontObject is a private instance variable of type Font. Font is a class in
the java.awt package. Objects of the class Font represent fonts.
In Display 18.21 the variable fontObject is set using a constructor for the class
Font. The initial font is set as part of the instance variable declaration in the following
lines taken from Display 18.21:
private Font fontObject =
new Font("SansSerif", Font.PLAIN, POINT_SIZE);
Font
The constructor for the class Font creates a font in a given style and size. The first argu-
ment, in this case "SansSerif", is a string that gives the name of the font (that is, the
5640_ch18.fm Page 949 Friday, February 13, 2004 4:52 PM
Fonts and the drawString Method 949
basic style). Some typical font names are "Times", "Courier", and "Helvetica". You
may use any font currently available on your system. Java guarantees that you will have
at least the three fonts "Monospaced", "SansSerif", and "Serif". To see what these
[ Pobierz całość w formacie PDF ]