This is my first topic here and I apologize you for my bad English (I'm Italian).
I wrote an example 2d application using javax.swing
library.
I used a BufferedImage
in which I rendered shapes, images and font.
The image is rendered in a Canvas (java.awt).
The size of the image is 100x100 and the size of Canvas is 700x500.
The problems now are when I draw strings into image, because the strings are drawn pixelated. I tried to activate anti-aliasing but they seem blurry.
Is there any way to fix this problem?
This is my code:
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);
}
}