Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 :)

like image 854
Koerr Avatar asked May 22 '26 00:05

Koerr


1 Answers

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.

like image 154
JB Nizet Avatar answered May 24 '26 13:05

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!