Reputation: 60
I changed image of cursor. I am trying to change size of cursor image but i couldnt. How can i change it's size? Thanks in advance.
Upvotes: 2
Views: 5568
Reputation: 159496
You can change the cursor by using one of the predefined cursor types or supplying an image to have your own cursor, but the system will automatically resize your supplied image based upon the cursor size support of the underlying platform. See the documentation for ImageCursor.getBestSize() and ImageCursor.chooseBestCursor() for details of the algorithm used to choose determine the actual cursor size used.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class ResizableCursor extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
StackPane layout = new StackPane();
layout.setCursor(Cursor.cursor("http://icons.iconarchive.com/icons/artua/star-wars/256/Death-Star-icon.png"));
stage.setScene(new Scene(layout, 200, 200));
stage.show();
}
}
If you want to have an arbitrarily sized node which moves with the mouse and looks and acts like a cursor (but is not a cursor), you can set the cursor to Cursor.NONE, then translate a node to follow the mouse location (as is done in this example when you click on a circle to move it around). Using this strategy it will seem as though the user has a custom cursor of arbitrary size, even though they don't.
Copy of relevant documentation for ImageCursor.getBestSize():
Gets the supported cursor size that is closest to the specified preferred size. A value of (0,0) is returned if the platform does not support custom cursors.
Note: if an image is used whose dimensions don't match a supported size (as returned by this method), the implementation will resize the image to a supported size. This may result in a loss of quality.
Note: These values can vary between operating systems, graphics cards and screen resolution, but at the time of this writing, a sample Windows Vista machine returned 32x32 for all requested sizes, while sample Mac and Linux machines returned the requested size up to a maximum of 64x64. Applications should provide a 32x32 cursor, which will work well on all platforms, and may optionally wish to provide a 64x64 cursor for those platforms on which it is supported.
Upvotes: 6
Reputation: 60
import java.awt.Toolkit;
import javafx.scene.Cursor;
import javafx.scene.image.Image;
public class ColorChooser extends Cursor
{
public override function impl_getAWTCursor(): java.awt.Cursor {
def url = "{__DIR__}eyedropper.png";
var toolkit = Toolkit.getDefaultToolkit();
var image = Image{width:32 height:32 url:url}.bufferedImage;
var hotspot = new java.awt.Point(0,15);
var cursor = toolkit.createCustomCursor(image, hotspot "colorpicker");
return cursor;
}
}
Upvotes: -2
Reputation: 60
VBox vBoxMainLayout = new VBox();
Scene scene = new Scene(vBoxMainLayout);
Image img = newImage(getClass().getResourceAsStream("image.png"));
ImageCursor cursor = new ImageCursor(img, 30, 30);
scene.setCursor(cursor);
Upvotes: 0