-
Notifications
You must be signed in to change notification settings - Fork 0
/
FontApplet.java
75 lines (68 loc) · 2.52 KB
/
FontApplet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This applet allows users to select font name, size, and style using control boxes.
import java.applet.Applet;
import java.awt.Choice;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class FontApplet extends Applet implements ItemListener {
// Declare Choice components for font name, size, and style
Choice fontName, fontSize, fontStyle;
// Default font properties
String name = "Serif";
int size = 12;
int style = Font.PLAIN;
// Initialize the applet
public void init() {
// Initialize font name choices
fontName = new Choice();
fontName.add("Serif");
fontName.add("SansSerif");
fontName.add("Monospaced");
fontName.addItemListener(this); // Add item listener to handle selection changes
// Initialize font size choices
fontSize = new Choice();
fontSize.add("12");
fontSize.add("14");
fontSize.add("16");
fontSize.add("18");
fontSize.addItemListener(this); // Add item listener to handle selection changes
// Initialize font style choices
fontStyle = new Choice();
fontStyle.add("Plain");
fontStyle.add("Bold");
fontStyle.add("Italic");
fontStyle.addItemListener(this); // Add item listener to handle selection changes
// Add the choice components to the applet
add(fontName);
add(fontSize);
add(fontStyle);
}
// Handle item state changes
public void itemStateChanged(ItemEvent e) {
// Update font name based on user selection
name = fontName.getSelectedItem();
// Update font size based on user selection
size = Integer.parseInt(fontSize.getSelectedItem());
// Update font style based on user selection
String styleStr = fontStyle.getSelectedItem();
if (styleStr.equals("Plain")) {
style = Font.PLAIN;
} else if (styleStr.equals("Bold")) {
style = Font.BOLD;
} else if (styleStr.equals("Italic")) {
style = Font.ITALIC;
}
// Request a repaint to update the display
repaint();
}
// Paint the applet
public void paint(Graphics g) {
// Create a new font with the selected properties
Font font = new Font(name, style, size);
// Set the font for the graphics context
g.setFont(font);
// Draw the sample text with the selected font
g.drawString("Sample Text", 50, 100);
}
}