I am supose to draw a grid and center it with a 10 pix border all the way around it. I have done this but no matter what I do I cannot get a 10 pix border. Here is my code. You will notice i have added 20 to the setsize param to account for 10 pix on both sides and use the "center" command but it does not work properly

import java.awt.*;
import java.awt.event.*;

import javax.swing.JOptionPane;

class DrawGrid extends Canvas
{
int userInput;
public DrawGrid(int userInput) {
this.userInput = userInput;
}

public void paint(Graphics g)
{
for (int i = 0;i<=userInput;i++)
{
g.drawLine(10,100*i+10, 100*userInput+10,100*i+10);
g.drawLine(100*i+10, 10, 100*i+10,100*userInput+10);

}
}
} // class DrawGrid

public class Grid extends Frame implements WindowListener, ActionListener
{ Panel p;
DrawGrid Output;

public static void main(String args[])
{
String str = JOptionPane.showInputDialog(null, "Enter number of boxes: ",
"Box Input", 1);
int userInput = getInt(str);
Frame f = new Grid(userInput);
f.setTitle("Unit 2 Project");
f.setSize(userInput * 100 + 20, userInput * 100 + 20);
f.setVisible(true);
}

public Grid(int userInput)
{
addWindowListener(this);
p = new Panel();
Output = new DrawGrid(userInput);
add("Center", Output);
}

private static int getInt(String str) {
int retVal = 0;
try
{
retVal = Integer.parseInt(str);
return retVal;
}catch (NumberFormatException nfe){
return -1;
}

}

public void actionPerformed(ActionEvent evt)
{ String arg = evt.getActionCommand();
}

public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
} // class Grid

I suppose you are referring to the previously posted question:

http://www.jiskha.com/display.cgi?id=1260893052

The requirements are rather strict, that the borders be exactly 10 pixels, and the boxes be 100 pixels.

I assume you have correctly interpreted the box size of 100 pixels as including the line, leaving an empty space of 99 pixels.

The problem is the frame size.

I have not read or found information on the frame size. The parameters used in the SetSize() method is for the external dimensions, which includes a rim of 4 pixels, and a banner of 23 pixels. I have not read about or found these numbers. I have parametrized the dimensions and found out by trial and error.

So the SetSize parameter should really be SetSize(RIM+2*BORDER+userinput*SIZE+1+RIM, BANNER+userinput*SIZE+RIM+1)
where
RIM=4
BANNER=23
BORDER=100

The reason 1 has to be added to each dimension (x,y) is because to draw the boxes, you need to add 1 pixel for the last line.

Try to work with these numbers, using BORDER=0 to confirm the parameters, and when you will see the exterior of the boxes touching the limits of the frame. After that, you can reset BORDER to 10 or any other value you desire.

Let me know if this works.

Your width and height appear to be too small; when I run it, I get a 10 pixel border on the left and the top, but the entire grid is not shown.

Correction:

SetSize(RIM + BORDER + userinput*SIZE+1 + BORDER + RIM, BANNER + BORDER + userinput*SIZE+1 + BORDER + RIM)

To center the grid with a 10 pixel border all the way around it, you need to make the following changes to your code:

1. In the `DrawGrid` class, adjust the `paint` method to start the grid lines from 10 pixels inside the canvas instead of starting from pixel 10. You can achieve this by changing the starting position of `g.drawLine` to `(20, 100*i+10)` and `(100*i+10, 20)`.

2. In the `Grid` class, adjust the `f.setSize` call to add an additional 20 pixels to the width and height of the frame. This accounts for the 10 pixel border on each side. Change it to `f.setSize(userInput * 100 + 40, userInput * 100 + 40)`.

Below is the modified code with the changes:

```java
import java.awt.*;
import java.awt.event.*;

import javax.swing.JOptionPane;

class DrawGrid extends Canvas
{
int userInput;

public DrawGrid(int userInput) {
this.userInput = userInput;
}

public void paint(Graphics g)
{
for (int i = 0;i<=userInput;i++)
{
g.drawLine(20, 100*i+10, 100*userInput+10,100*i+10);
g.drawLine(100*i+10, 20, 100*i+10,100*userInput+10);
}
}
}

public class Grid extends Frame implements WindowListener, ActionListener
{
Panel p;
DrawGrid Output;

public static void main(String args[])
{
String str = JOptionPane.showInputDialog(null, "Enter number of boxes: ",
"Box Input", 1);
int userInput = getInt(str);
Frame f = new Grid(userInput);
f.setTitle("Unit 2 Project");
f.setSize(userInput * 100 + 40, userInput * 100 + 40);
f.setVisible(true);
}

public Grid(int userInput)
{
addWindowListener(this);
p = new Panel();
Output = new DrawGrid(userInput);
add("Center", Output);
}

private static int getInt(String str) {
int retVal = 0;
try
{
retVal = Integer.parseInt(str);
return retVal;
}catch (NumberFormatException nfe){
return -1;
}

}

public void actionPerformed(ActionEvent evt)
{
String arg = evt.getActionCommand();
}

public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
```

With these changes, the grid will be properly centered with a 10 pixel border all the way around it.