2

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);
}
}
Vin
  • 701
  • 1
  • 9
  • 30
  • 1
    *"I tried to activate anti-aliasing.."* I don't see anywhere in that code that it tried.. – Andrew Thompson Jan 21 '15 at 14:17
  • 2
    Try various combinations of options as seen in [this code](http://stackoverflow.com/a/5998015/418556). – Andrew Thompson Jan 21 '15 at 14:18
  • 1
    Se also this related [example](http://stackoverflow.com/a/2658663/230513). – trashgod Jan 21 '15 at 16:41
  • I added the line of code about anti-aliasing. I deleted it because it did not work. – Vin Jan 21 '15 at 19:28
  • 1
    I read the examples but is not my case. Maybe the BufferedImage resize from 100x100 to 700x500 is the problem. I thought to increase the size of BufferedImage. Is it the way? – Vin Jan 21 '15 at 19:35

1 Answers1

2

1: Antialiasing for text is KEY_TEXT_ANTIALIASING

2: You are stretching the image to the point where the text gets very pixelated. Either make your image larger, make your font larger, or dont fill the canvas with the image (only draw portions of it), and instead color the canvas as well. - look at drawImage(...)

Terje
  • 1,753
  • 10
  • 13