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


Groups > comp.lang.java.gui > #3981

Re: Anti-aliasing in imag

From "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this>
Subject Re: Anti-aliasing in imag
Message-ID <nospam-A15874.23181413082008@aioe.org> (permalink)
Newsgroups comp.lang.java.gui
References <48a39fd0$0$2158$ec3e2dad@news.usenetmonster.com>
Date 2011-04-27 15:48 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui,comp.l
In article <48a39fd0$0$2158$ec3e2dad@news.usenetmonster.com>,
 "Kenneth P. Turvey" <kt-usenet@squeakydolphin.com> wrote:

> On Wed, 13 Aug 2008 19:07:14 -0700, Peter Duniho wrote:
> 
> > On Wed, 13 Aug 2008 17:55:10 -0700, Kenneth P. Turvey
> > <kt-usenet@squeakydolphin.com> wrote:
> > 
> >> I've got a simple problem that I'm sure someone in this group can help
> >> me with.  Let me say what I'm currently doing first.
> >>
> >> I have an image that has some transparent portions and I want to cut a
> >> circle out of the image.
> > 
> > Do you want to remove a circular area from inside the image?  Or do you
> > want to use only a ciruclar area from inside the image as your new
> > image?
> > 
> > That is, the transformed image, will it be the original image with a
> > hole in it?  Or will it be the original image with everything outside
> > the circular area excluded?
> > 
> > This description:
> > 
> >> So first I trim it down to a square subimage and then I go through it
> >> pixel by pixel and set the alpha channel to clear for those pixels
> >> outside a circle with the sub-image's diameter.
> > 
> > Makes me think you're taking a circular subset of the image, but other
> > parts of your message seem to contradict that.  I'm a bit confused.
> > 
> > If I have understood the goal correctly, then it seems to me that the
> > easiest way to transform your original image is to draw it into a new
> > image, clipping to the circular shape you want.
> > 
> > For example, here's some code that works when just drawing into the
> > Graphics2D passed to the paintComponent() method:
> > 
> >      // Where x, y, width, and height describe your circular area and
> >      image
> > is a reference
> >      // to the image you want to clip
> > 
> >      Area areaOval = new Area(new Arc2D.Double(x, y, width, height, 0,
> >      360,
> > Arc2D.PIE));
> >      Shape shapeClipSave = gfx2.getClip();
> > 
> >      gfx2.setClip(areaOval);
> >      gfx2.drawImage(image, 0, 0, null);
> >      gfx2.setClip(shapeClipSave);
> > 
> > You should be able to use basically the same thing drawing into a
> > Graphics2D instance you get from a new BufferedImage instance.  Just
> > make sure you've enabled anti-aliased rendering, and when the image is
> > drawn clipped into the new BufferedImage, the edges should wind up
> > anti-aliased.
> > 
> > Pete
> 
> I tried what you suggested, but with disappointing results.  Here's the 
> altered method:
> 
>    private void clearOutsideCircle() {
>         assert image.getWidth() == image.getHeight() : "Image should be 
> square"; 
>         int radius = image.getWidth() / 2; 
>         Area areaOval = new Area(new Arc2D.Double(0, 0, image.getWidth(), 
>                 image.getHeight(), 0, 360, Arc2D.PIE));
>         BufferedImage newImage = new BufferedImage(image.getWidth(), 
>                 image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); 
>         Graphics2D graphics = newImage.createGraphics();
>         graphics.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, 
>                 RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
>         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
>                 RenderingHints.VALUE_ANTIALIAS_ON);
>         graphics.setClip(areaOval);
>         graphics.drawImage(image, 0, 0, null); 
>         image = newImage; 
>     }
> 
> After this the image still doesn't look anti-aliased.  It seems like the 
> clipping region is either on or off.  
> 
> Do you see anything in the code that might be the problem?  Any other 
> suggestions?

Some implementations do better than others at honoring the hints. Try 
this with any .jpg:

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

public class Clip extends JPanel {

    private BufferedImage image;
    private Ellipse2D.Double border = new Ellipse2D.Double();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Clip();
            }
        });
    }

    public Clip() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);    
        try {
            image = ImageIO.read(new File("clip.jpg"));
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.exit(1);
        }
        frame.setSize(new Dimension(
            image.getWidth(), image.getHeight()));
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();
        g2d.setPaint(Color.BLUE);
        g2d.fillRect(0, 0, width, height);
        border.setFrame(0, 0, width, height);
        g2d.setClip(border);
        g2d.drawImage(image, 0, 0, width, height, this);
    }
}
-- 
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Anti-aliasing in image cl "Kenneth P. Turvey" <kenneth.p..turvey@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Kenneth P. Turvey" <kenneth.p..turvey@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
    Re: Anti-aliasing in imag "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Kenneth P. Turvey" <kenneth.p..turvey@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Kenneth P. Turvey" <kenneth.p..turvey@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Kenneth P. Turvey" <kenneth.p..turvey@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Kenneth P. Turvey" <kenneth.p..turvey@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
  Re: Anti-aliasing in imag "Peter Duniho" <peter.duniho@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000
    Re: Anti-aliasing in imag "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000

csiph-web