代码之家  ›  专栏  ›  技术社区  ›  Nitz

如何在Java中绘制鼠标光标的矩形?

  •  4
  • Nitz  · 技术社区  · 16 年前

    嘿,伙计们,我在我的项目中做了一个模块,用户可以用铅笔画任何东西。

    现在我想为这个绘图模块创建一个橡皮擦,所以我需要它,这样一旦用户单击橡皮擦按钮,然后围绕我的鼠标光标,我希望它周围有一个小矩形形状,这样用户就可以使用它来擦除绘图中的某些部分。

    怎么做?有什么建议吗?

    1 回复  |  直到 12 年前
        1
  •  6
  •   stacker    16 年前

    我建议不要在光标周围画一个矩形,而是为所选工具设置一个自定义的光标。

    只是为了展示API,(未测试)类似的东西应该可以工作。

    Image cursorImg = new ImageIcon("rectangle.gif").getImage();        
    Point hotspot = new Point(0, 0);     // should be set to the center of your rectangle    
    Cursor cursor = getToolkit().createCustomCursor(cursorImg, hotspot, "cursorname");
    
    YourComponent.setCursor( cursor );
    

    编辑:

    我必须添加gettoolkit()是 java.awt.Component