Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happened to anti-aliasing in Java 7?

I have a snippet of code that I use to take text and generate an icon with a shadow effect to assign to various components (shown below). This worked and looked great up until Java 7, in which something changed in the anti-aliasing logic and now it looks pretty crappy, whereas in Java 6, the same code looks great.

The last image actually looks OK, so it seems to only happen with certain color combinations...

At any rate, does anyone know what has changed with AA in Java 7 or how to change it back or make this work like it did in Java 6? Or who I complain to for breaking the 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);
            }
        });     
    }
}
like image 819
PAULUS Avatar asked Jul 15 '13 17:07

PAULUS


1 Answers

Anti aliasing was indeed changed in Java7. link I have not found anyway to revert it at this time as it appears as though the actual AA code was changed in Java7. So you can blame Oracle for breaking AA. Only solution I have come across is reverting to Java6 (which is impossible on the latest verions of OSX). Wish I could be more helpful, but the only solution I could think of would be using different AA code from an open source library. I hope this helps a little.

like image 102
Skylion Avatar answered Oct 17 '22 15:10

Skylion