【问题标题】:Java, how to drawing rectangle variables on my screenJava,如何在我的屏幕上绘制矩形变量
【发布时间】:2013-01-04 07:27:32
【问题描述】:

我编写了一个简单的 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 中提供的信息应该是浮点数,一切都是如此,那么为什么它一直给我这个错误?

【问题讨论】:

    标签: java compiler-errors rectangles


    【解决方案1】:

    这接缝是双值:

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

    【讨论】:

    • 非常感谢,矩形现在已绘制在屏幕上。
    • 是的,有机会请accept回答。 @MrSmith42 接受答案对我们所有人都有好处。如果有人在未来寻找这个问题的答案,通过看到大的“勾号”来知道答案“有效”是值得的。已回答的问题在搜索中也很明显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2013-09-20
    相关资源
    最近更新 更多