简体   繁体   English

在Java中设置自定义游标的大小

[英]Setting the size of a customized cursor in java

I have customized a cursor on a label component called ageLabel using the following code 我使用以下代码在名为ageLabel的标签组件上自定义了光标

ageLabel.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new javax.swing.ImageIcon(getClass().getResource("/images/image1.jpg")).getImage(),new Point(5,5),"custom cursor"));

It works fine anyway but what I want to do is increase the size of the customized cursor. 无论如何它都能正常工作,但是我想做的是增加自定义光标的大小。 I have tried changing the point to (10,10) but the cursor size wouldn't change. 我尝试将点更改为(10,10),但光标大小不会改变。 I also tried changing the dimensions of the image that I used ,it still wouldn't change. 我还尝试过更改所用图像的尺寸,但仍然不会更改。 I have searched through the internet but to no avail. 我已经通过互联网搜索,但无济于事。 Is It possible to resize the cursor in anyway? 是否可以调整光标大小? If it is how do I do that? 如果是我该怎么做? Thanks in advance for all helps. 在此先感谢您提供的所有帮助。

Java's Image class has a built in function for scaling. Java的Image类具有用于缩放的内置函数。

Documentation: https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance(int,%20int,%20int) 文档: https : //docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance(int,%20int,%20int)

My approach to the problem: 我的解决方法:

  1. Split the line up into many to make it look a bit nicer (not necessary). 将行拆分为许多行,以使其看起来更好(不必要)。
  2. Add an extra method call to scale the image up. 添加一个额外的方法调用以放大图像。

Potential Solution: 潜在的解决方案:

    URL imageResource = getClass().getResource("/images/image1.jpg");
    ImageIcon imageIcon = new ImageIcon(imageResource);
    Image image = imageIcon.getImage();

    // This is the important line that scales the image up!
    Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Cursor cursor = toolkit.createCustomCursor(scaledImage, new Point(5, 5), "Custom Cursor");
    ageLabel.setCursor(cursor);

Afterthoughts: 事后思考:

Remember to replace newWidth and newHeight with the values you want. 请记住用所需的值替换newWidthnewHeight

After you get it working, I'd even go further and extract some of this code into methods. 当它开始工作后,我什至会更进一步,将其中的一些代码提取到方法中。

You might need to tweak your imports a bit if it isn't compiling. 如果导入未编译,则可能需要对其进行一些调整。

I haven't tried running this code so I don't know if it works. 我没有尝试运行此代码,所以不知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM