Drawing HiDPI component to BufferedImage
Hendrik Schreiber
hs at tagtraum.com
Fri Jan 24 06:20:03 PST 2014
On Jan 24, 2014, at 14:31, Sergey Bylokhov <Sergey.Bylokhov at oracle.com> wrote:
> You can draw the component to the VolatileImage. or you can create double size bufferedimage and set scale on its graphics to 2.
Drawing to a VolatileImage created via
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration().createCompatibleVolatileImage(100, 100);
seems to do the trick.
However, your other suggestion (double size bufferedimage), does not work. Is this intended or a bug?
Thanks!
-hendrik
Here's a simple demo class. I'd expect both checkboxes to render in the same quality, but they don't using a BufferedImage.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class RenderHiDPI {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final JLabel label = new JLabel();
frame.getContentPane().setLayout(new FlowLayout());
final JCheckBox hiDPICheckBox = new JCheckBox("Hi DPI");
hiDPICheckBox.setFocusable(false);
frame.getContentPane().add(hiDPICheckBox);
frame.getContentPane().add(label);
final JCheckBox checkBox = new JCheckBox("Lo DPI");
checkBox.setSize(new Dimension(100, 100));
final BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
final Graphics g = image.getGraphics();
((Graphics2D)g).scale(2f, 2f);
checkBox.paint(g);
g.dispose();
label.setIcon(new Icon(){
@Override
public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
final Graphics2D graphics = (Graphics2D)g.create();
graphics.scale(0.5f, 0.5f);
graphics.drawImage(image, x, y, c);
}
@Override
public int getIconWidth() {
return 100;
}
@Override
public int getIconHeight() {
return 100;
}
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
}
});
}
}
More information about the macosx-port-dev
mailing list