简体   繁体   English

getScaledInstance java图像后的高度和宽度-1

[英]Height and Width -1 after getScaledInstance java Image

so for the wanky title. 所以对于怪异的头衔。 Didn't came up with a short pregnant description. 没有提出简短的怀孕描述。

My problem are these lines of code: 我的问题是这些代码行:

ii = new ImageIcon("images/player.png");
image = ii.getImage();
image = image.getScaledInstance(SCALED_X, SCLAED_Y, Image.SCALE_SMOOTH);
positionX = windowWidth / 2 - image.getWidth() / 2; 
positionY = windowHeight - image.getHeight();

When I debugged the code I noticed that the image has a height and width of -1, after calling the getScaledInstance function, and therefore the last two lines return wrong results. 当我调试代码时,我注意到在调用getScaledInstance函数后,图像的高度和宽度为-1,因此最后两行返回错误的结果。 A workaround I came up with is using SCALED_X and SCALED_Y as the new height and width but thats not that pretty -.- Can anybody explain why this is, ie the height and width have the value -1? 我想出的一种解决方法是使用SCALED_X和SCALED_Y作为新的高度和宽度,但是那还不是那么漂亮-.-有人可以解释为什么这就是为什么,即高度和宽度的值为-1吗? Are these properties lost when using the getScaledInstance function (could not find any words about that in the documentation)? 使用getScaledInstance函数时这些属性会丢失吗(在文档中找不到关于此的任何单词)? And is there any way to add them to the Image again? 有什么办法可以将它们再次添加到图像中吗?

Is there a way to reduce the size of an image and keep resp. 有没有办法减小图像的大小并保持响应。 automatically adjust the properties? 自动调整属性?

EDIT: 编辑:

Changed code to get the Image to: 更改代码以将图像获取到:

try {
    image = ImageIO.read(new File("images/player.png")).getScaledInstance(SCALED_X, SCLAED_Y, Image.SCALE_SMOOTH);          
} catch (IOException e) {
    e.printStackTrace();
}

Error with -1 still persists :( -1错误仍然存​​在:(

Reading the Javadocs of the methods used suggests that the image has not been scaled yet when you call getHeight/getWidth: 阅读Javadoc中所用方法的建议表明,当您调用getHeight / getWidth时,图像尚未缩放:

getScaledInstance(): getScaledInstance():

Creates a scaled version of this image. 创建此图像的缩放版本。 A new Image object is returned which will render the image at the specified width and height by default. 返回一个新的Image对象,它将默认以指定的宽度和高度渲染图像。 The new Image object may be loaded asynchronously even if the original source image has already been loaded completely. 即使原始源图像已经完全加载,也可以异步加载新的Image对象。

getHeight(): getHeight():

Determines the height of the image. 确定图像的高度。 If the height is not yet known, this method returns -1 如果高度未知,则此方法返回-1

You have several options: You can wait for the image to be scaled, or use an ImageObserver to calculate the required values when the width and height become available, or use your SCALED_X/Y constants instead of image.getWidth/Height, or scale the image yourself by creating a new BufferedImage and painting the old image onto the new one. 您有几种选择:您可以等待图像缩放,或者在宽度和高度可用时使用ImageObserver计算所需的值,或者使用SCALED_X/Y常量而不是image.getWidth / Height或缩放通过创建一个新的BufferedImage并将旧图像绘制到新图像上来对自己进行成像。

Another possibility is to simply assume that the player image is of the correct size - this will make your game load faster and the player will appear in-game exactly as it was drawn by the artist. 另一种可能性是简单地假定玩家图像的大小正确-这将使您的游戏加载速度更快,并且玩家将完全按照艺术家绘制的方式出现在游戏中。

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

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