Setting the JFrame size for my Maze

G'day Guys,

I'm having trouble setting the size of my main JFrame appropriatly for my Maze. I am generating a maze by creating a series of lines in my DrawMaze class (which extends JPanel):

g2d.draw(new Line2D.Double(x, y, x, y)

This works fine. I then go to sick it in a JFrame in my main class:

Container c = DrawMaze(x, y, cells);

jf.setContentPane(c);

jf.setSize((x * 10), (y * 10));// each cell is 10 by 10;

However this approach of sizing will set the JFrame too small (if it's a small maze) or too large (if it's a large Maze). Getting the preferred size of the container does not work either.

Here are some code snippets to clarify things a little better.

Remember, these are only snippets, if it looks like something isn't being defined or closed, thats because it is being defined/closed else where.

DrawMaze.java

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

for(int x = 0; x < length; x++){

for(int y = 0; y < height; y++){

if(cells[x][y].getWallStatus(1)){

double s = 10 * x;

double e = 10 * y;

g2d.draw(new Line2D.Double(s, e, (s + 10), e));

}

Repeat for wallStatus 2-4 (in the same for loops).

TestMain.java

publicstaticvoid main(String[] argv){

Maze m =new Maze(10,10);

x = m.getLength();

y = m.getHeight();

int size = x * y;

Container content =new DrawMaze(x, y, m.getCells());

JFrame jf =new JFrame("Test");

jf.setSize((x * 11), (y * 11));

jf.setContentPane(content);

jf.setVisible(true);

}

Any Suggestions would be greatly appreciated.

[2613 byte] By [cbr_madmonka] at [2007-11-27 6:47:27]
# 1
jf.setPreferredSize(width, height);jf.pack();Don't forget to assign your duke stars ;P
RedUnderTheBeda at 2007-7-12 18:20:17 > top of Java-index,Security,Cryptography...
# 2
you also have to make sure you're setting the size of your content pane. What happens is, you set the size and then the layout manager for your content pane says "I've got nothing in me, I'm gonna shrink to nothing", so you have to tell it "No, I'd prefer to be [heigh] x [width]".
RedUnderTheBeda at 2007-7-12 18:20:17 > top of Java-index,Security,Cryptography...
# 3
Thanks, that should about do it :-)
cbr_madmonka at 2007-7-12 18:20:17 > top of Java-index,Security,Cryptography...