开发者

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.s开发者_JAVA技巧etFont(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:

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

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

thanks for help :)


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.


The problem you are facing is called Aliasing. Try turning on Anti-Aliasing, something along the lines of

Graphics2D.setRenderingHints(Graphics2D.ANTIALIASING,Graphics2D.ANTIALIAS_ON)


As previous answers explained it, the problem is anti-aliasing.

Aliasing occurs when a signal (in this case, a 2D graphics signal) is sampled and quantized from a continuous space into a discretized space. Sampling is the process of reading a value from a continuously varying signal. Quantization is the process by which these continuous sampled values are assigned a discrete value in the finite space represented by digital (binary-based) systems.

Aliasing is a by-product of this quantization. Humans perceive this by-product visually as abrupt changes in color from pixel to pixel. Graphics professionals often refer to these jagged edges as jaggies.

Java 2D lets you set one of several rendering hints to indicate that you would like for your 2D graphics to be drawn using antialiasing algorithms -- which smoothes the edges.

There is an example chapter for this at javaworld.com

It's explained here:

/**
* Example05 illustrates some basics of Java 2D.
* This version is compliant with Java 1.2 Beta 3, Jul 1998.
* Please refer to: <BR>
* http://www.javaworld.com/javaworld/jw-08-1998/jw-08-media.html
* <P>
* @author Bill Day <bill.day@javaworld.com>
* @version 1.0
* @see java.awt.Font
* @see java.awt.Graphics2D
* @see java.awt.geom.AffineTransform
* @see java.awt.geom.GeneralPath
**/

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Example05 extends Frame {
/**
* Instantiates an Example05 object.
**/
public static void main(String args[]) {
 new Example05();
}

 /**
 * Our Example05 constructor sets the frame's size, adds the
 * visual components, and then makes them visible to the user.
 * It uses an adapter class to deal with the user closing
 * the frame.
 **/
 public Example05() {
 //Title our frame.
 super("Java 2D Example05");

 //Set the size for this frame.
 setSize(330,270); 

 //We need to turn on the visibility of our frame
 //by setting the Visible parameter to true.
 setVisible(true);

 //Now, we want to be sure we properly dispose of resources 
 //this frame is using when the window is closed.  We use 
 //an anonymous inner class adapter for this.
 addWindowListener(new WindowAdapter() 
  {public void windowClosing(WindowEvent e) 
     {dispose(); System.exit(0);}  
   }
 );
}

/**
* The paint method is where the real magic is.  In previous
* examples, we saw some jagged edges due to aliasing.
* Example05 illustrates how to use rendering hints to request
* an anti-aliased render from Graphics2D.
**/
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;

//This time, we want to use anti-aliasing if possible
//to avoid the jagged edges that were so prominent in
//our last example.  With ask the Java 2D rendering
//engine (Graphics2D) to do this using a "rendering hint".
g2d.setRenderingHints(Graphics2D.ANTIALIASING,
   Graphics2D.ANTIALIAS_ON);

//We reuse our GeneralPath filled shape.  We translate
//and rotate this shape as we did before.
GeneralPath path = new GeneralPath(GeneralPath.EVEN_ODD);
path.moveTo(0.0f,0.0f);
path.lineTo(0.0f,125.0f);
path.quadTo(100.0f,100.0f,225.0f,125.0f);
path.curveTo(260.0f,100.0f,130.0f,50.0f,225.0f,0.0f);
path.closePath();

AffineTransform at = new AffineTransform();
at.setToRotation(-Math.PI/8.0);
g2d.transform(at);
at.setToTranslation(0.0f,150.0f);
g2d.transform(at);

g2d.setColor(Color.green);
g2d.fill(path);

//Now, let's use some of the Java font and text support.
//Note that you need to be sure you have the same fonts I
//use in the example (Times New Roman True Type) if you
//execute this example code.
Font exFont = new Font("TimesRoman",Font.PLAIN,40);

//Un-comment the following diagnostic println's if you
//want to see what font was returned.  This can be useful
//when you have limited font supports on your system and
//are not sure which font the Java runtime may have
//substituted for your requested font.
//System.out.println(exFont.getFamily());
//System.out.println(exFont.isPlain());
//System.out.println(exFont.getSize());

g2d.setFont(exFont);
g2d.setColor(Color.black);
g2d.drawString("JavaWorld",0.0f,0.0f);
}
}

These are taken from: javaworld.com


Also

g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

might help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜