Hi,
There is an alternative to the mouse point rectangle. Create special sense
shapes with a somewhat thicker stroke and test if that sense shape contains the
mouse point. I don't know which solution is better. I changed your code to
demonstrate. (I took the liberty to set a cross hair cursor for easier testing).
[code]
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class MouseOverShapes extends JComponent {
private Shape line, senseLine;
private Shape arc, senseArc;
private boolean overLine;
private boolean overArc;
public MouseOverShapes() {
this.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
// create solid stroke with width 1.0f
BasicStroke basicStroke = new BasicStroke(1.0f);
BasicStroke senseStroke = new BasicStroke(4.0f);
// create arc
Shape shape = new Arc2D.Double(40, 40, 150, 150, 0, 210, Arc2D.CHORD);
// create stroked arc
arc = basicStroke.createStrokedShape(shape);
senseArc = senseStroke.createStrokedShape(shape);
// create line
shape = new Line2D.Double(10, 10, 200, 100);
// create stroked line
line = basicStroke.createStrokedShape(shape);
senseLine = senseStroke.createStrokedShape(shape);
overLine = false;
overArc = false;
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
// int r = 2;
// int d = 2 * r + 1;
// Rectangle rect = new Rectangle(e.getX() - r, e.getY() - r, d,
// d);
// boolean intersects = line.intersects(rect);
boolean intersects = senseLine.contains(e.getPoint());
if (intersects != overLine) {
overLine = intersects;
repaint();
}
// intersects = arc.intersects(rect);
intersects = senseArc.contains(e.getPoint());
if (intersects != overArc) {
overArc = intersects;
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(overLine ? Color.red : Color.black);
// fill instead of draw
g2d.fill(line);
g2d.setColor(overArc ? Color.red : Color.black);
// fill instead of draw
g2d.fill(arc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Mouse Over Shapes");
frame.getContentPane().add(new MouseOverShapes());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
});
}
}
[/code]
Piet
[Message sent by forum member 'pietblok' (pietblok)]
http://forums.java.net/jive/thread.jspa?messageID=327065
===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[email protected] and include in the body of the message "help".