Java,如何在我的屏幕上绘制矩形变量 [英] Java, how to drawing rectangle variables on my screen

查看:551
本文介绍了Java,如何在我的屏幕上绘制矩形变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的Java游戏,其中屏幕上有两个矩形,其中一个矩形移动,另一个保持静止,移动矩形与键盘箭头输入移动,可以向上,下,左或右移动。我遇到的问题是在屏幕上绘制我的矩形,我有我的变量设置如下所示:

I have coded a simple Java game where there are two rectangles on the screen, one of the rectangles moves and the other stays still, the moving Rectangle moves with keyboard arrow input and can move either up, down, left or right. The problem I am having is drawing my rectangles on the screen, I have my variables set up as shown:

  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绘制我的两个矩形:

and under my render method (which holds all the stuff I want drawn onto the screen) I have told Java to draw my two rectangles:

    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下得到以下错误: p>

But I am getting the following error under fillRect:

This method fillRect(float,float,float,float) in the type graphics is 
    not applicable for the arguments (double,double,double,double)

这让我感到困惑,因为我的理解是,

This is confusing me as from what I understand it is saying the information provided in fillRect should be floats which everything is, so why does it keep giving me this error?

推荐答案

这个接缝是双重的值:

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

方法返回双精度。请参阅这里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,如何在我的屏幕上绘制矩形变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆