Hey guys,

I'm drawing a grid using the nested for loop with draw line method.
Any tips on getting this in the center of my screen?

I am currently using the getWidth and getHeight methods of my center panel but it is still drawing in the top left corner.

It's to do with my drawLine method but I can't get it right...

for(int i = 1; i <= x; i++) {     // y

   for(int e = 1; e <= y; e++) { // x

   g.drawLine(i*h, 0,i*h, centerHeight); // y line
   g.drawLine(0 ,e*w, centerWidth, e*w); // x line

   int X=0;int Y=0;
   if (i == 1 & e == 1) {
       X = (i*w+i*w) - roverWidth;
       Y = ((5-e)*h+(5-e)*h) + roverHeight;
       lblRover.setBounds(0,0,X, Y);

Make sure you take the width and the height of the panel, when the panel have been initialized. In the constructor of the panel, both the width and the height are 0.

This got a little confusing but I had to use the height in the x line and the width in the y line, and the multiply by x/y instead.

g.drawLine(xPos+i*h, yPos ,xPos +i*h, yPos+w*e);
g.drawLine(xPos, e*w+ yPos, xPos+h*i, e*w + yPos);

Edited 3 Years Ago by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.