我有一段代码,用于获取文本并生成带阴影效果的图标以分配给各种组件(如下所示)。这在Java 7中起作用并且看起来很棒,其中反锯齿逻辑中的某些内容发生了变化,现在看起来很糟糕,而在Java 6中,相同的代码看起来很棒。
最后一张图片实际上看起来还不错,所以似乎只会出现某些颜色组合......
无论如何,是否有人知道Java 7中AA的变化,或者如何更改它或使其像Java 6中那样工作?或者我为了打破AA而抱怨谁?哈哈...
提前感谢您的帮助!
AA http://i43.tinypic.com/v5v5vr.jpg
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);
}
});
}
}
答案 0 :(得分:6)
在Java7中确实改变了抗锯齿。 link此时我还没有找到恢复它,因为看起来好像在Java7中更改了实际的AA代码。所以你可以责怪甲骨文打破AA。我遇到的唯一解决方案是恢复到Java6(这在OSX的最新版本中是不可能的)。希望我能提供更多帮助,但我能想到的唯一解决方案是使用开源库中的不同AA代码。我希望这有点帮助。