Hi all, I'm building an app with a zoomable interface and I'm getting quite poor
results when rendering text(AttributedStrings, actually) when the font size gets small.
I know this topic was briefly discussed during the Java2D BOF at this years JavaOne,
and the problem was put down to a bug that probably wouldn't get fixed until the
release of 1.5. Any news on this issue? Is it a platform-specific problem(I'm running
1.4.2 on win2k)?
Tips for workarounds to increase rendering quality would be greatly appreciated!
Here's a code snippet that shows the problem. Run the applet, change fontsize by
right-clicking and watch a significant change in quality between fontsize 11 and 10.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.font.*;
import java.text.*;
import javax.swing.*;
public class FontSizeTest extends Applet {
private boolean isStandalone = false;
int fontSize = 12;
AttributedString attributedString;
TextLayout textLayout;
String theString = "How come font size < 11 looks crappy?";
//Construct the applet
public FontSizeTest() {
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger()){
JPopupMenu pM = new JPopupMenu();
JMenu fontSizeMenu = new JMenu("Set font size");
for(int i = 5; i < 16; i++){
final int fs = i;
fontSizeMenu.add(new AbstractAction("Font size " + fs){
public void actionPerformed(ActionEvent e){
fontSize = fs;
attributedString = null;
textLayout = null;
repaint();
}
});
}
pM.add(fontSizeMenu);
pM.show(FontSizeTest.this,e.getX(), e.getY());
}
}
});
}
public void paint(Graphics g){
Dimension d = getSize();
int w = d.width;
int h = d.height;
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setColor(Color.black);
g2.fillRect(0, 0, w, h);
drawString(g2);
}
private void drawString(Graphics2D gr){
if(attributedString == null || textLayout == null){
attributedString = new AttributedString(theString);
attributedString.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.white);
attributedString.addAttribute(TextAttribute.BACKGROUND, Color.black);
attributedString.addAttribute(TextAttribute.FONT, new Font("Arial", Font.BOLD,
fontSize));
textLayout = new TextLayout(attributedString.getIterator(),
gr.getFontRenderContext());
}
textLayout.draw(gr, (int)(getWidth()/2 - textLayout.getBounds().getWidth()/2),
getHeight()/2);
}
//Main method
public static void main(String[] args) {
FontSizeTest applet = new FontSizeTest();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Font size test");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height -
frame.getSize().height) / 2);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Thanks,
Olof
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".