Search code examples
javajava-2d

how can I get "feather effect" using java 2D?


I using below codes to print ABC in java:

String NAME="ABC";
int FONT_SIZE=100;

BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();

g.setColor(new Color(255,255,255));
g.fillRect(0, 0, image.getWidth(), image.getHeight());

g.setColor(new Color(0,0,0));
g.setFont(new Font("arial", Font.PLAIN ,FONT_SIZE));
g.drawString(NAME,FONT_SIZE,FONT_SIZE);

g.dispose();

//write to file
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", out);
byte[] byteArray = out.toByteArray();
bytesToFile(byteArray,"D:/temp/pic/text/text.jpg");

I get result image: enter image description here

how can I get this "feather effect" in java? (or any others java library)

thanks for help :)


Solution

  • You should be able to cast your Graphics to Graphics2D, and use the following line of code :

    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
    

    See http://download.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html for more information.