Well, I was able to achive something that I need:
[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;
private Shape arc;
private boolean overLine;
private boolean overArc;
public MouseOverShapes() {
// create solid stroke with width 1.0f
BasicStroke basicStroke = new BasicStroke(1.0f);
// create arc
arc = new Arc2D.Double(40, 40, 150, 150, 0, 210, Arc2D.CHORD);
// create stroked arc
arc = basicStroke.createStrokedShape(arc);
// create line
line = new Line2D.Double(10, 10, 200, 100);
// create stroked line
line = basicStroke.createStrokedShape(line);
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);
// 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]
Is this the best way?
[Message sent by forum member 'kamre' (kamre)]
http://forums.java.net/jive/thread.jspa?messageID=327043
===========================================================================
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".