-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
b28
-
generic
-
generic
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2160482 | 6u10 | Philip Race | P3 | Resolved | Fixed | b22 |
The test below from ###@###.### shows that text on a translucent
image renders poorly.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* @author Chris Campbell
*/
public class TextTest2 extends JPanel {
private boolean useImage = false;
public TextTest2() {
setBackground(new Color(0.3f, 0.3f, 0.3f));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
useImage = !useImage;
repaint();
}
});
}
private void drawText(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.setFont(new Font("sansserif", Font.PLAIN, 70));
g.drawString("Hello!", 20, 200);
g.setFont(new Font("sansserif", Font.PLAIN, 12));
g.drawString("Hello!", 20, 230);
g.setFont(new Font("sansserif", Font.PLAIN, 10));
g.drawString("Hello!", 20, 250);
}
@Override
public void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(getBackground());
g.fillRect(0, 0, w, h);
if (useImage) {
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = img.createGraphics();
drawText(g2);
g.drawImage(img, 0, 0, null);
g2.dispose();
} else {
drawText((Graphics2D)g);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(280, 380);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
TextTest2 test = new TextTest2();
frame.add(test);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
image renders poorly.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* @author Chris Campbell
*/
public class TextTest2 extends JPanel {
private boolean useImage = false;
public TextTest2() {
setBackground(new Color(0.3f, 0.3f, 0.3f));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
useImage = !useImage;
repaint();
}
});
}
private void drawText(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.setFont(new Font("sansserif", Font.PLAIN, 70));
g.drawString("Hello!", 20, 200);
g.setFont(new Font("sansserif", Font.PLAIN, 12));
g.drawString("Hello!", 20, 230);
g.setFont(new Font("sansserif", Font.PLAIN, 10));
g.drawString("Hello!", 20, 250);
}
@Override
public void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(getBackground());
g.fillRect(0, 0, w, h);
if (useImage) {
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = img.createGraphics();
drawText(g2);
g.drawImage(img, 0, 0, null);
g2.dispose();
} else {
drawText((Graphics2D)g);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(280, 380);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
TextTest2 test = new TextTest2();
frame.add(test);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
- backported by
-
JDK-2160482 Poor text rendering on translucent image.
-
- Resolved
-