Java中,如何绘制矩形变量在我的屏幕(Java, how to drawing rectangle

2019-10-30 21:01发布

我已经编写了一个简单的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提供的信息应该是彩车这一切,为何还不断给我这个错误?

Answer 1:

This seams to be double values:

rectOne.getX(), rectOne.getY(), rectOne.getWidth(), rectOne.getHeight()

The methods return doubles. See here API

Because you set float values, simply use this:

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


文章来源: Java, how to drawing rectangle variables on my screen