-
Enhancement
-
Resolution: Fixed
-
P4
-
1.2.1, 1.2.2, 1.3.0
-
beta
-
x86
-
windows_98, windows_nt
Name: krT82822 Date: 04/28/99
On Windows 95/98 I have found that if I draw text in a Swing component it renders at a different length (usually longer) if text anti-aliasing is enabled via RenderingHints.
The following code packs 2 JLabels in a JFrame, one of which has anti-aliasing enabled. For most fonts the anti-aliased text is longer than the non-aliased text. Apparently FontMetrics is not aware of this discrepency, since both labels appear with the same dimensions, after then JFrame is pack()ed.
This is the only thing keeping us from moving to the Java 2 platform, so please give it your full attention.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private JLabel label;
private AliasLabel aliaslabel;
private Font font;
private JMenuBar bar;
private JMenu fontSizeMenu, fontTypeMenu;
private String[] fontNames;
private int fontSize = 20;
private String fontName = "sansserif";
public Main() {
super("Text alias test");
fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
bar = new JMenuBar();
setJMenuBar(bar);
fontSizeMenu = new JMenu("Font size");
bar.add(fontSizeMenu);
for (int i = 8; i <= 36; i+=2) {
JMenuItem temp = new JMenuItem(String.valueOf(i));
final int size = i;
temp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
changeFont(size);
}
});
fontSizeMenu.add(temp);
}
fontTypeMenu = new JMenu("Font names");
bar.add(fontTypeMenu);
for (int i = 0; i < fontNames.length; ++i) {
JMenuItem temp = new JMenuItem(fontNames[i]);
final String name = fontNames[i];
temp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
changeFont(name);
}
});
fontTypeMenu.add(temp);
}
getContentPane().setLayout(new BorderLayout());
label = new JLabel("welcome to intertainer");
getContentPane().add(label, BorderLayout.NORTH);
aliaslabel = new AliasLabel("welcome to intertainer");
getContentPane().add(aliaslabel, BorderLayout.SOUTH);
changeFont(26);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
setVisible(true);
}
public void changeFont(int size) {
fontSize = size;
font = new Font(fontName, Font.PLAIN, fontSize);
label.setFont(font);
aliaslabel.setFont(font);
pack();
}
public void changeFont(String name) {
fontName = name;
font = new Font(fontName, Font.PLAIN, fontSize);
label.setFont(font);
aliaslabel.setFont(font);
pack();
}
public static void main(String args[]) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
public class AliasLabel extends JLabel {
public AliasLabel(String text) {
super(text);
}
public void paintComponent(Graphics g) {
Graphics2D gg = (Graphics2D)g;
gg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
super.paintComponent(gg);
}
}
Output of java -version:
java version "1.2.1"
Classic VM (build JDK-1.2.1-A, native threads)
Output of java -fullversion:
java full version "JDK-1.2.1-A"
(Review ID: 57615)
======================================================================
Name: krT82822 Date: 06/13/99
When drawing a string of anti aliased text (using drawString on a Graphics object that's been "hinted" to antialias), character spacing is inconsistent and incorrect. Also, the total width of the anti-aliased text consistently exceeds the amount reported by FontMetrics.getStringWidth(). When anti-aliasing is turned off (comment out the hint code), the string is drawn correctly and fits within its box. I've observed this problem in both applets and applications on both Win98 and Win95.
With SansSerif (Arial) at 18pt, the inconsistent character spacing is particularly noticeable (note the position of the 'l' in Biology is very much not centered between the two 'o's).
---------------------------
// AntiTextApplet.java
import java.applet.Applet;
import java.awt.*;
public class AntiTextApplet extends Applet
{
public void init() {
setBackground(Color.white);
}
int fontSize = 18;
Font font = new Font("SansSerif", Font.PLAIN, fontSize);
String text = "Economics and biology";
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints qualityHints =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(qualityHints);
g2.setFont(font);
g2.drawString(text, 0, fontSize);
int w = Toolkit.getDefaultToolkit().getFontMetrics(font).stringWidth(text);
g2.drawRect(0,0,w,fontSize);
}
}
--------------------------------
(Review ID: 83924)
======================================================================
Name: krT82822 Date: 09/20/99
9/20/99 eval1127@eng -- dupe of 4248641. Root problem being addressed under bug # 4233886.
When anti-aliasing is on, bold text is only stretched, not truly of heavier weight. Style effects added to fonts of anti-aliased text should appear at least similar to non-anti-aliased text with the same styles applied.
The following program demonstrates this:
import java.awt.*;
import java.awt.font.*;
import java.util.*;
import javax.swing.*;
public class TestFrame extends JFrame {
//Construct the frame
public TestFrame() {
this.setSize(new Dimension(800, 600));
this.setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2d=(Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Map m;
Font f;
m=new Hashtable();
m.put(TextAttribute.FAMILY,"Arial");
m.put(TextAttribute.SIZE,new Float(48));
m.put(TextAttribute.WEIGHT,TextAttribute.WEIGHT_BOLD);
f=new Font(m);
g2d.setFont(f);
g2d.drawString("This is a test..",10,200);
Map m1=new Hashtable();
m1.put(TextAttribute.FAMILY,"Arial");
m1.put(TextAttribute.SIZE,new Float(48.00001));
// **** THIS IS NECESSARY BECAUSE OF BUG 4209476
m1.put(TextAttribute.WEIGHT,TextAttribute.WEIGHT_REGULAR);
Font f1=new Font(m1);
g2d.setFont(f1);
g2d.drawString("This is a test..",10,300);
}
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.validate();
frame.setVisible(true);
}
}
(Review ID: 95475)
======================================================================
- duplicates
-
JDK-4352501 Text length in anti-aliased swing components is wrong
-
- Closed
-
- relates to
-
JDK-4248641 FontMetrics.stringWidth() incorrect for antialiased bold text
-
- Closed
-