giáo trình Java By Example phần 5
Số trang: 66
Loại file: pdf
Dung lượng: 166.44 KB
Lượt xem: 20
Lượt tải: 0
Xem trước 7 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu giáo trình java by example phần 5, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 5 Table 17.2 Commonly Used FontMetrics Methods. Method Description Returns the width of a character. charWidth() Returns the fonts ascent. getAscent() Returns the fonts descent. getDescent() Returns the associated Font object. getFont() Returns the fonts height. getHeight() Returns the fonts leading (line spacing). getLeading() Returns the width of a string. stringWidth() Returns a string of information about the font. toString() NOTE If you havent used fonts before, some of the terms-leading, ascent, and descent-used in Table 17.2 may be unfamiliar to you. Leading (pronounced ledding) is the amount of white space between lines of text. Ascent is the height of a character, from the baseline to the top of the character. Descent is the size of the area that accommodates the descending portions of letters, such as the tail on a lowercase g. Height is the sum of ascent, descent, and leading. See Figure 17.3 for examples of each.Figure 17.3 : Ascent, descent, and leading determine the overall height of a font.Example: Displaying Font MetricsMost of the methods listed in Table 17.2 are self-explanatory. However, you probably want a chance tosee them in action. Listing 17.3 is the source code for the MetricsApplet, and Listing 17.4 is the appletsHTML document. When you run the MetricsApplet applet, you see the window shown in Figure 17.4. Atthe top of the window is a text box into which you can enter different strings of text. When you pressEnter, the applet displays the length of the string in pixels. Immediately below the text box is informationabout the current font.Figure 17.4 : This is Appletviewer running the MetricsApplet applet. Listing 17.3 MetricsApplet.java: An Applet That Displays Text Metrics. import java.awt.*; import java.applet.*; http://www.ngohaianh.infopublic class MetricsApplet extends Applet{ TextField textField; public void init() { textField = new TextField(20); add(textField); textField.setText(Default string); } public void paint(Graphics g) { Font font = getFont(); FontMetrics fontMetrics = g.getFontMetrics(font); int n = fontMetrics.getLeading(); String leading = String.valueOf(n); n = fontMetrics.getAscent(); String ascent = String.valueOf(n); n = fontMetrics.getDescent(); String descent = String.valueOf(n); n = fontMetrics.getHeight(); String height = String.valueOf(n); http://www.ngohaianh.info String s = textField.getText(); n = fontMetrics.stringWidth(s); String width = String.valueOf(n); g.drawString(FONT INFO:, 55, 60); g.drawString(Leading: + leading, 70, 80); g.drawString(Ascent: + ascent, 70, 95); g.drawString(Descent: + descent, 70, 110); g.drawString(Height: + height, 70, 125); g.drawString(STRING INFO:, 55, 155); g.drawString(Width: + width, 70, 175); } public boolean action(Event event, Object arg) { repaint(); return true; }}Listing 17.4 METRICSAPPLET.htmL: MetricsApplets HTML Document. http://www.ngohaianh.info Applet Test Page Applet Test Page NOTE Because all of the applets youve written so far in this book havent used text metrics when displaying text, you may wonder why you even need to bother with this stuff. Chances are that when youre running your applets under Windows 95 using the default font, everything will work fine. But remember that your applets may run on machines using other operating systems, and their default fonts may not be exactly the same size. Also, when you create your own fonts, you may not know the resultant fonts size exactly. In order to position text accurately, you need to use font metrics, as youll see later in this chapter.Creating FontsYou may thi ...
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 5 Table 17.2 Commonly Used FontMetrics Methods. Method Description Returns the width of a character. charWidth() Returns the fonts ascent. getAscent() Returns the fonts descent. getDescent() Returns the associated Font object. getFont() Returns the fonts height. getHeight() Returns the fonts leading (line spacing). getLeading() Returns the width of a string. stringWidth() Returns a string of information about the font. toString() NOTE If you havent used fonts before, some of the terms-leading, ascent, and descent-used in Table 17.2 may be unfamiliar to you. Leading (pronounced ledding) is the amount of white space between lines of text. Ascent is the height of a character, from the baseline to the top of the character. Descent is the size of the area that accommodates the descending portions of letters, such as the tail on a lowercase g. Height is the sum of ascent, descent, and leading. See Figure 17.3 for examples of each.Figure 17.3 : Ascent, descent, and leading determine the overall height of a font.Example: Displaying Font MetricsMost of the methods listed in Table 17.2 are self-explanatory. However, you probably want a chance tosee them in action. Listing 17.3 is the source code for the MetricsApplet, and Listing 17.4 is the appletsHTML document. When you run the MetricsApplet applet, you see the window shown in Figure 17.4. Atthe top of the window is a text box into which you can enter different strings of text. When you pressEnter, the applet displays the length of the string in pixels. Immediately below the text box is informationabout the current font.Figure 17.4 : This is Appletviewer running the MetricsApplet applet. Listing 17.3 MetricsApplet.java: An Applet That Displays Text Metrics. import java.awt.*; import java.applet.*; http://www.ngohaianh.infopublic class MetricsApplet extends Applet{ TextField textField; public void init() { textField = new TextField(20); add(textField); textField.setText(Default string); } public void paint(Graphics g) { Font font = getFont(); FontMetrics fontMetrics = g.getFontMetrics(font); int n = fontMetrics.getLeading(); String leading = String.valueOf(n); n = fontMetrics.getAscent(); String ascent = String.valueOf(n); n = fontMetrics.getDescent(); String descent = String.valueOf(n); n = fontMetrics.getHeight(); String height = String.valueOf(n); http://www.ngohaianh.info String s = textField.getText(); n = fontMetrics.stringWidth(s); String width = String.valueOf(n); g.drawString(FONT INFO:, 55, 60); g.drawString(Leading: + leading, 70, 80); g.drawString(Ascent: + ascent, 70, 95); g.drawString(Descent: + descent, 70, 110); g.drawString(Height: + height, 70, 125); g.drawString(STRING INFO:, 55, 155); g.drawString(Width: + width, 70, 175); } public boolean action(Event event, Object arg) { repaint(); return true; }}Listing 17.4 METRICSAPPLET.htmL: MetricsApplets HTML Document. http://www.ngohaianh.info Applet Test Page Applet Test Page NOTE Because all of the applets youve written so far in this book havent used text metrics when displaying text, you may wonder why you even need to bother with this stuff. Chances are that when youre running your applets under Windows 95 using the default font, everything will work fine. But remember that your applets may run on machines using other operating systems, and their default fonts may not be exactly the same size. Also, when you create your own fonts, you may not know the resultant fonts size exactly. In order to position text accurately, you need to use font metrics, as youll see later in this chapter.Creating FontsYou may thi ...
Tìm kiếm theo từ khóa liên quan:
lập trình Java tin học ứng dụng lập trình windows lập trình C# mẹo hay cho tin học thủ thuật windowsGợi ý tài liệu liên quan:
-
Tài liệu bồi dưỡng giáo viên sử dụng SGK Tin học 10 Cánh diều (Định hướng Tin học ứng dụng)
61 trang 238 0 0 -
101 trang 199 1 0
-
20 trang 183 0 0
-
Cách gỡ bỏ hoàn toàn các add on trên Firefox
7 trang 180 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 177 0 0 -
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 164 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 157 0 0 -
Giáo trình Tin học ứng dụng: Phần 1 - Trường ĐH Tài nguyên và Môi trường Hà Nội
125 trang 151 0 0 -
Bài giảng Tin học ứng dụng: Kiểm định trung bình - Trường ĐH Y dược Huế
25 trang 144 0 0 -
Giáo trình Quản trị mạng (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
173 trang 123 1 0