summaryrefslogtreecommitdiff
path: root/NET/worlds/console/TextCanvas.java
blob: 2175b193ae6e34adcbb5f741656c9d09e97e3afa (plain) (blame)
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
/*     */ package NET.worlds.console;
/*     */ 
/*     */ import java.awt.Canvas;
/*     */ import java.awt.Color;
/*     */ import java.awt.Dimension;
/*     */ import java.awt.Font;
/*     */ import java.awt.FontMetrics;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.Toolkit;
/*     */ import java.util.Vector;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TextCanvas
/*     */   extends Canvas
/*     */ {
/*     */   private static final long serialVersionUID = 2808978760046610505L;
/*     */   private Dimension calcSize;
/*     */   private Font font;
/*     */   private FontMetrics metrics;
/*  31 */   private Vector<String> lines = new Vector();
/*     */   
/*     */   public TextCanvas(String text, int width) {
/*  34 */     this.font = new Font(Console.message("CanvasFont"), 0, 12);
/*     */     
/*  36 */     this.metrics = Toolkit.getDefaultToolkit().getFontMetrics(this.font);
/*  37 */     char[] chars = text.toCharArray();
/*     */     
/*     */ 
/*  40 */     int start = 0;
/*  41 */     int end = 0;
/*  42 */     int prevEnd = 0;
/*  43 */     int lineWidth = 0;
/*  44 */     for (end = start; end < chars.length;) {
/*  45 */       char c = chars[end];
/*  46 */       boolean isEOL = c == '\n';
/*  47 */       if ((!isEOL) && (end == chars.length - 1)) {
/*  48 */         isEOL = true;
/*  49 */         end++;
/*     */       }
/*     */       
/*     */ 
/*  53 */       if ((c == ' ') || (isEOL)) {
/*  54 */         lineWidth = this.metrics.charsWidth(chars, start, end - start);
/*  55 */         if ((width != -1) && (lineWidth > width)) {
/*  56 */           end = prevEnd;
/*  57 */         } else if (((width == -1) || (lineWidth < width)) && (!isEOL)) {
/*  58 */           prevEnd = end;
/*  59 */           end++;
/*  60 */           continue;
/*     */         }
/*     */         
/*     */ 
/*  64 */         this.lines.addElement(new String(chars, start, end - start));
/*     */         
/*     */ 
/*  67 */         start = end + 1;
/*  68 */         while ((start < chars.length) && (chars[start] == ' '))
/*  69 */           start++;
/*  70 */         prevEnd = end = start;
/*     */       } else {
/*  72 */         end++;
/*     */       }
/*     */     }
/*     */     
/*  76 */     this.calcSize = new Dimension(width == -1 ? lineWidth : width, 
/*  77 */       this.metrics.getHeight() * this.lines.size());
/*     */   }
/*     */   
/*     */ 
/*     */   public void paint(Graphics g)
/*     */   {
/*  83 */     super.paint(g);
/*  84 */     g.setFont(this.font);
/*  85 */     g.setColor(Color.black);
/*  86 */     int height = this.metrics.getHeight();
/*     */     
/*     */ 
/*  89 */     int y = this.metrics.getAscent() + this.metrics.getLeading();
/*     */     
/*     */ 
/*  92 */     for (int i = 0; i < this.lines.size(); i++) {
/*  93 */       String line = (String)this.lines.elementAt(i);
/*  94 */       g.drawString(line, 0, y);
/*  95 */       y += height;
/*     */     }
/*     */   }
/*     */   
/*     */   public Dimension getPreferredSize()
/*     */   {
/* 101 */     return this.calcSize;
/*     */   }
/*     */ }


/* Location:              C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\console\TextCanvas.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */