Java Reference
In-Depth Information
repaint();
dragX = me.getX();
dragY = me.getY();
}
});
}
@Override
public Dimension getPreferredSize()
{
return d;
}
@Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
Renderin-
gHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.RED);
g2d.fill(rect);
}
}
Listing 7-9 's DragRectPane class subclasses JComponent , presents a noargu-
ment constructor, and overrides getPreferredSize() and paint() .
The constructor first instantiates Dimension and RoundRect-
angle2D.Double ,andthenregistersmouseandmotionlistenerswiththiscompon-
ent.
Whentheuserpressesamousebuttontoinitiateadragoperation(themousebutton
is held down while the mouse cursor is moved), the mouse listener's void
mousePressed(MouseEvent me) methodiscalled.Thismethodfirstinvokesthe
int getX() and int getY() methodsonits MouseEvent argumenttoobtainthe
component-relative location of the mouse cursor when the mouse button was pressed.
Thesemousecoordinates arepassedtotheroundrectangle's contains() method
todeterminewhetherornotthemousecursorwasoverthisshapewhenthebuttonpress
occurred. If the mouse cursor was not over the round rectangle, this method returns.
Search WWH ::




Custom Search