2 *******************************************************************************
3 * Copyright (C) 1997-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.demo.impl;
10 import java.awt.FontMetrics;
11 import java.awt.Graphics;
12 import java.text.BreakIterator;
14 public class DemoTextBox {
16 public DemoTextBox(Graphics g, String text, int width)
19 this.chars = new char[text.length()];
20 text.getChars(0, text.length(), chars, 0);
24 this.metrics = g.getFontMetrics();
29 public int getHeight() {
30 return (nbreaks + 1) * metrics.getHeight();
33 public void draw(Graphics g, int x, int y)
37 y += metrics.getAscent();
39 for (int i = 0; i < nbreaks; i++)
41 g.drawChars(chars, index, breakPos[i] - index, x, y);
43 y += metrics.getHeight();
46 g.drawChars(chars, index, chars.length - index, x, y);
50 private void breakText()
52 if (metrics.charsWidth(chars, 0, chars.length) > width)
54 BreakIterator iter = BreakIterator.getWordInstance();
57 int start = iter.first();
61 while ( (pos = iter.next()) != BreakIterator.DONE )
63 int w = metrics.charsWidth(chars, start, pos - start);
66 // We've gone past the maximum width, so break the line
68 // There was at least one break position before this point
69 breakPos[nbreaks++] = end;
73 // There weren't any break positions before this one, so
74 // let this word overflow the margin (yuck)
75 breakPos[nbreaks++] = pos;
79 // the current position still fits on the line; it's the best
80 // tentative break position we have so far.
90 // private Graphics port;
91 private FontMetrics metrics;
94 private int[] breakPos = new int[10]; // TODO: get real
95 private int nbreaks = 0;