modified MyCanvas.java

* Changed initial font-size to 40pt instead of 100pt - more speed
* trim() text before display
This commit is contained in:
mbirth 2004-12-08 10:17:57 +00:00
parent cea6530d2d
commit e4ee7d4fd9

View File

@ -9,7 +9,7 @@ class MyCanvas extends Canvas {
Color bgColor = new Color(0,0,128); Color bgColor = new Color(0,0,128);
Color fgColor = new Color(255,255,0); Color fgColor = new Color(255,255,0);
String word = ""; String word = "";
int maxFontSize = 100; int maxFontSize = 40;
public void paint(Graphics g) { public void paint(Graphics g) {
g.setColor(bgColor); g.setColor(bgColor);
@ -25,15 +25,16 @@ class MyCanvas extends Canvas {
do { do {
f = new Font("Dialog", Font.BOLD, fs); f = new Font("Dialog", Font.BOLD, fs);
fm = g.getFontMetrics(f); fm = g.getFontMetrics(f);
fs -= 2; // System.out.println("MyC: Font-size now "+fs+"pt.");
} while ((fm.stringWidth(word)>getSize().width-2) || (fm.getHeight()>getSize().height-2)); fs -= 4;
} while (((fm.stringWidth(word)>getSize().width-2) || (fm.getHeight()>getSize().height-2)) && fs > 0);
g.setFont(f); g.setFont(f);
g.drawString(word, getSize().width/2-fm.stringWidth(word)/2, getSize().height/2-fm.getHeight()/2+fm.getAscent()); g.drawString(word, getSize().width/2-fm.stringWidth(word)/2, getSize().height/2-fm.getHeight()/2+fm.getAscent());
} }
} }
public void setWord(String txt) { public void setWord(String txt) {
word = txt; word = txt.trim();
repaint(); repaint();
} }
} }