Exporting a JPanel to an image

The panel needs to be laid out based on it’s requirements. If the panel hasn’t being realized on the screen yet, it may not render the way you expect it do The following example assumes that the panel has not being displayed on the screen… setSize(getPreferredSize()); BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g … Read more

Java: Filling a BufferedImage with transparent pixels

After you clear the background with the CLEAR composite, you need to set it back to SRC_OVER to draw normally again. ex: //clear g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR)); g2.fillRect(0,0,256,256); //reset composite g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); //draw g2.setPaint(Color.RED); g2.fillOval(50,50,100,100);

Java – resize image without losing quality

Unfortunately, there is no recommended out-of-the-box scaling in Java that provides visually good results. Among others, here are the methods I recommend for scaling: Lanczos3 Resampling (usually visually better, but slower) Progressive Down Scaling (usually visually fine, can be quite fast) One-Step scaling for up scaling (with Graphics2d bicubic fast and good results, usually not … Read more

Bufferedimage resize

Updated answer I cannot recall why my original answer worked but having tested it in a separate environment, I agree, the original accepted answer doesn’t work (why I said it did I cannot remember either). This, on the other hand, did work: public static BufferedImage resize(BufferedImage img, int newW, int newH) { Image tmp = … Read more

Re-sizing an image without losing quality

The best article I have ever read on this topic is The Perils of Image.getScaledInstance() (web archive). In short: You need to use several resizing steps in order to get a good image. Helper method from the article: public BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint, boolean higherQuality) { int type = (img.getTransparency() … Read more