有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我在设置图形2D时遇到问题

我正在做一个简单的游戏,屏幕上画有两个矩形,用户通过键盘箭头键输入,其中一个矩形(玩家)将移动。我制作了一个Main类和一个Play类,它由我所有的游戏代码组成,这个代码包括init()update()render()等方法,这个init()处理所有的初始条件,update()处理来自输入的移动和按键输入,render()方法处理将内容绘制到屏幕上,例如背景和矩形

我遇到的问题是在我的屏幕上绘制矩形(用我的变量设置),把它们放在我的渲染类中,我的方法是用Graphic设置的,我被告知我需要Graphics2D,所以我试图将我的图形更改为Graphics2D,这样我就可以绘制我的矩形

以下是我的矩形,在我的ode顶部设置了我的变量:

Rectangle rectOne = new Rectangle(shiftX, shiftY,90,90);
Rectangle rectTwo = new Rectangle(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);

这是我的渲染方法,我试图用graphics2D绘制我的矩形:

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{


    if(rectOne.intersects(rectTwo)){
        g.drawString("Intersect", 100, 100);}


    if(g instanceof Graphics2D){//error: Incompatible conditional operand type Graphics and Graphics2D

        Graphics2D g2 = (Graphics2D)g;}//cannot cast from Graphics to Graphics2D

    ((Graphics2D)g).fill(rectOne);//cannot cast from Graphics to Graphics2D
    ((Graphics2D)g).fill(rectTwo);//cannot cast from Graphics to Graphics2D

}

代码旁边的注释显示了我尝试使用此代码时出现的错误。如果我的游戏课需要更多的代码或信息,请告诉我,我会发布


共 (0) 个答案