Java Panel Problem

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);

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

private static int getInt(String str) {
int retVal = 0;
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

  1. 👍 0
  2. 👎 0
  3. 👁 196
  1. Hopefully a Tu torial will h elp:


    1. 👍 0
    2. 👎 0
  2. I suppose you are referring to the previously posted question:

    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)

    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.

    1. 👍 0
    2. 👎 0
  3. 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.

    1. 👍 0
    2. 👎 0
  4. Correction:
    SetSize(RIM + BORDER + userinput*SIZE+1 + BORDER + RIM, BANNER + BORDER + userinput*SIZE+1 + BORDER + RIM)

    1. 👍 0
    2. 👎 0

Respond to this Question

First Name

Your Response

Similar Questions

  1. Math

    The real numbers $a$ and $b$ satisfy $|a| < 1$ and $|b| < 1.$ (a) In a grid that extends infinitely, the first row contains the numbers $1,$ $a,$ $a^2,$ $\dots.$ The second row contains the numbers $b,$ $ab,$ $a^2 b,$ $\dots.$ In

    asked by Anonymous on March 27, 2020
  2. solar science physics

    Which of the following statements is NOT true about solar inverters? A stand-alone inverter is supposed to work as an AC voltage source for a specified range of AC loads. An ideal grid-tied inverter has its operational voltage and

    asked by pritam on January 29, 2017
  3. Chemistry - Lewis Dots

    Draw the molecule by placing atoms on the grid and connecting them with bonds. Do not identify the charge on each of these species. Include all lone pairs of electrons. H3COCH3 Draw the ion by placing atoms on the grid and

    asked by Amy on December 4, 2012
  4. MATH

    A four by four grid of unit squares contains squares of various sizes (1 by 1 through 4 by 4), each of which are formed entirely from squares in the grid. In each of the 16 unit squares, write the number of squares that contain

    asked by mathemagiacian on August 15, 2013
  5. Calculus

    Find dy/dx by implicit differentiation. (sin(pix)+cos(pix))^7=29

    asked by Anonymous on March 30, 2018
  1. Statistics

    The Hill of Tara in Ireland is a place of great archaeological importance. This region has been occupied by people for more than 4,000 years. Geomagnetic surveys detect subsurface anomalies in the earth's magnetic field. These

    asked by ReeSha on July 7, 2011
  2. electricity

    The grid in a high-vacuum triode is usually kept negatively charged with respect to the cathode so that the electrons may be? A)accelerated toward the anode. B)accelerated toward the cathode. C)attracted to the anode instead of

    asked by angela on August 24, 2005
  3. Math

    A flowerbed is 3 3/4 meters long and 2 meters wide. Uncle James wants to build a border around the flowerbed. The width of the border is 1/2 meter. The cost of building the border is $20 per square meter. How much does Uncle James

    asked by Sophie on April 10, 2015
  4. math

    a poster is 25cm taller than it is wide. it is mounted on a piece of cardboard so that there is a 5cm border on all sides. if the area of the border alone 1350cm^2, what are the dimensions of the poster? It is almost a must to

    asked by jason on November 29, 2006
  5. 3rd grade

    use the grid. draw two different figures that each have a perimeter of 14. find each area. question 2. draw a figure with the given perimeter of 10 units and 22 units. question 3. explain why it would be important to know the area

    asked by Kainalu Mauga on March 9, 2010
  6. Understanding Perimeter

    When finding the perimeter of a figure on a grid, why do you not count the spaces inside the grid?

    asked by Sharon on February 25, 2013

You can view more similar questions or ask a new question.