-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomStringsPanel.java
More file actions
120 lines (95 loc) · 4.02 KB
/
RandomStringsPanel.java
File metadata and controls
120 lines (95 loc) · 4.02 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
/**
* This panel displays 25 copies of a message. The color and
* position of each message is selected at random. The font
* of each message is randomly chosen from among five possible
* fonts. The messages are displayed on a black background.
* Note: The style of drawing used here is poor, because every
* time the paintComponent() method is called, new random values are
* used. This means that a different picture will be drawn each time.
*/
public class RandomStringsPanel extends JPanel {
private String message; // The message to be displayed. This can be set in
// the constructor. If no value is provided in the
// constructor, then the string "Java!" is used.
private Font font1, font2, font3, font4, font5; // The five fonts.
/**
* Default constructor creates a panel that displays the message "Java!".
*/
public RandomStringsPanel() {
this(null); // Call the other constructor, with parameter null.
}
/**
* Constructor creates a panel to display 25 copies of a specified message.
* @param messageString The message to be displayed. If this is null,
* then the default message "Java!" is displayed.
*/
public RandomStringsPanel(String messageString) {
message = messageString;
if (message == null)
message = "Java!";
font1 = new Font("Serif", Font.BOLD, 14);
font2 = new Font("SansSerif", Font.BOLD + Font.ITALIC, 24);
font3 = new Font("Monospaced", Font.PLAIN, 30);
font4 = new Font("Dialog", Font.PLAIN, 36);
font5 = new Font("Serif", Font.ITALIC, 48);
setBackground(Color.BLACK);
}
/**
* The paintComponent method is responsible for drawing the content of the panel.
* It draws 25 copies of the message string, using a random color, font, and
* position for each string.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g); // Call the paintComponent method from the
// superclass, JPanel. This simply fills the
// entire panel with the background color, black.
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
int width = getWidth();
int height = getHeight();
for (int i = 0; i < 25; i++) {
// Draw one string. First, set the font to be one of the five
// available fonts, at random.
int fontNum = (int)(5*Math.random()) + 1;
switch (fontNum) {
case 1:
g.setFont(font1);
break;
case 2:
g.setFont(font2);
break;
case 3:
g.setFont(font3);
break;
case 4:
g.setFont(font4);
break;
case 5:
g.setFont(font5);
break;
} // end switch
// Set the color to a bright, saturated color, with random hue.
float hue = (float)Math.random();
g.setColor( Color.getHSBColor(hue, 1.0F, 1.0F) );
// Select the position of the string, at random.
int x,y;
x = -50 + (int)(Math.random()*(width+40));
y = (int)(Math.random()*(height+20));
// Draw the message.
g.drawString(message,x,y);
} // end for
} // end paintComponent()
public static void main(String[] args) {
JFrame window = new JFrame("Java!");
RandomStringsPanel content = new RandomStringsPanel();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.setSize(350,250);
window.setVisible(true);
}
} // end class RandomStringsPanel