-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplayer.java
More file actions
60 lines (51 loc) · 1.72 KB
/
Displayer.java
File metadata and controls
60 lines (51 loc) · 1.72 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
/**
Display a string at the top of the window.
To make successive displays overlap, set the window height
to the height of your shell window.
To make successive displays scroll, specify a window height of 0.
*/
public class Displayer {
private int windowHeight;
private java.io.Console console;
/**
Construct an instance that will display mazes in a window
with height @windowHeight.
*/
public Displayer( int windowHeight) {
this.windowHeight = windowHeight;
console = System.console(); /* see docs on Console for
conditions under which console is non-null */
}
/**
Display @pic at the top of the window.
*/
public void atTopOfWindow( String pic) {
System.out.print( pic);
// fill rest of screen
for( int linesPrinted = lineSeparatorsIn( pic)
; linesPrinted < windowHeight -1 // leave 1 for cursor
; linesPrinted++
)
System.out.print( System.lineSeparator());
// wait until user has assimilated the display
if( console != null)
console.readLine( "<enter> to continue: ");
}
/**
@return the number of lineSeparators in @string
It would be pathetic for Java to lack a built-in
substring-counter. But I've wasted too long looking for it.
*/
private static int lineSeparatorsIn( String string) {
int count = 0;
int fromIndex = 0;
while( ( fromIndex = string.indexOf( System.lineSeparator()
, fromIndex)
) != -1
) {
count++;
fromIndex += System.lineSeparator().length();
}
return count;
}
}