I have an application which heavily leverages swing components, using JButtons and JLabels within a JPanel. I use a GridBagLayout to layout these components, and on screen they display just fine. Running on windows if that matters.
The problem I have been struggling with has to do with the clarity of the printed image, whether this is printed to paper, or a jpg. The image tends to look muddy. The edges of buttons, the text within buttons or labels, even straight lines look fuzzy.
Below is an example that generates a 50x50 jpg which contains a 40x40 rectangle inside of it. There seems to be a faint shadow line inside and outside of the rectangle, faint dots at the corners, and even the edges of the line seem fuzzy. I see the same results with swing components too.
In my searching and reading, I've found a lot of references to RenderingHints, but I've seen too many suggestions of the form "try setting this" without any explanation to discern which hint(s) might apply.
Any suggestions/pointers would be appreciated.
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
public class MyFrame extends JFrame {
public static void main(String[] args) {
new MyFrame();
}
MyFrame() {
JButton button;
button = new JButton("print jpg");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
takePicture();
}
});
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
add(button);
pack();
setVisible(true);
}
void takePicture() {
BufferedImage img = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,50,50);
g.setColor(Color.black);
g.drawRect(5,5,40,40);
try {
ImageIO.write(img, "jpg", new File("output_file.jpg") );
}
catch (IOException e) {
e.printStackTrace();
}
}
}