Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Custom java.awt.TextArea  enhancement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom java.awt.TextArea  enhancement" Watch "Custom java.awt.TextArea  enhancement" New topic
Author

Custom java.awt.TextArea enhancement

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hello Friends,
I require a custom TextArea component which can display text aswell as images. After a tiresome hunt I got the below code which accomplishes the above said goal, an excellent awt custom component. But this component got one drawback. Though it allows to change Font, it displays entite textarea content only in the font that is specified at last, which is a great drawback and I believe that it requires miner modifications that allows it display text in various fonts ( as it does for various colors i.e text in this component can have various colors but drawback is only one font is used for entire text ). As Iam not very well versed in custom awt programming, can any one please tune the below code so that it displays text in the desired font. If you got any alternative components which accomplishes the above goal, please intimate me as it is very urgent for me.
----------
import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;
import java.net.URL;
import java.util.*;
class k extends Canvas
{
public void setForeground(Color color)
{
super.setForeground(color);
t = color;
}
public void a(Image image)
{
x = image;
}
public synchronized Dimension getMinimumSize()
{
return size();
}
public int b()
{
return r;
}
public Dimension getPreferredSize()
{
return getMinimumSize();
}
public int c()
{
return m;
}
public void a(int i1, int j1)
{
m = j1 / r - 1;
a = i1;
s = j1;
c = new Dimension(a, s);
}
public void update(Graphics g1)
{
Object obj = null;
MediaTracker mediatracker = new MediaTracker(this);
int j1 = 0;
if(d())
{
f = createImage(size().width, size().height);
g = f.getGraphics();
g.setFont(getFont());
} else
{
g.setColor(y);
g.fillRect(0, 0, size().width, size().height);
}
if(x != null)
{
int k1 = x.getWidth(this);
int i2 = x.getHeight(this);
g.drawImage(x, (a - k1) / 2, (s - i2) / 2, this);
}
String s1 = "";
String s3 = "";
boolean flag = false;
Image image = null;
for(int k2 = d; k2 < u.size(); k2++)
{
if((k2 - d) * r >= s)
break;
String s2 = (String)u.elementAt(k2);
j1 += r;
StringTokenizer stringtokenizer = new StringTokenizer(s2, "\251", true);
int j2 = 5;
while(stringtokenizer.hasMoreTokens())
{
String s4 = stringtokenizer.nextToken();
if(v.elementAt(k2) != null)
g.setColor((Color)v.elementAt(k2));
else
g.setColor((Color)w.elementAt(k2));
if(s4.indexOf("\251") == -1)
{
g.drawString(s4, j2, j1);
j2 += k.stringWidth(s4);
} else
{
String s5 = stringtokenizer.nextToken();
try
{
URL url = new URL(b, s5.substring(0, s5.indexOf(' ')));
image = z.getImage(url);
mediatracker.addImage(image, 0);
mediatracker.waitForID(0, 5000L);
}
catch(Exception _ex) { }
int l1 = image.getWidth(z);
int i1 = j1 - image.getHeight(z);
g.drawImage(image, j2, i1, z);
j2 += l1;
g.drawString(s5.substring(s5.indexOf(' ')), j2, j1);
j2 += k.stringWidth(s5.substring(s5.indexOf(' ')));
}
}
}
g1.drawImage(f, 0, 0, this);
}
protected boolean d()
{
Dimension dimension = size();
return f == null || dimension.width != f.getWidth(this) || dimension.height != f.getHeight(this);
}
public int a(String s1, Color color)
{
char c1 = '\u012C';
int i1 = 0;
String as[] = new String[c1];
int j1 = 0;
Object obj = null;
MediaTracker mediatracker = new MediaTracker(this);
String s2 = "";
String s3 = "";
s1 = s1.trim() + " ";
for(int j2 = s1.indexOf("*", 1); j2 > 0; j2 = s1.indexOf("*", j2 + 2))
if(!s1.substring(j2 - 1, j2).equals(" ") && j2 < s1.length() - 1)
{
String s5 = s1.substring(j2 + 1, j2 + 2);
if(s5.compareTo("9") <= 0 && s5.compareTo("0") >= 0)
s1 = s1.substring(0, j2) + " " + s1.substring(j2);
}
int k2 = -1;
byte byte0 = -1;
k2 = s1.indexOf("_.");
if(k2 >= 0)
{
int l2 = s1.indexOf(" ", k2);
s2 = s1.substring(k2 + 2, l2);
String s4 = s1.substring(0, k2);
s4 = s4 + s1.substring(l2 + 1);
s1 = s4;
if(l2 < 0)
{
int i3 = s1.length();
s2 = s1.substring(k2 + 2, i3);
}
}
try
{
q = Integer.parseInt(s2, 10);
}
catch(Exception _ex)
{
q = -1;
}
if(q >= j.length)
q = -1;
for(int j3 = 0; j3 < c1; j3++)
as[j3] = "";
StringTokenizer stringtokenizer = new StringTokenizer(s1, " \t\n\r", true);
int k3 = 0;
int l3 = size().width - 10;
boolean flag = false;
boolean flag1 = false;
String s7 = "";
while(stringtokenizer.hasMoreElements())
{
String s8 = stringtokenizer.nextToken();
boolean flag2 = false;
if(s8.equals("\n"))
{
s8 = "";
flag2 = true;
}
if(s8.equals("\t"))
s8 = " ";
if(p.containsKey(s8.toUpperCase()))
{
try
{
String s9 = (String)p.get(s8.toUpperCase());
String s6;
if(s8.charAt(0) == '*')
s6 = "";
else
s6 = s8.toUpperCase();
URL url = new URL(b, "../icon/" + s9);
Image image = z.getImage(url);
mediatracker.addImage(image, 0);
try
{
mediatracker.waitForID(0, 5000L);
}
catch(InterruptedException _ex) { }
int k1 = image.getWidth(z);
if(k1 > 0)
{
int i2 = k.stringWidth(s6);
if(j1 + k1 + i2 >= l3 || flag2)
{
as[++k3] = " \251" + "../icon/" + s9 + " " + s6;
j1 = 5 + k1 + k.stringWidth(" " + s6);
} else
{
as[k3] += "\251" + "../icon/" + s9 + " " + s6;
j1 += k1 + k.stringWidth(" " + s6);
}
i1 = Math.max(image.getHeight(z), i1);
}
}
catch(Exception exception)
{
System.err.println("Problem with the image " + exception);
}
} else
{
int l1 = k.stringWidth(s8);
if(j1 + l1 >= l3 || flag2)
{
as[++k3] = " " + s8;
j1 = 5 + l1;
} else
{
as[k3] += s8;
j1 += l1;
}
}
}
boolean flag3 = false;
for(int l4 = 0; l4 <= k3; l4++)
{
if(as[l4].indexOf("\251") != -1)
{
int i4 = i1 % r;
int j4;
if(i4 == 0)
j4 = 0;
else
j4 = r - i4;
int k4 = (i1 + j4) / r;
for(int i5 = 1; i5 < k4; i5++)
{
u.insertElementAt(" ", i);
if(q > 0)
{
v.insertElementAt(j[q], i);
w.insertElementAt(null, i);
} else
{
v.insertElementAt(null, i);
w.insertElementAt(color, i);
}
if(i >= 200)
{
u.removeElementAt(0);
w.removeElementAt(0);
v.removeElementAt(0);
} else
{
i++;
}
}
}
u.insertElementAt(as[l4], i);
if(q > 0)
{
v.insertElementAt(j[q], i);
w.insertElementAt(null, i);
} else
{
v.insertElementAt(null, i);
w.insertElementAt(color, i);
}
if(i >= 200)
{
u.removeElementAt(0);
w.removeElementAt(0);
v.removeElementAt(0);
} else
{
i++;
}
}
repaint();
if(i - m < 0)
d = 0;
else
d = i - m;
return d;
}
public void setFont(Font font)
{
super.setFont(font);
k = getFontMetrics(font);
r = k.getHeight();
}
public Dimension size()
{
if(super.size().width <= 0)
{
return c;
} else
{
a(super.size().width, super.size().height);
return super.size();
}
}
public int a()
{
return 200;
}
public int a(int i1)
{
int j1 = 0;
if(i - m < 0)
j1 = 0;
else
j1 = i - m;
if(i1 >= j1)
d = j1;
else
d = i1;
repaint();
return d;
}
public void paint(Graphics g1)
{
update(g1);
}
public void a(Applet applet)
{
try
{
z = applet;
b = z.getCodeBase();
return;
}
catch(Exception exception)
{
System.err.println(exception.getMessage());
exception.printStackTrace();
return;
}
}
public k(int i1, int j1, Font font)
{
j = (new Color[] {
new Color(0,0,0), Color.blue, Color.black, Color.gray, Color.green, Color.orange, Color.red, Color.white, Color.yellow
});
i = 0;
q = -1;
y = Color.white;
t = Color.black;
x = null;
d = 0;
setFont(font);
setBackground(y);
a(i1, j1);
u = new Vector(200);
w = new Vector(200);
v = new Vector(200);
p = new Hashtable();
int k1 = 0;
do
p.put("*" + k1, k1 + "icon.gif");
while(++k1 < 41);
}
public void setBackground(Color color)
{
super.setBackground(color);
y = color;
}
private final String e = "../icon/";
private URL b;
private Color t;
private Color y;
private Graphics g;
public Dimension c;
private Dimension h;
private Image x;
private Image f;
private FontMetrics k;
private int q;
private int s;
private int a;
private int r;
private int m;
private int d;
private int i;
private final int n = 5;
private final int o = 5;
private final int l = 200;
private Hashtable p;
private Vector v;
private Vector w;
private Vector u;
private Applet z;
final Color j[];
final int ab = 200;
}
----------
Regards,
Ch.Praveen.
 
jQuery in Action, 2nd edition
 
subject: Custom java.awt.TextArea enhancement