2012-07-08 72 views
1

在我的java程序中,我在我的JFrame中繪製了這些多邊形,並且希望鼠標光標更改爲我點擊的多邊形,所以再次單擊,多邊形將在我的點擊中繪製。我不知道該怎麼做,因爲我不知道如何將光標更改爲多邊形?將我的鼠標光標更改爲我在java中點擊的多邊形

回答

2

您想創建一個自定義光標。創建和註冊該遊標的方法是toolkit.createCustomCursor。基本上,創建一個適當大小的圖像,安裝並根據需要使用。以下是代碼片段:

Toolkit kit = Toolkit.getDefaultToolkit(); 
    Dimension dim = kit.getBestCursorSize(48, 48); 
    BufferedImage buffered = GraphicsUtilities.createCompatibleTranslucentImage(dim.width, dim.height); 
    Shape circle = new Ellipse2D.Float(0, 0, dim.width - 1, dim.height - 1); 
    Graphics2D g = buffered.createGraphics(); 
    g.setColor(Color.BLUE); 
    g.draw(circle); 
    g.setColor(Color.RED); 
    int centerX = (dim.width - 1) /2; 
    int centerY = (dim.height - 1)/2; 
    g.drawLine(centerX, 0, centerX, dim.height - 1); 
    g.drawLine(0, centerY, dim.height - 1, centerY); 
    g.dispose(); 
    Cursor cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor");