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/