我有一段代码用于获取文本并生成带有阴影效果的图标以分配给各种组件(如下所示)。这在 Java 7 之前一直有效并且看起来很棒,其中抗锯齿逻辑发生了一些变化,现在看起来很糟糕,而在 Java 6 中,相同的代码看起来很棒。
最后一张图片实际上看起来不错,所以它似乎只发生在某些颜色组合中......
无论如何,有谁知道 Java 7 中的 AA 发生了什么变化,或者如何将其改回或使其像 Java 6 中那样工作?或者我向谁抱怨打破了AA?哈哈...
提前感谢您的帮助!
package mainpackage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.AttributedString;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class AntiAliasProblems
{
private static final long serialVersionUID = 1L;
private static Font font = new Font("Arial", Font.BOLD, 12);
public static ImageIcon makeShadowIcon1()
{
AttributedString as = new AttributedString("Delete");
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, 1);
as.addAttribute(TextAttribute.FONT, font);
BufferedImage image = new BufferedImage(75, 23, BufferedImage.TRANSLUCENT);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
TextLayout textLayout = new TextLayout(as.getIterator(), g2.getFontRenderContext());
g2.setPaint(java.awt.Color.white);
textLayout.draw(g2, 19, 17);
g2.setPaint(new java.awt.Color(0x00, 0x3D, 0x76));
textLayout.draw(g2, 19, 16);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
try
{
ImageIO.write(image, "png", baos);
baos.flush();
baos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
g2.dispose();
return new ImageIcon(baos.toByteArray());
}
public static ImageIcon makeShadowIcon2()
{
AttributedString as = new AttributedString("Delete");
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, 1);
as.addAttribute(TextAttribute.FONT, font);
BufferedImage image = new BufferedImage(75, 23, BufferedImage.TRANSLUCENT);
Graphics2D g2 = image.createGraphics();
TextLayout textLayout = new TextLayout(as.getIterator(), g2.getFontRenderContext());
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2.setPaint(java.awt.Color.white);
textLayout.draw(g2, 19, 17);
g2.setPaint(new java.awt.Color(0x00, 0x3D, 0x76));
textLayout.draw(g2, 19, 16);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
try
{
ImageIO.write(image, "png", baos);
baos.flush();
baos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
g2.dispose();
return new ImageIcon(baos.toByteArray());
}
public static ImageIcon makeShadowIcon3()
{
AttributedString as = new AttributedString("Delete");
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, 1);
as.addAttribute(TextAttribute.FONT, font);
BufferedImage image = new BufferedImage(75, 23, BufferedImage.TRANSLUCENT);
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
TextLayout textLayout = new TextLayout(as.getIterator(), g2.getFontRenderContext());
g2.setPaint(java.awt.Color.black);
textLayout.draw(g2, 19, 15);
g2.setPaint(java.awt.Color.white);
textLayout.draw(g2, 19, 16);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
try
{
ImageIO.write(image, "png", baos);
baos.flush();
baos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
g2.dispose();
return new ImageIcon(baos.toByteArray());
}
public static void main(String s[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Anti-Alias Issue");
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 33));
JButton button1 = new JButton(makeShadowIcon1());
button1.setPreferredSize(new Dimension(75, 23));
button1.setBackground(Color.PINK);
JButton button2 = new JButton(makeShadowIcon2());
button2.setPreferredSize(new Dimension(75, 23));
button2.setBackground(Color.PINK);
JButton button3 = new JButton(makeShadowIcon3());
button3.setPreferredSize(new Dimension(75, 23));
button3.setBackground(Color.PINK);
panel.add(button1);
panel.add(button2);
panel.add(button3);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
}
}