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


Groups > comp.lang.java.programmer > #36185 > unrolled thread

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

Started bymcheung63@gmail.com
First post2017-01-13 10:44 -0800
Last post2017-01-13 19:30 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#36185 — how to get high res bufferedimage from JComponent in retina display?

Frommcheung63@gmail.com
Date2017-01-13 10:44 -0800
Subjecthow to get high res bufferedimage from JComponent in retina display?
Message-ID<2fbe4456-4d80-4af2-9be1-0e002d00ad87@googlegroups.com>
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;
	}

[toc] | [next] | [standalone]


#36186

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2017-01-13 16:17 -0600
Message-ID<o5bjip$cq$1@dont-email.me>
In reply to#36185
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

[toc] | [prev] | [next] | [standalone]


#36188

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-01-13 19:30 -0500
Message-ID<o5brfa$1sr4$1@gioia.aioe.org>
In reply to#36186
On 1/13/2017 5:17 PM, Knute Johnson wrote:
> What's a retina display?

https://en.wikipedia.org/wiki/Retina_Display

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web