Hi Ralph,

    Looks like the geometry for Rectangle2D is messed up as you
surmised, and while contains() works properly, drawing does not.  I
suspect this has something to do with all the 'legacy' notes in the 2D
stuff.  And, they did it up good.  There's a package called
sun.awt.Albert  ( no kidding ) in rt.jar; no source for it, but that's where
the specific geometry classes are.  There's stuff like
sun.awt.Albert.TGRect;
sun.awt.Albert.TRectGeometry;

and I suspect that's where the problem lies.  You should post your
example as a bug, because it is, and a deep one.  Anyone from Sun ( maybe
Albert ) listening?

    The good news is, I guess, that's there is a one line workaround.  I
tested it on NT and it works properly there.  Replace

        area[0] = new Area (new Rectangle2D.Float(10,10,40,40));

with

    area[0] = new Area

        new GeneralPath( new Rectangle2D.Float(10,10,40,40) ));

    That is, create a GeneralPath from your Rectangle and put that
in your Area array.

    BTW, you'd get less code and auto bufering if you used JFrame and JPanel
in the program.  Since this is Java 2 specific, there's little in the way of
tradeoffs.


                                    Joe Sam Shirah
                                    Autumn Software


-----Original Message-----
From: Ralph E. LaChance <[EMAIL PROTECTED]>
To: lkfds <[EMAIL PROTECTED]>
Date: Friday, May 21, 1999 1:49 PM
Subject: [java2d] transform problem w/ rectangle (jdk1.2.1)


>Hello,
>
>We're seeing some weirdness in translating (we think)
>a rectangle. Our example is trivial -- perhaps we got
>it wrong.  Any ideas?
>
>Attached code, draws one rectangle and 3 ellipses,
>intended to demonstrate dragging an area around
>a small canvas.
>
>The behavior is decidedly different for the rectangle
>in this demo than than for the ellipses.  Pick and drag
>the ellipses, they move and drop into a new position;
>pick and drag the rectangle, it moves, but when dropped
>it redraws in its old position, and (at first blush) seems
>not to be pick-able anymore.
>
>In fact, after  dropping,  the rectangle, has "moved"
>to the new location -- it can be picked from the hidden
>drop point and dragged again -- but it always redraws
>at the original location....
>
>We'd be happy for any guidance.  Thanks in advance.
>
>        'best
>        -Ralph LaChance
>
>        (Friday, May 21, 1999,  1:38 PM)
>
>------------------ the code ------------------
>
>import java.awt.*;
>import java.awt.event.*;
>import java.awt.geom.*;
>
>public class Mover extends Frame
>{
>    public static void main(String[] arg)
>    {
>        new Mover();
>    }
>
>    Mover()
>    {
>        super("Mover");
>        addWindowListener(new WindowAdapter() {
>            public void windowClosing(WindowEvent e)
>                { System.exit(0); } } );
>        MoverPanel mp = new MoverPanel();
>        add(mp);
>        pack();
>        show();
>    }
>
>class MoverPanel extends Canvas
>        implements MouseListener, MouseMotionListener
>{
>    boolean moving = false ;
>    int moveIndex = -1 ;
>    int startx ;
>    int starty ;
>    int endx ;
>    int endy ;
>    Image offscreen = null ;
>    Area[] area ;
>    AffineTransform at ;
>    BasicStroke dashes ;
>    BasicStroke solid ;
>
>
>    MoverPanel()
>    {
>        addMouseListener(this);
>        addMouseMotionListener(this);
>        area = new Area[4];
>//
>//      alternate code, all ellipses
>//
>//    area[0] = new Area (new Ellipse2D.Float  (10,10,40,40));
>//
>        area[0] = new Area (new Rectangle2D.Float(10,10,40,40));
>        area[1] = new Area (new Ellipse2D.Float  (80,80,20,50));
>        area[2] = new Area (new Ellipse2D.Float  (130,130,60,60));
>        area[3] = new Area (new Ellipse2D.Float  (200,200,50,20));
>
>        setSize(300,250);
>        at = new AffineTransform();
>        float[] pattern = { 5f,5f };
>        dashes = new BasicStroke(1f,BasicStroke.CAP_BUTT,
>        BasicStroke.JOIN_BEVEL,1f,pattern,0f);
>        solid  = new BasicStroke(1f,BasicStroke.CAP_BUTT,
>                BasicStroke.JOIN_BEVEL);
>    }
>
>
>    // null out offscreen buffer as part of invalidation
>    public void invalidate()
>    {
>        super.invalidate() ;
>        offscreen = null ;
>    }
>
>    // override to *not* erase background before painting
>    public void update (Graphics g)
>    {
>         paint (g) ;
>    }
>
>
>    public void paint(Graphics g)
>    {
>        if (offscreen == null)
>          offscreen = createImage (getSize().width, getSize().height);
>
>        Graphics2D g2 = (Graphics2D) offscreen.getGraphics() ;
>        g2.setClip (0,0,getSize().width, getSize().height) ;
>        super.paint (g2) ;
>
>        for(int i=0; i<area.length; i++)
>          {
>            g2.setColor (i==moveIndex ? Color.yellow : Color.red) ;
>                    g2.fill(area[i]) ;
>            g2.setColor (Color.black) ;
>                    g2.setStroke(solid) ;
>                    g2.draw(area[i]) ;
>                }
>
>                if(moving)
>          {
>                     g2.setTransform(at);
>                     g2.setStroke(dashes);
>             g2.setColor (Color.blue) ;
>                     g2.draw(area[moveIndex]) ;
>          }
>
>        ((Graphics2D)g).drawImage (offscreen, 0,0, null) ;
>        g2.dispose() ;
>
>    }
>    public void mouseMoved(MouseEvent event)
>    {
>        moving = false;
>        moveIndex = -1 ;
>    }
>
>    public void mouseDragged(MouseEvent event)
>    {
>        if(!moving)
>            return;
>
>        endx = event.getX() ;
>        endy = event.getY() ;
>        at.setToTranslation(endx - startx,endy - starty) ;
>        repaint() ;
>    }
>
>    public void mousePressed(MouseEvent event)
>    {
>        moving = false ;
>        startx = event.getX() ;
>        starty = event.getY() ;
>
>        // hit detect in reverse of draw order to get top/bottom effect
>        for(int i=area.length - 1; i>=0; i--)
>          {
>            if(area[i].contains(startx,starty))
>              {
>                moving = true ;
>                moveIndex = i ;
>                at.setToTranslation(0,0) ;
>                break ;
>              }
>          }
>    }
>    public void mouseReleased(MouseEvent event)
>    {
>        if(moving)
>          {
>            area[moveIndex].transform(at) ;
>            moving = false ;
>            moveIndex = -1 ;
>            repaint() ;
>          }
>    }
>    public void mouseEntered (MouseEvent event) { }
>    public void mouseExited  (MouseEvent event) { }
>    public void mouseClicked (MouseEvent event) { }
>}
>}
>
>------------------  end of code  ------------------
>


=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/

Reply via email to