如果我使用 Java2D 画一些圆圈。当我将鼠标悬停在任何圆圈上时,有没有办法显示一些文本?即我想显示该圈子的 ID 和其他一些内容。
最佳答案
有多种方法可以实现您想要的目标。这是一种解决方案。我假设您正在使用 Ellipse2D 来创建圆。我假设您正在像 JPanel 一样在 JComponent 上绘制圆圈。
所以你声明了椭圆。
Shape circle = new Ellispe2D.Double(x, y, width, height);
然后,您实现 MouseMotionListener 来检测用户何时将鼠标移动到 JPanel 上。
public void mouseMoved(MouseEvent e){
if(circle.contains(e.getPoint())){
//the mouse pointer is over the circle. So set a Message or whatever you want to do
msg = "You are over circle 1";
}else{
msg = "You are not over the circle";
}
}
然后在paint()或paintComponent方法中(无论您要重写哪个方法来进行绘制):
g2.fill(circle);
g2.drawString(msg, 10, 10); //write out the message
关于Java 2D。悬停在圆上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1837000/