-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnowmanCanvas.java
99 lines (87 loc) · 2.36 KB
/
SnowmanCanvas.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* CS 106A Snowman
*
* DO NOT MODIFY THIS FILE!
* DO NOT MODIFY THIS FILE!
* DO NOT MODIFY THIS FILE!
*
* This instructor-provided file provides a simple graphical display of the
* game state as a secondary console.
*
* Author : Colin Kincaid
* Version: 2018/07/11
*
* Your program should work properly with an UNMODIFIED version of this file.
* If you want to modify this file for testing or for fun, that is your choice.
* Some students implement a fancier graphical display as an extra feature.
*
* This file and its contents are copyright (C) Stanford University and Marty Stepp,
* licensed under Creative Commons Attribution 2.5 License. All rights reserved.
*/
import acm.graphics.*;
public class SnowmanCanvas extends GCanvas {
private static final String FONT = "Monospaced-Bold-13";
private static final int LINE_HEIGHT = 14;
private int y; // current y-coordinate at which to draw line of text
private SnowmanProgram program; // program to which the canvas is attached
/**
* Sets up initial state of canvas.
*/
public SnowmanCanvas() {
y = LINE_HEIGHT;
}
/**
* Removes any drawn text from the canvas.
*/
public void clear() {
removeAll();
y = LINE_HEIGHT;
}
/**
* Causes all output sent to this console to instead go to the main console
* of the given Snowman program.
* @param snowmanProgram the snowman program whose console should be used
*/
public void merge(SnowmanProgram snowmanProgram) {
this.program = snowmanProgram;
}
/**
* Prints the given text onto the console.
* @param text the text to print
*/
public void print(String text) {
println(text);
}
/**
* Prints the given text onto the console.
* @param text the text to print
*/
public void println(String text) {
if (program != null) {
program.println(text); // just forward to main console
} else if (text.contains("\n")) {
// special case: print a multi-line string
String[] lines = text.split("\r?\n");
for (String line : lines) {
println(line);
}
} else {
GLabel label = new GLabel(text, 0, y);
label.setFont(FONT);
add(label);
y += LINE_HEIGHT;
}
}
/**
* Ends merging of the console so that this canvas will once again
* display output on itself.
*/
public void unmerge() {
program = null;
}
}
/*
* DO NOT MODIFY THIS FILE!
* DO NOT MODIFY THIS FILE!
* DO NOT MODIFY THIS FILE!
*/