title: Maze generator applet
body: <p>This is a simple maze generator in which the program asks the user to input the size of the maze through the console and displays the maze through an applet .. i would like to input the size through a text field in a form .. how would i be able to do that?</p>
  
  <pre><code>import java.awt.*;
  import java.util.*;
  import java.applet.*;
  
  public class hexagonal extends Applet implements Runnable
  {
  
      public static final int TOPL = 1;
      public static final int TOPR = 2;
      public static final int RIGHT = 4;
      public static final int BOTTOMR = 8;
      public static final int BOTTOML = 16;
      public static final int LEFT = 32;
  
      int cells[][];
      Point current_cell;
      Stack hold;
      int total, count;
      int gridw, gridh, cellw, cellh;
      boolean finished = false;
  
      Image offscreen;
      Graphics offgr;
      Thread t;
  
      public void init()
      {
          int x, y;
          Scanner uInput = new Scanner(System.in);
          System.out.print("Enter the size of your hexagonal maze: ");
          gridw = uInput.nextInt(); // 10
          gridh = uInput.nextInt(); // 8
          cellw = 30;
          cellh = (int)(0.866*cellw); // approx. regular hexagon
  
          cells = new int[gridw][gridh];
  
          int full = TOPL | TOPR | RIGHT | BOTTOMR | BOTTOML | LEFT;
          for (x = 0; x &lt; gridw; x++)
              for (y = 0; y &lt; gridh; y++)
  
          int top = (TOPL | TOPR) &lt;&lt; 6;
          int bottom = (BOTTOML | BOTTOMR) &lt;&lt; 6;
          for (x = 0; x &lt; gridw; x++)
          {
              cells[x][0] |= top;
              cells[x][gridh-1] |= bottom;
          }
  
          int left = (LEFT | BOTTOML) &lt;&lt; 6;
          int right = (RIGHT | TOPR) &lt;&lt; 6;
          for (y = 0; y &lt; gridh; y++)
          {
              cells[0][y] |= left;
              cells[gridw-1][y] |= right;
          }
  
          total = gridw*gridh;
          offscreen = createImage(432, 237);
          offgr = offscreen.getGraphics();
      }
  
      public void start()
      {
          t = new Thread(this);
          t.start();
      }
  
      public void stop()
      {
          t.stop();
          t = null;
      }
  
      public void run()
          {
              int dir;
  
              current_cell = new Point(rnd(gridw)-1,rnd(gridh)-1);
              count = 1;
              hold = new Stack();
              while (count &lt; total)
              {
                  dir = findNewNbr(current_cell);
  
                  if (dir == 0)
                      current_cell = (Point)(hold.pop());
                  else
                  {
                      removeWall(current_cell, dir);
                      count++;
                      hold.push(current_cell);
                      current_cell = getNbr(current_cell, dir);
                  }
  
  
                  if(total == count)
                      finished = true;
  
                  repaint();
                  try
                  {
                      Thread.sleep(0);
                  } catch (Exception e) {};
              }
  
              repaint();
      }
  
      public void update(Graphics g)
      {
          paint(offgr);
          g.drawImage(offscreen,0,0,this);
      }
  
      public void paint(Graphics g)
      {
          int val, x , y;
  
          int basex = 10;
          int basey = 10;
          g.setColor(Color.white);
          g.fillRect(0, 0, gridw*cellw+(cellw+1)*gridh/2, (gridh+2)*cellh);
          g.setColor(Color.black);
  
          int x1=0, x2=0, x3=0, y1=0, y2=0, y3=0, y4=0;
          for (x = 0; x &lt; gridw; x++)
              for (y = 0; y &lt; gridh; y++)
              {
                  val = cells[x][y];
  
                  x1 = basex + x*cellw + y*cellw/2;
                  x2 = x1 + cellw/2;
                  x3 = x1 + cellw;
                  y1 = basey + y*cellh;
                  y2 = y1 + cellh/3;
                  y3 = y1 + cellh;
                  y4 = y1 + 4*cellh/3;
                  if ((val &amp; TOPL) != 0)
                      g.drawLine(x1, y2, x2, y1);
                  if ((val &amp; TOPR) != 0)
                      g.drawLine(x2, y1, x3, y2);
                  if ((val &amp; LEFT) != 0)
                      g.drawLine(x1, y2, x1, y3);
                  if ((val &amp; RIGHT) != 0)
                      g.drawLine(x3, y2, x3, y3);
                  if ((val &amp; BOTTOML) != 0)
                      g.drawLine(x1, y3, x2, y4);
                  if ((val &amp; BOTTOMR) != 0)
                      g.drawLine(x2, y4, x3, y3);
              }
  
              g.setColor(Color.white);
              if ((count &lt; total) &amp;&amp; (current_cell != null))
                  g.fillOval(current_cell.y*(x3-x2)+basex+current_cell.x*cellw, basey+current_cell.y*cellh+4,
                      cellw, cellh);
  
              g.setColor(Color.black);
              if(finished == true)
              g.drawString("-", 430/2 -20, 230);
      }
  
  
          int findNewNbr(Point p)
      {
  
          int full = TOPL | TOPR | RIGHT | BOTTOMR | BOTTOML | LEFT;
          int d = rnd(6)-1;
          int k = 0;
          while (k &lt; 6)
          {
              switch(d)
              {
                  case 0: 
                      if ((cells[p.x][p.y] &amp; (TOPL&lt;&lt;6)) != 0) break;
  
                      if ((cells[p.x][p.y-1] &amp; 0x3F) == full) return TOPL;
                      break;
                  case 1: 
                      if ((cells[p.x][p.y] &amp; (TOPR&lt;&lt;6)) != 0) break;
                      if ((cells[p.x+1][p.y-1] &amp; 0x3F) == full) return TOPR;
                      break;
                  case 2: 
                      if ((cells[p.x][p.y] &amp; (RIGHT&lt;&lt;6)) != 0) break;
                      if ((cells[p.x+1][p.y] &amp; 0x3F) == full) return RIGHT;
                      break;
                  case 3: 
                      if ((cells[p.x][p.y] &amp; (BOTTOMR&lt;&lt;6)) != 0) break;
                      if ((cells[p.x][p.y+1] &amp; 0x3F) == full) return BOTTOMR;
                      break;
                  case 4: 
                      if ((cells[p.x][p.y] &amp; (BOTTOML&lt;&lt;6)) != 0) break;
                      if ((cells[p.x-1][p.y+1] &amp; 0x3F) == full) return BOTTOML;
                      break;
                  case 5: 
                      if ((cells[p.x][p.y] &amp; (LEFT&lt;&lt;6)) != 0) break;
                      if ((cells[p.x-1][p.y] &amp; 0x3F) == full) return LEFT;
                      break;
              }
              d = (d+1) % 6;
              k++;
          }
          return 0;
      }
  
      void removeWall(Point p, int d)
      {
          cells[p.x][p.y] ^= d;
  
          switch(d)
          {
              case TOPL: cells[p.x][p.y-1] ^= BOTTOMR;
                  break;
              case TOPR: cells[p.x+1][p.y-1] ^= BOTTOML;
                  break;
              case RIGHT: cells[p.x+1][p.y] ^= LEFT;
                  break;
              case BOTTOMR: cells[p.x][p.y+1] ^= TOPL;
                  break;
              case BOTTOML: cells[p.x-1][p.y+1] ^= TOPR;
                  break;
              case LEFT: cells[p.x-1][p.y] ^= RIGHT;
                  break;
          }
      }
  
      Point getNbr(Point p, int d)
      {
  
          switch(d)
          {
              case TOPL: return new Point(p.x, p.y-1);
              case TOPR: return new Point(p.x+1, p.y-1);
              case RIGHT: return new Point(p.x+1, p.y);
              case BOTTOMR: return new Point(p.x, p.y+1);
              case BOTTOML: return new Point(p.x-1, p.y+1);
              case LEFT: return new Point(p.x-1, p.y);
          }
          return null; // This shouldn't ever happen
      }
  
      int rnd(int n)
          {
              return (int)(Math.random()*n+1);
      }
  }
  </code></pre>