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 Graphic
s 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.