-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollText.java
More file actions
156 lines (123 loc) · 3.65 KB
/
Copy pathScrollText.java
File metadata and controls
156 lines (123 loc) · 3.65 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package threadbook.ch09;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
public class ScrollText extends JComponent {
private BufferedImage image;
private Dimension imageSize;
private volatile int currOffset;
private Thread internalThread;
private volatile boolean noStopRequested;
public ScrollText(String text) {
currOffset = 0;
buildImage(text);
setMinimumSize(imageSize);
setPreferredSize(imageSize);
setMaximumSize(imageSize);
setSize(imageSize);
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
try {
runWork();
} catch ( Exception x ) {
x.printStackTrace();
}
}
};
internalThread = new Thread(r, "ScrollText");
internalThread.start();
}
private void buildImage(String text) {
// Request that the drawing be done with anti-aliasing
// turned on and the quality high.
RenderingHints renderHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
// Create a scratch image for use in determining
// the text dimensions.
BufferedImage scratchImage = new BufferedImage(
1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D scratchG2 = scratchImage.createGraphics();
scratchG2.setRenderingHints(renderHints);
Font font =
new Font("Serif", Font.BOLD | Font.ITALIC, 24);
FontRenderContext frc = scratchG2.getFontRenderContext();
TextLayout tl = new TextLayout(text, font, frc);
Rectangle2D textBounds = tl.getBounds();
int textWidth = (int) Math.ceil(textBounds.getWidth());
int textHeight = (int) Math.ceil(textBounds.getHeight());
int horizontalPad = 10;
int verticalPad = 6;
imageSize = new Dimension(
textWidth + horizontalPad,
textHeight + verticalPad
);
// Create the properly-sized image
image = new BufferedImage(
imageSize.width,
imageSize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHints(renderHints);
int baselineOffset =
( verticalPad / 2 ) - ( (int) textBounds.getY());
g2.setColor(Color.white);
g2.fillRect(0, 0, imageSize.width, imageSize.height);
g2.setColor(Color.blue);
tl.draw(g2, 0, baselineOffset);
// Free-up resources right away, but keep "image" for
// animation.
scratchG2.dispose();
scratchImage.flush();
g2.dispose();
}
public void paint(Graphics g) {
// Make sure to clip the edges, regardless of curr size
g.setClip(0, 0, imageSize.width, imageSize.height);
int localOffset = currOffset; // in case it changes
g.drawImage(image, -localOffset, 0, this);
g.drawImage(
image, imageSize.width - localOffset, 0, this);
// draw outline
g.setColor(Color.black);
g.drawRect(
0, 0, imageSize.width - 1, imageSize.height - 1);
}
private void runWork() {
while ( noStopRequested ) {
try {
Thread.sleep(100); // 10 frames per second
// adjust the scroll position
currOffset =
( currOffset + 1 ) % imageSize.width;
// signal the event thread to call paint()
repaint();
} catch ( InterruptedException x ) {
Thread.currentThread().interrupt();
}
}
}
public void stopRequest() {
noStopRequested = false;
internalThread.interrupt();
}
public boolean isAlive() {
return internalThread.isAlive();
}
public static void main(String[] args) {
ScrollText st =
new ScrollText("Java can do animation!");
JPanel p = new JPanel(new FlowLayout());
p.add(st);
JFrame f = new JFrame("ScrollText Demo");
f.setContentPane(p);
f.setSize(400, 100);
f.setVisible(true);
}
}