【发布时间】:2015-03-20 02:21:53
【问题描述】:
这是我在这里的第一个主题,我为我的英语不好(我是意大利人)向你道歉。
我使用 javax.swing 库编写了一个示例 2d 应用程序。
我使用BufferedImage 渲染形状、图像和字体。
图像在 Canvas (java.awt) 中呈现。
图片尺寸为 100x100,Canvas 尺寸为 700x500。
现在的问题是当我将字符串绘制到图像中时,因为字符串是像素化的。我尝试激活抗锯齿,但它们看起来很模糊。
有没有办法解决这个问题?
这是我的代码:
public class Application extends Canvas implements Runnable {
public static final int WIDTH=700, HEIGHT=500;
private BufferedImage image;
private Thread thread;
public Application() {
image=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
setSize(WIDTH, HEIGHT);
}
public void addNotify() {
super.addNotify();
if(thread==null) {
thread=new Thread(this);
}
thread.start();
}
public void run() {
while(true) {
render();
}
}
public void render() {
BufferStrategy bs=getBufferStrategy();
if(bs==null) {
createBufferStrategy(2);
return;
}
Graphics2D g=(Graphics2D)bs.getDrawGraphics();
Graphics2D imageG=(Graphics2D)image.createGraphics();
image.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
imageG.setColor(Color.BLUE);
imageG.fillRect(0, 0, image.getWidth(), image.getHeight());
//rendering other stuffs
imageG.setColor(Color.WHITE);
Font f=new Font(Font.SANS_SERIF, Font.PLAIN, 15);
imageG.setFont(f);
imageG.drawString("hello", 40, 50);
g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Application());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
【问题讨论】:
标签: java string swing graphics2d