What happened to anti-aliasing in Java 7?

I have a piece of code that I use to take text and create an icon with a shadow effect to assign to various components (shown below). It worked and looked great right up to Java 7, in which something has changed in the smoothing logic, and now it looks pretty crappy, while in Java 6 the same code looks great.

The last image actually looks normal, so it seems that this only happens with certain color combinations ...

Anyway, does anyone know what has changed from AA in Java 7 or how to get it back or do it the way it was in Java 6? Or who am I complaining about hacking AA? Haha ...

Thanks in advance for any help!

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); } }); } } 
+6
source share
1 answer

Anti-aliasing has really been changed in Java7. I did not find a link in any case to return it at this time, as it seems that the actual AA code was changed in Java7. Therefore, you can blame Oracle for violating AA. The only solution I came across was a return to Java6 (which is not possible on recent versions of OSX). I would like to be more useful, but the only solution I could think of would be to use another AA code from the open source library. Hope this helps a bit.

+6
source

Source: https://habr.com/ru/post/949483/


All Articles