1 package code
.kalter
.longflight
;
3 import java
.io
.IOException
;
4 import javax
.microedition
.lcdui
.Graphics
;
5 import javax
.microedition
.lcdui
.Image
;
8 * Костыль - и этим всё сказанно
14 private static Font instance
;
16 public static Font
getInstance() throws IOException
{
17 if (instance
== null) {
18 instance
= new Font();
23 private final Image
[] ascii
;
25 private Font() throws IOException
{
26 Image font
= Loader
.getInstance().getImage("/gfx/font.png");
27 ascii
= new Image
[0x7F];
29 for (int i
= 0x41; i
<= 0x5A; i
++, x
+= 5) {
30 ascii
[i
] = Image
.createImage(font
, x
, 0, 5, 9, 0);
32 for (int i
= 0x61; i
<= 0x7A; i
++, x
+= 5) {
33 ascii
[i
] = Image
.createImage(font
, x
, 0, 5, 9, 0);
35 for (int i
= 0x30; i
<= 0x39; i
++, x
+= 5) {
36 ascii
[i
] = Image
.createImage(font
, x
, 0, 5, 9, 0);
38 ascii
[0x21] = Image
.createImage(font
, x
, 0, 5, 9, 0);
39 ascii
[0x22] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
40 ascii
[0x27] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
41 ascii
[0x2C] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
42 ascii
[0x2D] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
43 ascii
[0x2E] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
44 ascii
[0x3A] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
45 ascii
[0x28] = Image
.createImage(font
, x
+= 5, 0, 5, 9, 0);
46 ascii
[0x29] = Image
.createImage(font
, x
+= 5, 0, 4, 9, 0);
49 public int getWidth(String text
) {
50 return text
.length() * 6;
53 public void paint(Graphics graphics
, String text
, int x
, int y
) {
54 for (int i
= 0, px
= x
; i
< text
.length(); i
++) {
55 final char c
= text
.charAt(i
);
70 graphics
.drawImage(ascii
[c
], x
, y
, 0);
77 public void paint(Graphics graphics
, String text
,
78 int x
, int y
, int width
) {
79 String
[] word
= split(text
);
81 for (int i
= 0; i
< word
.length
; i
++) {
82 paint(graphics
, word
[i
], px
, y
);
83 int length
= (getWidth(word
[i
])) + 6;
84 if ((px
+= length
) + length
> width
) {
91 public int getHeight(String text
, int width
) {
92 String
[] word
= split(text
);
94 for (int i
= 0, x
= 0; i
< word
.length
; i
++) {
95 int length
= (getWidth(word
[i
])) + 6;
96 if ((x
+= length
) + length
> width
) {
104 private String
[] split(String text
) {
107 for (int i
= 0; i
< text
.length(); i
++) {
108 if (text
.charAt(i
) == ' ') {
112 result
= new String
[length
];
113 StringBuffer word
= new StringBuffer();
115 for (int i
= 0; i
< text
.length(); i
++) {
116 char c
= text
.charAt(i
);
118 result
[j
++] = word
.toString();
119 word
= new StringBuffer();
124 result
[j
] = word
.toString();