source: lost-perception/utils/WrappedString.java@ ebd3538

Last change on this file since ebd3538 was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

  • Property mode set to 100644
File size: 1.6 KB
Line 
1package utils;
2
3import java.awt.Graphics;
4import java.awt.FontMetrics;
5import java.util.ArrayList;
6
7public class WrappedString
8{
9 private ArrayList<String> lstStrings;
10 private FontMetrics metrics;
11
12 public WrappedString(String str, final FontMetrics metrics, final int width) {
13 this.lstStrings = new ArrayList<String>();
14 this.metrics = metrics;
15 while (!str.equals("")) {
16 int cur = 1;
17 int lastSpace = -1;
18 while (cur <= str.length() && metrics.stringWidth(str.substring(0, cur)) < width) {
19 if (str.substring(cur - 1, cur).equals(" ")) {
20 lastSpace = cur - 1;
21 }
22 ++cur;
23 }
24 if (lastSpace == -1 || cur > str.length()) {
25 this.lstStrings.add(str.substring(0, cur - 1));
26 str = str.substring(cur - 1);
27 }
28 else {
29 this.lstStrings.add(str.substring(0, lastSpace));
30 str = str.substring(lastSpace + 1);
31 }
32 while (str.length() > 0 && str.substring(0, 1).equals(" ")) {
33 str = str.substring(1);
34 }
35 }
36 }
37
38 public int getLineCount() {
39 return this.lstStrings.size();
40 }
41
42 public String getLine(final int index) {
43 return this.lstStrings.get(index);
44 }
45
46 public FontMetrics getMetrics() {
47 return this.metrics;
48 }
49
50 public void draw(final Graphics g, final int x, final int y) {
51 for (int i = 0; i < this.getLineCount(); ++i) {
52 g.drawString(this.getLine(i), x, y + i * this.metrics.getHeight());
53 }
54 }
55}
Note: See TracBrowser for help on using the repository browser.