public class PlottingArea {
private int width, height;
private char[][] canvas = null;
private final char EMPTY_CHAR = '#';
public PlottingArea(int width, int height) {
this.width = width;
this.height = height;
canvas = new char[height][width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
setChar(x, y, EMPTY_CHAR);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public char[][] plotString(int xStart, int yStart, String s) {
// First line
int firstLineLength = width - xStart; // get empty space in first line
String firstLine = firstLineLength < s.length() ? s.substring(0, firstLineLength) : s;
plotHorizontalLine(xStart, yStart, firstLine); // plot it
int lineStartIndex = firstLineLength; // get the new start index
// Other lines
for (int i = yStart + 1; i < height; i++) { // height +1
if (lineStartIndex >= s.length())
return canvas; // String has already fully been plotted
int lineEndIndex = s.length() - lineStartIndex < width ? lineStartIndex + (s.length() - lineStartIndex) : lineStartIndex + width;
String line = s.substring(lineStartIndex, lineEndIndex); // get the new line
plotHorizontalLine(0, i, line); // plot it
lineStartIndex += width; // set the new start index
}
// Plotting finished
return canvas;
}
private void plotHorizontalLine(int x, int y, String s) {
int currentChar = 0;
for (int i = x; i < width; i++) {
if (currentChar < s.length())
setChar(i, y, s.charAt(currentChar++));
}
}
/**
* Inserts the character at his appropriate coordinates
*
* @param x x coordinate of the character
* @param y y coordinate of the character
* @param c the character to be inserted
*/
private void setChar(int x, int y, char c) {
canvas[y][x] = c;
}
}
private int width, height;
private char[][] canvas = null;
private final char EMPTY_CHAR = '#';
public PlottingArea(int width, int height) {
this.width = width;
this.height = height;
canvas = new char[height][width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
setChar(x, y, EMPTY_CHAR);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public char[][] plotString(int xStart, int yStart, String s) {
// First line
int firstLineLength = width - xStart; // get empty space in first line
String firstLine = firstLineLength < s.length() ? s.substring(0, firstLineLength) : s;
plotHorizontalLine(xStart, yStart, firstLine); // plot it
int lineStartIndex = firstLineLength; // get the new start index
// Other lines
for (int i = yStart + 1; i < height; i++) { // height +1
if (lineStartIndex >= s.length())
return canvas; // String has already fully been plotted
int lineEndIndex = s.length() - lineStartIndex < width ? lineStartIndex + (s.length() - lineStartIndex) : lineStartIndex + width;
String line = s.substring(lineStartIndex, lineEndIndex); // get the new line
plotHorizontalLine(0, i, line); // plot it
lineStartIndex += width; // set the new start index
}
// Plotting finished
return canvas;
}
private void plotHorizontalLine(int x, int y, String s) {
int currentChar = 0;
for (int i = x; i < width; i++) {
if (currentChar < s.length())
setChar(i, y, s.charAt(currentChar++));
}
}
/**
* Inserts the character at his appropriate coordinates
*
* @param x x coordinate of the character
* @param y y coordinate of the character
* @param c the character to be inserted
*/
private void setChar(int x, int y, char c) {
canvas[y][x] = c;
}
}
das wollte ich schreiben bis ich gemerkt hat des mein System es nicht Akzeptiert