Jump to content

Map problem

- - - - -

  • Please log in to reply
4 replies to this topic

#1
zapdude1234

zapdude1234

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I've been messing around with programs to create simple 2d tile maps. I have a simple draw method that passes in a Graphics2d object and creates a grid.
public synchronized void draw(Graphics2D g) {

		Window w = s.getFullScreenWindow();

		g.setColor(w.getBackground());

		g.fillRect(0, 0, s.getWidth(), s.getHeight());

		g.setColor(w.getForeground());

		for(int x = 0; x<10; x++){

			for(int y = 0; y<10; y++){

				g.drawRect(x * 32, y * 32, 32,32);

				

				

			}

		}

		g.drawString("mouse x: " + mousex, 400, 400);

		g.drawString("mouse y: "+ mousey, 400, 420);

		

	}

i want to make it so the grid fills the rectangle in the "for" loop based on a map array. this is what i have but there seems to be an error
public synchronized void draw(Graphics2D g) {

		Window w = s.getFullScreenWindow();

		g.setColor(w.getBackground());

		g.fillRect(0, 0, s.getWidth(), s.getHeight());

		g.setColor(w.getForeground());

		for(int x = 0; x<10; x++){

			for(int y = 0; y<10; y++){

				rect = new Rectangle(x * 32, y * 32, 32,32);

		               g.drawRect(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight());

				

				

			}

		}

		g.drawString("mouse x: " + mousex, 400, 400);

		g.drawString("mouse y: "+ mousey, 400, 420);

		

	}

it says im passing in double values when i need int values. any idea?

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Could you tell us in what line it is giving that error? The only thing I can see that could cause that is the 32,32 that may be recognized as 32.32 but I find that hard to believe.

#3
zapdude1234

zapdude1234

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
It says the error is in the line with the g.drawRect(). It describes the parameters needed as(Int,Int,Int Int) but it says im passing in a (double,double,double.double) parameter. i was wondering if anyone has suggestions about how to either change the doubles to integers or how to better call/store the grid data.

#4
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Oh right the getX, getY, getHeight, getWidth functions return doubles. Wasn't expecting that since the attributes themselves seem to be integers.

Well, you just have to convert the doubles to integers:

g.drawRect((int) rect.getX(), (int) rect.getY(), ...);


#5
zapdude1234

zapdude1234

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Wow that was simple! haha thank you!!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users