0

I am making a simple game where there are two rectangles drawn on the screen and with user input from the keyboard arrow keys one of the rectangles (the player) will move. I have made a Main class and a Play class which consists of all my game code, this code includes methods such as init(), update() and render(), the init() deals with all the initial conditions, the update() deals with the movement and key inputs from the input and the render() method deals with drawing the stuff onto the screen such as the background and rectangles.

The problem I am having is drawing my rectangles (which are set up with my variables) on my screen by putting them in my render class, my method is set up with Graphics and I have been told I require Graphics2D so I am attempting to change my graphics to Graphics2D so that I can draw my rectangles.

Here are my Rectangles which have been set up with my variables at the top of my ode:

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

and here is my render method where I am trying to draw my rectangles using the 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

}

The comments next to my code shows the errors that are appearing when I try to use this code. If any more code or information is required from my Play class please tell me and I will post it.

jiaweizhang
  • 809
  • 1
  • 11
  • 28
Rahul Khosla
  • 349
  • 9
  • 21
  • Do you see these compile-time errors in eclipse? – Swapnil Jan 03 '13 at 15:31
  • 1
    Are you sure the `Graphics` and `Graphics2D` classes come from the package `java.awt` or are you picking up classes with these names from some other package? (Maybe one of your own classes is called `Graphics` and this is picked up instead of the one from `java.awt`?) – Jesper Jan 03 '13 at 15:33
  • There are only 2 class's the main class which is called Game and the Play class which has all the methods listed above in. About the first question, I have imported everything required if that is what you are asking. @Swanpnil, I see the errors when I hover over the underlined code which is the ones I have added comments to above. – Rahul Khosla Jan 03 '13 at 15:41
  • As per [this link](http://stackoverflow.com/a/8732542/1711796), you may just be missing `import java.awt.Graphics2D`. – Bernhard Barker Jan 04 '13 at 00:22

0 Answers0