我使用以下代码在java中打印 ABC :
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");
我得到结果图片:
我怎样才能在java中获得这种“羽毛效果”? (或任何其他java库)
感谢您的帮助:)
答案 0 :(得分:6)
您应该能够将Graphics转换为Graphics2D,并使用以下代码行:
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
有关详细信息,请参阅http://download.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html。
答案 1 :(得分:3)
您遇到的问题称为别名。尝试打开抗锯齿,这是
的内容Graphics2D.setRenderingHints(Graphics2D.ANTIALIASING,Graphics2D.ANTIALIAS_ON)
答案 2 :(得分:1)
正如之前的答案所解释的那样,问题在于抗锯齿。
当信号(在这种情况下,2D图形信号)被采样并从连续空间量化到离散空间时发生混叠。采样是从连续变化的信号中读取值的过程。量化是在数字(基于二进制)系统表示的有限空间中为这些连续采样值分配离散值的过程。
别名是这种量化的副产品。人类在视觉上将这种副产品视为从像素到像素的颜色突然变化。图形专业人员经常将这些锯齿状边缘称为锯齿状。
Java 2D允许您设置几个渲染提示之一,以指示您希望使用抗锯齿算法绘制2D图形 - 这可以平滑边缘。
在javaworld.com上有一个示例章节
这里解释了:
/**
* 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);
}
}
这些摘自:javaworld.com
答案 3 :(得分:0)
另外
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
可能有帮助