Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36186

Re: how to get high res bufferedimage from JComponent in retina display?

From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Re: how to get high res bufferedimage from JComponent in retina display?
Date 2017-01-13 16:17 -0600
Organization A noiseless patient Spider
Message-ID <o5bjip$cq$1@dont-email.me> (permalink)
References <2fbe4456-4d80-4af2-9be1-0e002d00ad87@googlegroups.com>

Show all headers | View raw


On 1/13/2017 12:44, mcheung63@gmail.com wrote:
> how to get high res bufferedimage from JComponent in retina display? Below just doesnt work, image is low res.
>
>
> 	private BufferedImage toBufferedImage(Component component) {
> 		Dimension size = component.getSize();
> 		BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
> 		Graphics2D g2 = image.createGraphics();
>
> 		component.paint(g2);
> 		return image;
> 	}
>

By low res do you mean the same size as the component?  Were you 
expecting it to be bigger?  Do you want a scaled up image of the comopnent?

You should dispose the graphics context after you are done using it to 
draw with.  Components are typically not opaque and look funny when you 
paint them without the white background so make the component opaque.  A 
BufferedImage.TYPE_INT_RGB may work fine for you but sometimes if you 
get a compatible image it will be better.

What's a retina display?

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test1 {
     public static void main(String... args) {
         EventQueue.invokeLater(() -> {
             JFrame f = new JFrame("test1");
             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             JLabel l = new JLabel("hello world!");
             l.setOpaque(true);
             f.add(l,BorderLayout.CENTER);
             JButton b = new JButton("get image of label");
             b.addActionListener(ae -> {
                 GraphicsConfiguration gc = GraphicsEnvironment.
                  getLocalGraphicsEnvironment().getDefaultScreenDevice().
                  getDefaultConfiguration();
                 BufferedImage image =
 
gc.createCompatibleImage(l.getWidth()*10,l.getHeight()*10);
                 Graphics2D g = image.createGraphics();
                 g.scale(10.0,10.0);
                 l.paint(g);
                 g.dispose();
                 try {
                     ImageIO.write(image,"JPEG",new File("image.jpg"));
                 } catch (IOException ioe) {
                     ioe.printStackTrace();
                 }
             });
             f.add(b,BorderLayout.SOUTH);
             f.pack();
             f.setVisible(true);
         });
     }
}


-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how to get high res bufferedimage from JComponent in retina display? mcheung63@gmail.com - 2017-01-13 10:44 -0800
  Re: how to get high res bufferedimage from JComponent in retina display? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-01-13 16:17 -0600
    Re: how to get high res bufferedimage from JComponent in retina display? Arne Vajhøj <arne@vajhoej.dk> - 2017-01-13 19:30 -0500

csiph-web