Java Panel Problem

posted by .

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

  • Java Panel Problem -

    Hopefully a Tu torial will h elp:


  • Java Panel Problem -

    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.

  • Java Panel Problem -

    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)

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. calc

    given S=piX(X+2Y) and V=piX^2Y show that dS/dX=pi(X-Y) when V is constant, and dV/dX=-piX(X-Y)when S is constant. Please HELP!! I have tried several times, and I don't get the result you asked for. If we're given S=piX(X+2Y), then …
  2. Math

    you want to expand the garden by planting a border of flowers. the border will have the same width around the entire garden.the flowers you brought will fill an area of 276sq.ft. How wide should the border be?

    We are doing solving polynomials using the zero factor property and here is the question: Marie has a rectangular board 18 inches by 23 inches around which she wants to put a uniform border of shells. If she has enough shells for a …
  4. Algebra

    This is a 2-part problem and I solved the first part, but can't get the second. Brad has a circular garden with a radius of 3 feet. There are 54 flowers in his garden. He wants to plant different flowers in a 1-foot border all around …
  5. math

    1. Mrs. Macdonald wants to put a uniform border of tulips around the outside of her vegetable garden. Her vegetable garden is 20m by 28m. This border will look best if the area of the tulips border equal to the area of the garden. …
  6. 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 …
  7. math gr. 10

    A concrete border is to be built around a triangular flower garden that has sides of 10 m, 7 m and 8 m. The border is to be 1 m wide on all sides of the garden. Before the concrete can be poured, wooden forms needs to be placed on …
  8. 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 …
  9. Math

    Jason has 75 feet wallpaper border.He wants to put up a wallpaper border around his rectangular bedroom that measures 12 feet by 14 feet. He multiplies 12×14=168 to get an exact answer of how much border he needs.He concludes that …
  10. Math (Integrals) (Using given method)

    I would like to solve the ∫sin^2(pix) dx Using the given substitution identity: sin^2(x) = (1/2)(1-cos2x) This is what I did so far: ∫sin^2(pix) let u = pix du = pi dx (1/pi)∫sin^2(u)du Applying the identity is where I'm lost …

More Similar Questions