簡體   English   中英

如何在Java AWT和/或Swing中更改光標圖像?

[英]How to change the cursor image in Java AWT and/or Swing?

我正在制作一個簡單的圖形編輯器(即繪畫程序 )。 我沒有計划任何花哨的東西,但我確實希望我的程序在進入Paint Panel時將鼠標光標更改為“+”或“O”之類的東西。 就像在Photoshop或GIMP中一樣。 Gimp Cursor

我該怎么做? 關於如何更改鼠標光標,我在AWT / Swing線程中找不到任何內容。

以防有人想要比任何默認游標更“花哨”的東西:可以創建一個自定義光標(如果Toolkit支持它),顯示任意自定義圖像。 原始(沒有光澤的視覺效果)示例:

    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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM