April 21, 2014

Homework Help: Java Panel Problem

Posted by Robert on Saturday, December 19, 2009 at 10:34pm.

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

Answer this Question

First Name:
School Subject:

Related Questions

Astronomy - Given that the mass of the Milky Way galaxy is 10^11 times that of ...
Java - Write a GUI application that prompts the user for a number between 1 and ...
math - a pool in the shape of a circle measures 10 feet across. One cubic yard ...
Chemistry - Lewis Dots - Draw the molecule by placing atoms on the grid and ...
math 5th grade - theo wants to put a dinosaur border around the perimeter of his...
Algebra - This is a 2-part problem and I solved the first part, but can't get ...
Math - you want to expand the garden by planting a border of flowers. the border...
algebra 2 - Write the inequality in the space provided and graph it on the grid...
calc - given S=piX(X+2Y) and V=piX^2Y show that dS/dX=pi(X-Y) when V is constant...
Math - A concrete border is to be built around a triangular flower garden that ...