In general I would like have many shapes and check if mouse is over some shape.
So my approach currently is like this:
[code]
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class MouseOverShapes extends JComponent {
private Line2D line;
private Arc2D arc;
private boolean overLine;
private boolean overArc;
public MouseOverShapes() {
line = new Line2D.Double(10, 10, 200, 100);
arc = new Arc2D.Double(40, 40, 150, 150, 0, 210, Arc2D.CHORD);
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);
if (intersects != overLine) {
overLine = intersects;
repaint();
}
intersects = arc.intersects(rect);
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);
g2d.draw(line);
g2d.setColor(overArc ? Color.red : Color.black);
g2d.draw(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]
But Shape.intersects(...) check intersection of [b]interior of Shape[/b] and
not it's contour. At the same time intersection with Line2D (which doesn't have
any interior at all) works as I need. Is it possible somehow to check
intersection of contour and not interior? Or may be my approach for finding
shapes under mouse cursor is completely wrong?
[Message sent by forum member 'kamre' (kamre)]
http://forums.java.net/jive/thread.jspa?messageID=326769
===========================================================================
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".