我编写了一个简单的Java游戏,其中屏幕上有两个矩形,其中一个矩形移动而另一个保持静止,移动的矩形随键盘箭头输入移动,可以向上,向下,向左或向右移动。我遇到的问题是在屏幕上绘制矩形,我的变量设置如下所示:
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
//my two rectangles are shown under here
Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
并在我的渲染方法下(它包含了我想要绘制到屏幕上的所有内容)我告诉Java绘制我的两个矩形:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
//draws the two rectangles on the screen
g.fillRect(rectOne.getX(), rectOne.getY(), rectOne.getWidth(), rectOne.getHeight());
g.fillRect(rectTwo.getX(), rectTwo.getY(), rectTwo.getWidth(), rectTwo.getHeight());
}
但是我在fillRect:
下遇到以下错误This method fillRect(float,float,float,float) in the type graphics is
not applicable for the arguments (double,double,double,double)
这让我感到困惑,因为据我所知,fillRect中提供的信息应该是浮动的,一切都是,所以为什么它一直给我这个错误?
答案 0 :(得分:2)
这种接缝是双重值:
rectOne.getX(), rectOne.getY(), rectOne.getWidth(), rectOne.getHeight()
The methods return doubles. See here API
因为您设置了浮点值,只需使用:
g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());